SharePoint – Enabling Enterprise Keywords field for libraries

This is a sample how you can enable the enterprise keywords field for a library. The funny part is that in SharePoint2013 just adding the field did not work properly. What I had to do was to use ILSpy to get an idea what the SharePoint code is doing. I found out that for some reason(in this case atleast) the exterprise keyword field need to exist so that the list content type is altered to use the enterprise keyword field. Only then I could use the field in the library properly. Don’t know why :), didn’t find out :).

public static string FeatureProperty_ListsToApplyLogic = “ListsToApplyLogic”;
private System.Guid guidKeywords = new System.Guid(“{23F27201-BEE3-471e-B2E7-B64FD8B7CA38}”);

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
if (web == null)
return;
String listsToApplyLogic = null;
if(properties.Feature.Properties[FeatureProperty_ListsToApplyLogic] != null)
listsToApplyLogic = properties.Feature.Properties[FeatureProperty_ListsToApplyLogic].Value as String;

if (String.IsNullOrEmpty(listsToApplyLogic))
return;

String[] listNames = listsToApplyLogic.Split(‘;’);

foreach (var listName in listNames)
{
SPList metadataList = web.Lists.TryGetList(listName);
if (metadataList != null)
{
if (!metadataList.Fields.Contains(this.guidKeywords))
{
SPWeb parentWeb = metadataList.ParentWeb;
SPField field = parentWeb.AvailableFields[this.guidKeywords];
metadataList.Fields.Add(field);
}
else
{
this.KeywordsFieldExistsInContentTypes(true, metadataList);
}
metadataList.Update();
}
}

}

bool KeywordsFieldExistsInContentTypes(bool bAdd, SPList metadataList)
{
SPField sPField = null;
bool result = true;
if (!metadataList.AllowContentTypes)
{
return result;
}
foreach (SPContentType sPContentType in metadataList.ContentTypes)
{
if ((sPContentType.Id.IsChildOf(SPBuiltInContentTypeId.Document) || sPContentType.Id.IsChildOf(SPBuiltInContentTypeId.DocumentSet)) && sPContentType.FieldLinks[this.guidKeywords] == null)
{
if (bAdd)
{
bool @sealed = sPContentType.Sealed;
bool readOnly = sPContentType.ReadOnly;
try
{
if (@sealed || readOnly)
{
sPContentType.Sealed = false;
sPContentType.ReadOnly = false;
}
if (sPField == null)
{
SPWeb parentWeb = metadataList.ParentWeb;
sPField = parentWeb.AvailableFields[this.guidKeywords];
}
SPFieldLink fieldLink = new SPFieldLink(sPField);
sPContentType.FieldLinks.Add(fieldLink);
sPContentType.Update(false, false);
continue;
}
finally
{
if (@sealed || readOnly)
{
sPContentType.Sealed = @sealed;
sPContentType.ReadOnly = readOnly;
sPContentType.Update(false, false);
}
}
}
result = false;
if (!bAdd)
{
break;
}
}
}
return result;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s