« EPiServer Module 3.3.0.39 Released | Main | SharePoint Module 3.1 Hotfix 2 now available »

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;
}
}

TrackBack

TrackBack URL for this entry:
http://blogs.networkedplanet.com/mt/mt-tb.pl/105

About

This page contains a single entry from the blog posted on July 2, 2009 3:53 PM.

The previous post in this blog was EPiServer Module 3.3.0.39 Released.

The next post in this blog is SharePoint Module 3.1 Hotfix 2 now available.

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

Subscribe to this blog's feed