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!!


Monday, April 18, 2011

Performance Of SharePoint List Access Methods

As you might have experience throughout your SharePoint development, there are several methods available in SharePoint API to access the data from SharePoint lists. I wanted to measure the performance of each method available in SharePoint. In the following experiment, I have excluded SharePoint LINQ query. I’ll accommodate that with some other interesting metrics in my future post.

I have executed the following four different queries five time each on the same list in a particular sub-site. Number of executed time and the time taken to execute the query is depicted in the X axis and Y axis respectively.
Though I have played with all the available methods, certain situation may restrict you from using some of the optimized method. In case of if you are about to write an exe/console application, you can’t use PortalSiteMapProvider API.



Conculsion : Good Bye to SPQuery…!! Welcome to SPSiteDataQuery…!! Stay with me PortalSiteMapProvider…!!

P.S: The above data is extracted from Developer Dashboard, by enabling the SPMonitoredScope for each access mechanism.
SharePoint List Access Methods, SharePoint List Access Mechanism, SharePoint List Access, GetSiteData, SPSiteDataQuery

Monday, April 4, 2011

Access Denied On IISReset

Problem:

“Access denied, you must be an administrator of the remote computer to use this command. Either have your account added to the administrator local group of the remote computer or to the domain administrator group”

This is a common problem for those who have just started SharePoint 2010 development on newly installed Windows Server 2008/Windows 7 OS. Though you have the administrator rights on the computer, the default security mechanism in the OS itself enable the UAC (User Access Control) to higher level which blocks certain activities from doing deliberatively. One of such situation causes the IISRESET command to be failed, reporting the above error.

 

Solution:

Set the UAC to “Never Notify” level and make sure that you Restart the computer. Now the problem solved J