Geeks With Blogs

News
About Me:
I am a Female SharePoint Developer (a platinum unicorn). I have been working with SharePoint since I attended the Portal University in 2005. I hold a BA in Computer Science from the University of Missouri - Kansas City. I love playing Rockband, organizing user group meetings, working with code, attending events as a speaker or organizer, and having bizarre conversations about geek things with cool people. If you have any comments or questions fill out the contact form and I will try my best to help.



The Guide of a MOSSLover Becky Isserman's Blog

I was working with the People Editor Control and realized there are a couple weird things you need to do before you can read values into the control and save values into a list.  Here are a few samples related to saving multiple items into a list and reading multiple items from a list into the control.

Updating an Item from a PeopleEditor Control:

string[] UsersSeperated = peUserEditor.CommaSeparatedAccounts.Split(',');
                    SPFieldUserValueCollection UserCollection= new SPFieldUserValueCollection();
                    foreach (string UserSeperated in UserSeperated)
                    {
                        SPUser User = Web.SiteUsers[“UserField”];
                        SPFieldUserValue UserName = new SPFieldUserValue(Web, User.ID, User.LoginName);
                        UserCollection.Add(UserName);
                    }
Item["UserField"] = UserCollection

Notice that we have to separate the users, because it is comma delimited.  You have to split the string, then you cast the string in an SPUser, so that you can get the User Guid and User Login for and SPFieldUserValue and add it to the SPFieldUserValue Collection.  My field is a People and Groups field, so it requires an SPFieldUserValueCollection.

Reading an SPFieldUserValueCollection into a PeopleEditor Control:

SPFieldUserValueCollection Users = new SPFieldUserValueCollection(Web, Item["UserField"].ToString());
                    string UsersCommaDelimited = "";
                    foreach (SPFieldUserValue User in Users)
                    {
                        if (UsersCommaDelimited == "")
                            UsersCommaDelimited = User.User.LoginName;
                        else
                            UsersCommaDelimited += "," + User.User.LoginName;
                    }
peUserEditor.CommaSeparatedAccounts = UsersCommaDelimited;

In this piece of code notice that we pull the value into an SPFieldUserValueCollection, then we loop through the login name and add them to a string using commas.  Then we can go ahead and save the string into the PeopleEditor’s CommaSeperatedAccounts Function for displaying on a form.

This control is incredibly simple to use, obviously you would not need to loop through and add the commas if you set the MultiSelect value to false.  It is a little different for multiple users, so I chose to write about that instead of adding a single user value.  If anyone has comments or questions let me know.  Later guys.

Posted on Thursday, March 12, 2009 3:28 PM MOSS , Web parts | Back to top


Comments on this post: Revisiting the People Picker (PeopleEditor) Control

# re: Revisiting the People Picker (PeopleEditor) Control
Requesting Gravatar...
Very cool code! This control is not the easiest to work with.
Left by Derek on Oct 07, 2009 11:03 AM

# re: Revisiting the People Picker (PeopleEditor) Control
Requesting Gravatar...
good but i want to insert spgroups
Left by bala on Mar 27, 2010 12:12 AM

# re: Revisiting the People Picker (PeopleEditor) Control
Requesting Gravatar...
Thanks a lot sir!
It saved me lot of time.
Real a good work!
Left by Dinesh on Dec 28, 2010 6:50 AM

# re: Revisiting the People Picker (PeopleEditor) Control
Requesting Gravatar...
Thanks a lot Mam! Really a useful information for many newbie's to sharepoint like me. Your code helped me for retreiving the value from the Sharepoint List on to the people editor control while editing the list form.
Left by Dinesh on Dec 28, 2010 6:56 AM

Your comment:
 (will show your gravatar)


Copyright © MOSSLover | Powered by: GeeksWithBlogs.net