Friday, April 22, 2011

Add Enterprise Keywords Column Programmatically For SharePoint Lists

As you all are SharePoint geeks, you must be knowing the difference between Enterprise Keywords and Managed Metadata(Terms). Though I’m skipping that Montessori session, for the sake of those who are new to taxonomy field please read this. Let me to start with my today’s topic. First, how do you enable the Enterprise keyword field for a list through SharePoint List setting (manually)? Even this also a simple question seems. Yes, follow this article to recollect how would  you do, if you would have already done.
The next question is How will you do if you want to enable that programmatically?
Yes, that’s a bit of tricky question.
Because the SharePoint List definition does not have the CAML support to enable that as you enable the content type for the list definition
(<List xmlns:ows="Microsoft SharePoint"  EnableContentTypes="TRUE"… />)

So let’s have a look at SharePoint Taxonomy API does have any explicit support. If you go through the MSDN library for SPList and TaxonomySession, there is no way we can enable that.

So what is happening behind the scene, when I’m ticking the Enable Enterprise Keywords checkbox.
The Reflector helped me in finding the mystery behind that. Taxonomy assembly is having an internal class, with “EnableKeywordsField” column. So your challenge is to somehow access that and set the Boolean for true.


How would you access internal class and its properties?
Here is the answer.
               
          /// <summary>
        /// This will enable the Enterprise Keyowrds for the associated lists
        /// </summary>
        /// <param name="properties">from list event</param>
       private void EnableEnterpriseKeywords(SPListEventProperties properties)
       {
           //get an instance of list to be enabled with keyword
           SPList list = properties.List;

           //get an instance of SPTaxonomy assembly
           Assembly taxonomyAssembly = Assembly.LoadWithPartialName("Microsoft.SharePoint.Taxonomy");

           //get an instance of internal class for the keyword association
           Type listFieldSettings = taxonomyAssembly
                                   .GetType("Microsoft.SharePoint.Taxonomy.MetadataListFieldSettings");

           //pass the list to the internal class's constructor
           object listSettings = listFieldSettings.GetConstructor(new Type[] { typeof(SPList) })
               .Invoke(new object[] { list });

           //get an instance of KW property and set the boolean
           listFieldSettings.GetProperty("EnableKeywordsField", BindingFlags.NonPublic |  
                                        BindingFlags.Instance) .SetValue(listSettings, true, null);
           listFieldSettings.GetProperty("EnableMetadataPromotion", BindingFlags.NonPublic |
                                   BindingFlags.Instance).SetValue(listSettings, true, null);
           //update the list
                     listFieldSettings.GetMethod("Update", BindingFlags.NonPublic | BindingFlags.Instance)
                            .Invoke(listSettings, null);
        } 


Please format the above code J
  Now your list is enabled with the Enterprise Keywords. Cheers!!


2 comments:

  1. Do you have any examples of how to add an enterprise keyword programmatically?

    ReplyDelete
  2. Superb Article Murugamahinthan. Please note that the above code needs a little tweaking when there are multiple content types in a list.

    the following code needs to be added else the column will not be added to all the content types and the Enterprise Keyword Check box will remain Unchecked.

    listFieldSettings.GetMethod("EnsureKeywordsField", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(listSettings, null);



    ReplyDelete