Interview Questions administrator

Best SharePoint administrator Interview Questions and Answers with topic wise:


Recycle Bin | MMS | ManagedVsCrawledProperties |

Thursday, February 23, 2012

How to add a user and multi users to the People picker field in a sharepoint list programmtically


string projectMemberValue = string.Empty;
try
   {
      //To Store Multiple users
      if (spProjectListItem["ProjectMember"] != null)
      projectMemberValue = spProjectListItem["ProjectMember"].ToString();
      SPFieldUserValueCollection oFieldUserValueCollection = new SPFieldUserValueCollection(spDLRWeb,
                                                                                                                    spProjectListItem["ProjectMember"].ToString());
       spListItem["ProjectMember"] = oFieldUserValueCollection;
   }
 catch { }

try
   {
      // To Store Single user.
       if (spProjectListItem["ProjectOwner"] != null)
       projectOwnerValue = spProjectListItem["ProjectOwner"].ToString();
       SPFieldUser userOwnerField = (SPFieldUser)spDLRWeb.Lists["Projects"].Fields.GetField("ProjectOwner");
       SPFieldUserValue fieldOwnerValue = (SPFieldUserValue)userOwnerField.GetFieldValue(projectOwnerValue);
       projectOwner = fieldOwnerValue.User;
    }
 catch { }

Please add comments if it is really helped you or for any issues.

No comments:

Post a Comment