PANVEGA’s Blog

DotNet Development, SharePoint Customizing, Silverlight, MS Infrastructure and other tips and tricks

Parsing the PeoplePicker and adding Users to a SP List

Posted by PANVEGA on January 3, 2008

That should do it. You should be able to save the entries from the PeoplePicker Control (PeopleEditor) to a list.

  1. Create a new Custom List named Demo. Within List Settings, create a column of type Person or Group named Employees
  2. In this particular example, I’ll be building a custom application page with a single PeopleEditor control and a submit button. Create a new page, called Demo.aspx and store it in the layouts directory.
  3. Since the people editor control is located within the Microsoft.SharePoint.WebControls namespace, you’ll have to register the tag prefix at the top of your page:
    <%@ Register TagPrefix=”wssawc” Namespace=”Microsoft.SharePoint.WebControls” Assembly=”Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” %>

  4. Insert the control on the page. If you look at my Demo.aspx page, I’ve put my control within a table tag, but you can format this however you’d like:
    <wssawc:PeopleEditor AllowEmpty=”false” Width=”300px” id=”peEmployees” runat=”server” SelectionSet=”User” />

    AllowEmtpy – let’s you specify if blank values are permitted for this control
    SelectionSet – can be User or SecGroup or SPGroup or all three: User, SecGroup, SPGroup
    MultiSelect – set to true or false, false if you don’t want the user to be able to select more than one

Note:

One thing I found was that in order for this control to successfully save a user, that user has to exist within the site collection. If they don’t exist, the web.SiteUsers call will fail with an exception ‘user cannot be found’.

SPWeb web = SPContext.Current.Web;

// get the entries submitted into the people editor and store them in a string

string employees= peEmployees.CommaSeparatedAccounts;

// commaseparatedaccounts returns entries that are comma separated. we want to split those up

char[] splitter = { ’,’ };

string[] splitPPData = employees.Split(splitter);

// this collection will store the user values from the people editor for storage into the list

SPFieldUserValueCollection values = new SPFieldUserValueCollection();

// for each item in our array, create a new sp user object given the loginname and add to our collection

for (int i = 0; i < splitPPData.Length; i++)

{

string loginName = splitPPData[i];

if (!string.IsNullOrEmpty(loginName))

{

SPUser user = web.SiteUsers[loginName];

SPFieldUserValue fuv = new SPFieldUserValue(web, user.ID, user.LoginName);

values.Add(fuv);

}

}

// set the Person or Group column

SPListItemCollection listItems = web.Lists[“Demo”].Items;

SPListItem employee = listItems.Add();

employee[Employees] = values;

employee.Update();

Advertisements

4 Responses to “Parsing the PeoplePicker and adding Users to a SP List”

  1. santosh said

    hi
    the above one is good
    what i am trying to do is i want to pass that id to a caml query
    this was giving the exception
    following is the code
    SPWeb oWeb1 = SPContext.Current.Web;
    SPList oEmpList1 = oWeb1.Lists[“Employee Information”];
    string strUser = PeopleEditor1.Accounts[0].ToString();
    SPUser oUser = oWeb1.AllUsers[strUser];
    int iUserId = oUser.ID;
    SPQuery spqueryForEmpinfo1 = new SPQuery();
    spqueryForEmpinfo1.Query = ” ”
    + “” + iUserId + ” “;
    SPListItem spListItemEmpInfoAdmin = oEmpList1.GetItems(spqueryForEmpinfo1)[0];

    “System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. at Microsoft.SharePoint.SPListItemCollection.get_Item(Int32 iIndex) at ASP.Work_Status_Change.btnDelUser_Click(Object sender, EventArgs e) ”

    can u tell me how to resolve this

  2. santosh said

    sorry man query in the above was missed
    hi
    the above one is good
    what i am trying to do is i want to pass that id to a caml query
    this was giving the exception
    following is the code
    SPWeb oWeb1 = SPContext.Current.Web;
    SPList oEmpList1 = oWeb1.Lists[“Employee Information”];
    string strUser = PeopleEditor1.Accounts[0].ToString();
    SPUser oUser = oWeb1.AllUsers[strUser];
    int iUserId = oUser.ID;
    SPQuery spqueryForEmpinfo1 = new SPQuery();
    SPListItem spListItemEmpInfoAdmin = oEmpList1.GetItems(spqueryForEmpinfo1)[0];
    spqueryForEmpinfo1.Query = ” ”
    + “” + iUserId + ” “;
    “System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. at Microsoft.SharePoint.SPListItemCollection.get_Item(Int32 iIndex) at ASP.Work_Status_Change.btnDelUser_Click(Object sender, EventArgs e) ”

    can u tell me how to resolve this

  3. santosh said

    spqueryForEmpinfo1.Query = ” ”
    + “” + iUserId + ” “;

  4. santosh said

    hi man the query i am trying to post was not displaying

    + “” + iUserId + ” “;

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: