Main

SharePoint Module Archives

July 2, 2009

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(@"SourceFieldId{0}ShowDrafts{1}ShowVersionInformation{2}", relatedTopicFieldSource, showDrafts, showVersionInfo);

string fldxml = String.Format(@"{3}", typeName, displayName, internalName, customisation);

web.Fields.AddFieldAsXml(fldxml);
}
}
catch (Exception e)
{
//Log the error
throw;
}
}

About SharePoint Module

This page contains an archive of all entries posted to NetworkedPlanet On Topic in the SharePoint Module category. They are listed from newest to oldest.

NPCL Schema Editor is the previous category.

TMCore is the next category.

Many more can be found on the main index page or by looking through the archives.

Subscribe to this blog's feed