• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1613
  • Last Modified:

Loop for PeopleEditor C#

I have a custom webpart that has 3 PeopleEditors on the form. This form submits to the custom list in SharePoint.  I have the code to update 1 of the PeopleEditors, but need to make this loop through 3 different people editors in the form.  Maybe a second class with my code that I could call?
string requestors = ppRequestor.CommaSeparatedAccounts;
 
            // commaseparatedaccounts returns entries that are comma separated. we want to split those up
            char[] splitter = { ',' };
            string[] splitPPReq = requestors.Split(splitter);
            // this collection will store the user values from the people editor which we'll eventually use
            // to populate the field in 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 < splitPPReq.Length; i++)
            {
            string loginName = splitPPReq[i];
            if (!string.IsNullOrEmpty(loginName))
            {
            SPUser user = web.SiteUsers[loginName];
            // you could also use SPUser user = web.EnsureUser(loginName);
            SPFieldUserValue fuv = new SPFieldUserValue(web, user.ID, user.LoginName);
            values.Add(fuv);
                
            string primaryAdmin = ppAdmin.CommaSeparatedAccounts;
        }
 
    }
 
    // set the Person or Group column
    newRequest["Requestor"] = values;

Open in new window

0
Christopher Gore
Asked:
Christopher Gore
  • 2
1 Solution
 
Christopher GoreSolutions ArchitectAuthor Commented:
I solved it.
newRequest["Requestor"] = GetUsersFromPeopleEditor(ppRequestor.CommaSeparatedAccounts, web);
            newRequest["Reqestor2"] = GetUsersFromPeopleEditor(ppRequestor2.CommaSeparatedAccounts, web);
            newRequest["Requestor3"] = GetUsersFromPeopleEditor(ppRequestor3.CommaSeparatedAccounts, web);
            //---------End view section------------------
            newRequest.Update();
        }
        protected SPFieldUserValueCollection GetUsersFromPeopleEditor(string requestors, SPWeb web)
        {
            // commaseparatedaccounts returns entries that are comma separated. we want to split those up
            char[] splitter = { ',' };
            string[] splitPPReq = requestors.Split(splitter);
            // this collection will store the user values from the people editor which we'll eventually use
            // to populate the field in 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 < splitPPReq.Length; i++)
            {
                string loginName = splitPPReq[i];
                if (!string.IsNullOrEmpty(loginName))
                {
                    SPUser user = web.SiteUsers[loginName];
                    // you could also use SPUser user = web.EnsureUser(loginName);
                    SPFieldUserValue fuv = new SPFieldUserValue(web, user.ID, user.LoginName);
                    values.Add(fuv);
                }
            }

            // set the Person or Group column
            return values;
        }

Open in new window

0
 
Christopher GoreSolutions ArchitectAuthor Commented:
Solved
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now