Sharepoint Module - Adding Related Topic Name fields programmatically
Related Topic Name and Related Topic Link Fields are custom fields which you may want to add to the site columns collection programmatically. However SharePoint sometimes returns exceptions when trying to use SPFieldCollection.CreateNewField(), the code below shows how to use an feature receiver to create the custom fields programmatically and add them to the site columns.
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
var site = properties.Feature.Parent as SPSite;
if (site != null)
{
SPWeb web = site.RootWeb;
const string typeName = “RelatedTopicName”; //important - the type name must be correct
const string displayName = “Related Themes”;
const string internalName = “RTN_Related_Themes_001”;
const string relatedTopicFieldSource = “37d6a246-0cd3-449c-8c06-6ccd75bc1c8c”;
const string showDrafts = “true”;
const string showVersionInfo = “false”;
string customisation = String.Format(@”
string fldxml = String.Format(@”<Field Type=”“{0}”” DisplayName=”“{1}”” Name=”“{2}””>{3}</Field>”, typeName, displayName, internalName, customisation);
web.Fields.AddFieldAsXml(fldxml);
}
}
catch (Exception e)
{
//Log the error
throw;
}
}