Add Enterprise Keywords to List with Receiver

Posted on Updated on

Let’s see, a easy tag field is the “TaxKeyword”, the default for “Enterprise Keywords”. The advantage is that he is already at the Search Schema an Indexed, so you can use in all objects that you want, Page Layouts for Publishing Pages, Lists, whatever.

How to do it in a SharePoint Feature Receiver?

SCOPE: Web

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    var web = properties.Feature.Parent as SPWeb;
    AddDocs(web);
}

private void AddDocs(SPWeb web)
{
    if (web == null) return;
    var list = web.Lists.TryGetList("Documents");
    if (list != null)
    {
        var field = list.ParentWeb.AvailableFields["Enterprise Keywords"];
        if (!list.Fields.ContainsField("Enterprise Keywords"))
        {
            list.Fields.Add(field);
            list.EnableModeration = true;
            list.EnableVersioning = true;

            var view = list.Views[0];
            view.ViewFields.Add(field);
            view.Update();

            list.Update();
            web.Dispose();
        }
        else
        {
            //Log
        }
    }
    else
    {
        //Log
    }
} 

 

How to add it to a Page Layout?

<%@ Register TagPrefix="Taxonomy" Namespace="Microsoft.SharePoint.Taxonomy" Assembly="Microsoft.SharePoint.Taxonomy, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>


 <Taxonomy:TaxonomyFieldControl FieldName="TaxKeyword" InputFieldLabel="My Custom Tags" runat="server"></Taxonomy:TaxonomyFieldControl> 

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