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

 

Tuesday, March 29, 2011

SPWeb Vs SPSite Vs SPWebApplication

If you are a beginner to SharePoint its vital to get an clear knowledge and should have a clear idea to distinguish SPWeb and SPSite and SPWebapplication. If you have a SharePoint environment setup in your machine try writing the following simple console application. This will go beyond the definitions and will be ease to understand and clarify these three buzz words.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;

namespace MyFirstTest
{
class Program
{
static void Main(string[] args)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWebApplicationCollection webappcol = SPWebService.ContentService.WebApplications;
foreach (SPWebApplication webapp in webappcol)
{
Console.WriteLine("Web Application URL :" + webapp.Name);

foreach (SPSite site in webapp.Sites)
{
Console.WriteLine("Site Collection URL :" + site.Url);
Console.ReadKey();

foreach (SPWeb web in site.AllWebs)
{
Console.WriteLine("Site URL :" + web.Url);
Console.ReadKey();
}
}
}

});
}
}
}

OutPut:

Web Application URL : Sharepoint-80

Site Collection URL : http://cd-Home

Site URL : http://cd-Home
http://cd-Home/Test
http://cd-Home/Test/Inside

Note:Since I am getting the "site.AllWebs" property it will iterate through all the webs in the Site Collection

Try in your environment you can understand the difference.....

Monday, March 7, 2011

Error occurred in deployment step 'Activate Features': Invalid field name.

Error occurred in deployment step 'Activate Features': Invalid field name. {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

The above error may be a common problem on the deployment step, where the project contains site columns and content type/lists. The content type or list will be referencing the site column.

Cause :
There are three reasons for the above behavior.
1. If you have already deployed the solution and you get the error on later attempt, then the declarative mark-up of Field definition/List Schema could be modified from the version you already deployed to the site. Because the content db is queried for a non-existent object, where at that point it throws an error.

2. Referencing site column(field) from content type/lists cannot be resolved by the  deploying thread. Your feature will be structured as below. The referee column located in the “Elements” Module is referred from “Test ContentType” Module. On deployment the feature is activated in the following order.

3. The template/structure of the schema/field file is wrong.

Solution :
1.For the former case, make sure that you retract the package with the same version of above files and then modify the changes in fields/schema to get the newer version effect.

2. For the latter case, the site column(field) should be already deployed in the site before it’s being used by any other module(Test ContentType). In order to do that you don’t need to go for another feature, but simply you can change the order in the feature. In this case, the deployable feature takes the following form.

3. For the third case, rectify the issue in the schema/fields file.

Thursday, February 3, 2011

Coping DLL from the Global Assembly Cache

You Can Easily copy any .dll which is installed in GAC using windows. If you browse to assembly folder placed in c:\windows you will have only the option to uninstall. Rather you browse to GAC_MSIL folder under c:\windows\assembly you can see the installed assembly folders. Each folder has a subfolder under it. In sub folder you can find the DLL what you need. There is no need of using the shfusion.dll to copy the DLL’s.

Steps are as follows:

1.       Browse to c:\windows\assembly\ GAC_MSIL
2.       Each assemblies has a folder, with each version being a subfolder under that.
3.       In the subfolder is the DLL that you can copy to a different folder of you choosing.


Wednesday, February 2, 2011

Control Anonymous User from Accessing Application Pages

I came across a requirement where the SharePoint site should be enabled for the anonymous users in the meantime, they should be restricted to view from the application pages. So first my choice was ViewLockDownFeature, shipped with SharePoint 2007 STSADM. I thought I saved the time by just enabling the feature would solve me the problem. Yes it solved me the problem but it spawned me another problem where the feature blocks the anonymous users navigate to the blog entries, since the blog entries are accessing the Lists directly and get the page contents.

Sample Url of blog

../myblog/Lists/Categories/Category.aspx?CategoryId=2&Name=Category 2

Workaround 1:

Setting the policy at web.config level.

<configuration>

<location path="_layouts/viewlsts.aspx ">

<system.web>

<authorization>

<deny users="*"/>

</authorization>

</system.web>

</location>

</configuration>

For further information follow up here.

The problem in the above method is, if you want to block all the application pages and still wants the blog accessible to anonymous users, then you have to specify all the application page url.

Workaround 2:

Tweaking with Delegate control

SharePoint is a pool to play with delegate controls, where the winner is supposed to get a chunk of development time compared to other optional method.

Normally SharePoint delegate controls resides in Master page and Page layout as well as in Application Pages. First I delve into the delegate controls for the custom navigation control. This is the second time, where I observed that all the application pages are having the delegate control. So overriding the delegate control in the following manner will help you to implement your own policy to ranging from different user groups to different site, page and to url level.

public class AnonyousePolicy:UnsecuredLayoutsPageBase

{

protected override void OnLoad(EventArgs e)

{

if (System.Web.HttpContext.Current.Request.Url.PathAndQuery.IndexOf("_layouts/RedirectionModule/") > 0)

{

//Do Nothing as Anonymous users need to get that redirection available

}

else if (System.Web.HttpContext.Current.Request.Url.PathAndQuery.IndexOf("_layouts/", StringComparison.InvariantCultureIgnoreCase) > 0)

{

if (SPContext.Current.Web.CurrentUser == null)

{

// Anonymous user, prevent access

SPUtility.TransferToErrorPage("Anonymous users have no access to this page");

}

}

}

}

Conclusion : I feel the first method is a simple way to block the anonymous users, provided the accessibility is not restricted upon the user group and less number of application pages.

Second method is handy method is more flexible than the first method I hope !

Expect your feedback.