Say you have a list and you wish to copy and paste from excel through the Grid-View Function. You will notice two things as it relates to the People and Groups Field.
- It will not take users that do not already exist in SharePoint
- It will not copy at all if the people and groups has the "Allow Multiple Selections" checkbox checked
Well now that's a bummer.
The following solution is just what I came up with:
What we are going to do is make a new field that will store a semi-comma delimited list and populate the users in there. We are then going to write some quick c# do take the data from that column and populate correctly across to the correct column.
The first function we will write will take in the SPWeb that you wish to do this for as well as the semi-comma delimited list.
{ SPFieldUserValueCollection userCollection = new SPFieldUserValueCollection();
{ SPUser user = null;
{ user = web.AllUsers[sUser]; }
{
{ web.AllUsers.Add(sUser, "", sUser, ""); web.Update(); user = web.AllUsers[sUser]; }
}
{ userCollection.Add(new SPFieldUserValue(web, user.ID, user.LoginName)); } }
} } |
The second function we will write will be to take the list, and actually update the list based on the previous method.
public {
{
{
{
SPList list = null;
SPListItemCollection listCol = null;
list = sPWeb.Lists[listName]; listCol = list.Items; dt = listCol.GetDataTable();
{ SPFieldUserValueCollection userCollection = new SPFieldUserValueCollection(); userCollection = UserValidation(sPWeb, dr["Multi"].ToString()); SPListItem ListItem = list.GetItemById((int)dr["Id"]); ListItem["PersonField"] = userCollection; ListItem.Update(); } } } }
{
} } |
Happy coding!
No comments:
Post a Comment