SharePoint Gen

The official blog of Chandrasekar, Microsoft SharePoint PFE

Programmatically adding a person or group to a list ( People/Group Column) who is not a part of the Site Collection.

Programmatically adding a person or group to a list ( People/Group Column) who is not a part of the Site Collection.

  • Comments 1

Consider there is a custom list that contains multiple custom columns with one of the column being of type Person or Group column.  Say that we want to show all the site collection in the same Web App in one of the column and show the users of that site collection in the other person/group column.  Programmatically we can add users who belong to the current site collection.  But, when you try to add other users who doesn't belong to the current site collection, it doesn't add those users in the People/Group column.  Through SharePoint UI when we try to implement this it works.

But why ?  To know the reason we need to understand a bit of internal working process when an user is added.  Every time when a new user is added to the site collection, each user gets user id.  It starts with the user id as 8, then goes to 9,10...  So when we try to add an user of other site collection in people/group column, it just gets the id (say for eg 8) and checks in the current site collection and gets the user with the user id 8 in the current site collection.  And then it adds a user with the id 8 in the current site collection instead of the user with the user id 8 in other site collection. If the user id in the current site collecion is not found  ( say for eg 10), error is thrown saying "user not found".

Through UI when we try to achieve the same task it works fine.  Actually what happens is that when we try to add an user from different site collection in a list (in people/group column) before adding to the list, it adds to People and Groups and from there it retrieves the user.  So it works perfectly fine.  First it checks if the user exists in People and Groups and from there the user is retrieved and shown in the list.

This is what we need to know through object model as well.  So programmatically before we add an user of different site collection to a list, first we need to add them in People and Group in the current site collection and then add the user to the list.

First get the user from different site collection.  Then add them to People and Group in the current site collection.  And then programmatically add the user to the list in the current site.

SPFieldUserValueCollection fieldUserValues = new SPFieldUserValueCollection();
SPUser listUser;
listUser = webApp.Sites[1].OpenWeb().AllUsers.GetByEmail("EMAIL ID");//Get the user from different site collection
webApp.Sites[0].OpenWeb().AllUsers.Add(listUser.LoginName,listUser.Email, listUser.Name,"Test");//Add the user to the actual SC where the list exists
listUser = webApp.Sites[0].OpenWeb().AllUsers.GetByEmail("EMAIL ID"); // Now, get the user from the actual SC.
fieldUserValues.Add(new SPFieldUserValue(webApp.Sites[1].OpenWeb(), listUser.ID, listUser.Name));

Leave a Comment
  • Please add 8 and 2 and type the answer here:
  • Post