Solved

Loop for PeopleEditor C#

Posted on 2011-02-16
4
1,571 Views
Last Modified: 2013-12-17
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
Comment
Question by:Christopher Gore
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
Christopher Gore earned 0 total points
ID: 34928465
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
 
LVL 1

Author Closing Comment

by:Christopher Gore
ID: 34928469
Solved
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now