Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I would like to add users from a people picker as separate list items to a custom list. Following code only saves the last person in my peoplepickerlist

string users = ppUsers.CommaSeparatedAccounts;
char[] splitter = { ',' };
string[] splitPPData = users.Split(splitter);

 SPList SiteAccessList = newSite.RootWeb.Lists["Site Access Rights"];
      newSite.RootWeb.AllowUnsafeUpdates = true;

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

                            if (!string.IsNullOrEmpty(loginName))
                            {
                                string LoginName=loginName;
                                SPUser user = newSite.RootWeb.EnsureUser("SPTEST\\"+ LoginName);
                             SPItem newEntry = SiteAccessList.Items.Add();
                            newEntry["Title"] = "New User";
                            newEntry["Permissions"] = "Contributor";
                            newEntry["UserName"] = user.Name.ToString();
                            newEntry["UserEmail"] = user.Email.ToString();
                            newEntry.Update();                                    
                            }
                        }
                        newSite.RootWeb.AllowUnsafeUpdates = false;
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Are you missing some code? How does PPeople get created? You only create one newEntry object here, too, outside the loop. You might want to move that line inside the loop, just after the EnsureUser call.

share|improve this answer
    
Thanks James, that was the problem: SPItem newEntry = SiteAccessList.Items.Add(); Must be in my loop... –  Wim Hill Oct 10 '11 at 14:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.