Many times we have requirement of adding multiple values to a column of type "Persons and Group" through object model. There's a way to do that very easily.
public void AssignMultipleValues(String strSemicolonSeperatedMulipleUsers)
{
String[] userName;
SPUser objSPUser = null;
SPFieldUserValueCollection userCollection = new SPFieldUserValueCollection();
if (!string.IsNullOrEmpty(strSemicolonSeperatedMulipleUsers))
{
userName = StringToArray(strSemicolonSeperatedMulipleUsers, ";");
foreach (string strUserName in userName)
{
objSPUser = SPContext.Current.Web.EnsureUser(strUserName);
if (objSPUser != null)
{
userCollection.Add(new SPFieldUserValue(SPContext.Current.Web, objSPUser.ID, objSPUser.Name));
}
}
if (userCollection.Count > 0)
item[field.ColumnName] = userCollection;
item.Update();
}
}
public string[] StringToArray(string input, string separator)
{
string[] stringList = input.Split(separator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
return stringList;
}
Thanks
No comments:
Post a Comment