Friday, December 31, 2010

SharePoint 2010: Taxonomy Event Receiver?

One of the power of SharePoint is extensibility. You can hook your custom code in different places in different time with Event Receiver. Taxonomy is new and powerful feature added in SharePoint 2010. Unfortunately, the taxonomy missing event receiver feature. If you want to do something when taxonomy added, deleted or updated, you are unlucky that SharePoint 2010 doesn’t provide event receiver in SharePoint 2010 for taxonomy.

 

Why taxonomy event receiver needed?

With taxonomy support, SharePoint is a good place to manage taxonomy. However, if I need this taxonomy to use in another application (like another asp.net application) then with the event receiver I could sync my asp.net application with SharePoint taxonomy. Also when a taxonomy is deleted I would like to run my own code to allow or disallow the deletion. So taxonomy event receiver would be very handy if it would be available.

 

How taxonomy managed in SharePoint?

Let’s discuss a bit about how taxonomy managed in SharePoint. Taxonomy is managed by central administration (with managed metadata service). So when you add/delete/update taxonomy, the taxonomies are managed in central administration site. However, for faster retrieval of taxonomies in individual web, a hidden list of taxonomies maintained in each site collection. So in each site collection, there’s a hidden list TaxonomyHiddenList that keeps the copy of taxonomies from central administration site. And in every hour a timer job “Taxonomy Update Scheduler” is run to sync the taxonomies between central admin and site collection. However, the hidden list in site collection doesn’t contain all taxonomies from central admin rather taxonomies that are used in the webs of the site collection. So the hidden taxonomy list have only keywords and taxonomies used in the webs of the site collection. You can get the contents of the hidden list by browsing “http://mysitecollection/Lists/TaxonomyHiddenList” where mysitecollection is your site collection url. Few things to notice for the hidden list:

  • In case of edit mode, the taxonomy is read from central administration and as user save the list item, the taxonomy/keywords is saved from hidden list.
  • In case of viewing an item, it’s for sure that the taxonomy/keyword is in hidden list (it was put in hidden list during edit mode). So the taxonomy/keyword is shown from hidden list.

 

Event Receiver for Taxonomy Hidden List

Now we know the taxonomies are kept in a hidden list at site collection level. And in every hour,  the hidden list is synchronized with central administration site. So if we add event receiver for the hidden taxonomy list,then in every hour our event receiver will be fired while the hidden list will be synchronized. So adding an event receiver for hidden taxonomy list will solve the problem apparently. But there’s still problem. Not all taxonomies are added to the hidden list from central administration. Only the taxonomies added to subsites of site collection, are added to the hidden list. Now the problem of event receiver for taxonomies can be divided into two categories:

  • Need event receiver only for taxonomies used in subsite of site collection: If you need the event receiver only for taxonomies used in subsites of site collection, then having the event receiver for hidden taxonomies will do. However, you’ll have to wait for one hour to fire the event receiver.
  • Need event receiver for all taxonomies: If you need your event receiver to be fired for every taxonomy (added/edited/deleted) manipulation, then tapping the hidden taxonomy list will not work as not all taxonomies will be added from central admin to hidden taxonomy list. So what might be the solution for this? One solution is to create a custom timer job of your own and in that timer job, use all taxonomies in a test list. Since all taxonomies are used in your site, so all taxonomies will be synchronized with the site collection and your event receiver will be fired for all taxonomies.
 

Solution (Re-explained)

so if only care for taxonomies used in the site collection then adding an event receiver for hidden list will work. In every hour the hidden taxonomy lists will be synchronized and your event receiver for that hidden list will be fired.

However, if you need to care all taxonomies (not just taxonomies used in the site collection), then you need to follow the steps:

  1. Create a test list with one metadata field.
  2. Create a timer job to add all metadata from central admin to the list’s metadata field. The purpose of this job is make sure all taxonomies from central admin are used in site collection.
  3. Now add the event receiver for hidden list and you are done.

Saturday, December 25, 2010

SharePoint 2010: Linking SharePoint User to Active Directory User

While we are using SharePoint Foundation, Sometimes we need to get the active directory user details based on current logged in user. If you are using SharePoint Server then this is not a big deal as you can get the user details through user profile. However, if you are using SharePoint Foundation then there’s no shortcut way to getting user details. However, one of my client is using SharePoint Foundation and wanted to get the user details from active directory (say user’s First Name). Here’s how I’ve achieved this:

 

Step 1: Created a timer job to import user details from Active Directory

I have created a custom list to store imported data from Active Directory into SharePoint. The list looks like below

image

Figure 1: SharePoint List to keep Active Directory User Details

 

As shown in the figure 1, the SID is the key to map a SharePoint user to Active Directory User. The following code snippet shows the code to import data

First I created DTO class to represent LDAP User:

public class LdapUser
{
    public int ID { get; set; }
    public bool IsActive { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public string SId { get; set; }
    public string DisplayName { get; set; }
    public string UserName { get; set; }
}

Figure 2: An DTO to keep Active directory and/or SharePoint list data

 

The following code is to get the Active Directory data into LdapUser DTO:

public IList<LdapUser> GetUsersFromActiveDirectory(string connectionString, string userName, string password)
{
    var users = new List<LdapUser>();
const int UF_ACCOUNTDISABLE = 0x0002; using (var directoryEntry = new DirectoryEntry(connectionString, userName, password, AuthenticationTypes.None)) { var directorySearcher = new DirectorySearcher(directoryEntry); directorySearcher.Filter = "(&(objectClass=user)(objectClass=person)(objectClass=organizationalPerson)(!objectClass=computer))"; var propertiesToLoad = new[] { "SAMAccountName", "displayName", "givenName", "sn", "mail", "userAccountControl", "objectSid" }; directorySearcher.PropertiesToLoad.AddRange(propertiesToLoad); foreach (SearchResult searchEntry in directorySearcher.FindAll()) { var userEntry = searchEntry.GetDirectoryEntry(); var ldapUser = new LdapUser(); ldapUser.DisplayName = NullHandler.GetString(userEntry.Properties["displayName"].Value); if (string.IsNullOrEmpty(ldapUser.DisplayName)) continue; ldapUser.Email = NullHandler.GetString(userEntry.Properties["mail"].Value); ldapUser.FirstName = NullHandler.GetString(userEntry.Properties["givenName"].Value); ldapUser.LastName = NullHandler.GetString(userEntry.Properties["sn"].Value); ldapUser.UserName = NullHandler.GetString(userEntry.Properties["SAMAccountName"].Value); var userAccountControl = (int)userEntry.Properties["userAccountControl"].Value; ldapUser.IsActive = (userAccountControl & UF_ACCOUNTDISABLE) != UF_ACCOUNTDISABLE; var sid = new SecurityIdentifier((byte[])userEntry.Properties["objectSid"][0], 0).Value; ldapUser.SId = sid; users.Add(ldapUser); } } return users; }

Figure 2: A method to get Active Directory User data in DTO format

 

I’ve used a helper class NullHandler above, which is shown below:

public class NullHandler
{
    public static string GetString(object value)
    {
        return (value == null || value == DBNull.Value) ? string.Empty : value.ToString();
    }
}

 

The above method GetUsersFromActiveDirectory return the Ldap dto from Active Directory. Then you need to save the Ldap dto into SharePoint. The following code shown the method that will save the Ldap dto in SharePoint list:

public void SaveActiveDirectoryUsersToSharePointList(SPWeb currentWeb, IList<LdapUser> ldapUsers)
{
    const string query = @"<Where><Eq><FieldRef Name='SID'/><Value Type='Text'>{0}</Value></Eq></Where>";
    var ldapUserList = currentWeb.Lists["LDAPUsers"];
    foreach (var ldapUser in ldapUsers)
    {
        SPQuery spQuery = new SPQuery();
        spQuery.Query =  string.Format(query, ldapUser.SId);
        var items = ldapUserList.GetItems(spQuery);

        SPListItem listItem;

        //if the user exists with the same Sid then update 
        //either create a new list item.
        if (items.Count == 1)
        {
            listItem = items[0];
        }
        else
        {
            listItem = ldapUserList.AddItem();
        }
        listItem[Constants.Lists.LdapUsersList.Email] = ldapUser.Email;
        listItem[Constants.Lists.LdapUsersList.FirstName] = ldapUser.FirstName;
        listItem[Constants.Lists.LdapUsersList.LastName] = ldapUser.LastName;
        listItem[Constants.Lists.LdapUsersList.DisplayName] = ldapUser.DisplayName;
        listItem[Constants.Lists.LdapUsersList.IsActive] = ldapUser.IsActive;
        listItem[Constants.Lists.LdapUsersList.PID] = ldapUser.SId;
        listItem[Constants.Lists.LdapUsersList.UserName] = ldapUser.UserName;
        listItem.Update();
    }
}

Figure 3: A method to save DTO (LdapUser) in SharePoint list.

 

In the above method SaveActiveDirectoryUsersToSharePointList, if a listitem with the same SId as in the LdapUsers list, then the list item is updated or a new one is added. So SId is key to synchronize list item and Active Directory item.

 

After User data is imported from Active Directory to SharePoint, the SharePoint list has use details. As shown in the image below, the user doceditor properties in Active Directory is shown on the left side whereas the imported SharePoint list in right side.

image

Figure 4: Active Directory User and SharePoint list item side-by-side

 

Finally you can create a timer job to sync data from Active Directory to SharePoint list. However, I’m skipping this step for brevity.

Step 2: Retrieve SPUser’s details from SharePoint List where Active Directory data imported

As shown in the code below, you can get current SharePoint user’s SId by accessing SPUser’s Sid property. Once you have the sid you can query the list (LDAPUsers) where you imported the user data from Active Directory.

var ldapList = currentWeb.Lists["LDAPUsers"];
var currentUserSid = currentWeb.CurrentUser.Sid;
var query = new SPQuery();
query.Query = string.Format(@"<Where>
                    <Eq>
                        <FieldRef Name='PID'  />
                        <Value Type='Text'>{0}</Value>
                    </Eq>
                </Where>", sid);
var items = ldapList.GetItems(query);
if (items.Count == 1)
{
    var ldapUserListItem = items[0];
}

Conclusion

So the Active directory sid is mapped to current SPUser’s Sid. So you can access the Active Directory user’s Sid using code shown below:

var sid = new SecurityIdentifier((byte[])userEntry.Properties["objectSid"][0], 0).Value;

Then you can get the SharePoint User’s Sid by using the code snippet below:

SPContext.Current.Web.CurrentUser.Sid

Once you have the mapping, you can import any data from Active Directory to SharePoint list, sync the data with timer job and get the data of current logged in user from SharePoint list.

Tuesday, December 14, 2010

SharePoint 2010: Create Taxonomy Error “Term set update failed because of save conflict.”

I was trying to answer a user’s problem in MSDN forum. He was trying to add a term in term store. Using the term adding code (that I have taken from the post) I was getting the error “Term set update failed because of save conflict.” while I was calling the CommitAll method.

 

Problematic Code

My code is as shown below:

public static void AddTerminTermStoreManagement(string siteUrl, string termSetName, string Term)
{
    try
    {
        using (var siteTerm = new SPSite(siteUrl))
        {
            var sessionTerm = new TaxonomySession(siteTerm);
            var termStoreTerm = sessionTerm.DefaultSiteCollectionTermStore;
                    
            var collection = termStoreTerm.GetTermSets(termSetName, 1033);
            var termSet = collection.FirstOrDefault();

            if (!termSet.IsOpenForTermCreation)
            {
                termSet.IsOpenForTermCreation = true;
            }

            termSet.CreateTerm(Term, sessionTerm.TermStores[0].DefaultLanguage);
            termStoreTerm.CommitAll();
        }

    }
    catch
    {

    }

}

After investigating the code I had found the problem was in the line as shown in the above code snippet with yellow marker (termSet.IsOpen..).

 

Solution

I have fixed the error by modifying the code as shown below. I had just called the CommitAll just after changing the value of IsOpenForTermCreation as shown below:

public static void AddTerminTermStoreManagement(string siteUrl, string termSetName, string Term)
{
    try
    {
        using (var siteTerm = new SPSite(siteUrl))
        {
            var sessionTerm = new TaxonomySession(siteTerm);
            var termStoreTerm = sessionTerm.DefaultSiteCollectionTermStore;
                    
            var collection = termStoreTerm.GetTermSets(termSetName, 1033);
            var termSet = collection.FirstOrDefault();

            if (!termSet.IsOpenForTermCreation)
            {
                termSet.IsOpenForTermCreation = true;
                termStoreTerm.CommitAll();
            }

            termSet.CreateTerm(Term, sessionTerm.TermStores[0].DefaultLanguage);
            termStoreTerm.CommitAll();
        }

    }
    catch
    {

    }
}

 

Conclusion

So the problem was that when I changed the value of IsOpenForTermCreation, there was a commit pending. So without committing, I created a new term and tried to committed. So the save conflict error was thrown. If you get the same error in different scenario then u can check if u have any pending commit that u have not committed.

Monday, December 13, 2010

SharePoint 2010 Error: Accessing lookup field values using Elevated web generates exception “Value does not fall within the expected range” when the lookup fields exceeds the lookup threshould

I have a list which has 9 look columns. By default SharePoint doesn’t allow that more than 8 lookup columns (However you can modify the value). When I tried to access the list from SharePoint list view, I could access the list without any problem. I was even accessing the list from webpart code and it was working fine.

 

All of sudden I had found some piece of my code is not working. While I was trying to access the 9th lookup column value of the SharePoint List from webpart  code I had got the error “Value does not fall within the expected range”. After investigating the problem I had found the 9th field doesn’t exist in the list. After spending some time on the issue, the final summary is:

So, If your list’s lookup columns exceeds the Lookup column threshold and if you try to access the list lookup field value from code using elevated web, then you will get the error “Value does not fall within the expected range”. And interestingly the exception will be thrown for not all lookup fields rather the ‘n+1’ lookup fields whereas the n is the lookup field threshold value."

Problem At a glance

So here is how you can reproduce the issue:

  1. You have set the list view lookup threshold to N.
  2. Then you have a list MyList with more than N lookup fields.
  3. If you try to access the list from SharePoint UI, you can access the list.
  4. Even if you try to access the list from code with SharePoint object model using SPContext.Current.Web you can access all lookup field values.
  5. However, If you try to access the lookup field values using Elevated web (code under SPSecurity.RunWithElevated) you will get error for N+1 lookup fields.

Sample Code to regenerate the issue

The following code block I used to test the issue. To run the test I had set the lookup column limit to two from central admin. So the code can read first two field’s lookup value but get exception to read the third.

var webId = SPContext.Current.Web.ID;
var siteId = SPContext.Current.Site.ID;
SPSecurity.RunWithElevatedPrivileges(
() =>
{
    using (SPSite site = new SPSite(siteId))
    {
        using (SPWeb web = site.OpenWeb(webId))
        {
            var fields = new string[] {"LooupField1", "LookupField2", "LookupField3"};
            var lookupTestList = web.Lists["LookupTestList"];
            foreach (SPListItem spListItem in lookupTestList.Items)
            {
                foreach (var f in fields)
                {
                    try
                    {
                        //get excception here for reading lookup col 3
                        var value1 = spListItem[f];
                    }
                    catch (Exception exception)
                    {
                        Response.Write(string.Format("Error in reading field: {0}. Error: {1}", f, exception.Message));
                    }
                }
            }
        }
    }
});

The points to notice to regenerate the issue:

  • The exception is not thrown if I try the same code shown above in console application. However, the exception is thrown when I tried to run it webpart.
  • The exception is shown when I try to get the items by accessing List.Items. However the exception is not thrown when I get the item using any other means (like list.GetItemById etc)

Thursday, December 2, 2010

SharePoint 2007 and Visual Studio 2010: Resolution of “w3wp process does not attach” problem

Recently we have moved to Visual Studio 2010 for our SharePoint project. In Visual Studio 2008 we were using WSP builder to manage SharePoint deployment. After moving to Visual Studio 2010, we have used beta version of WSP 2010. We had converted our SharePoint projects successfully to VS 2010, we tested deployments and others are working great with VS 2010 and WSP 2010. However when we tried to start developing, we had found that w3wp process can’t be attached. We tried different approach, sometimes the attaching worked but only for the first time. If we detach the debugger and then try to attach again the debugger doesn’t work for the second time.

 

After Googling I had found another person Patrick Lamber has solved the problem in his blog. I’m reposting it in details hoping this might be helpful for someone.

  1. Click Debug => Attach to Process. The “Attach to Process” dialog comes up.
  2. In the “Attach to Process” dialog, click Select button, which will show “Select Code Type” dialog as shown below: image Figure 1: Change Code Type dialog

  3. In the “Select Code Type” dialog, select “Debug these types of code” and then check option for “Managed (v2.0..)
  4. You are done. Now you can try attaching debugger. Hope it’ll work. Smile