Solved

Loop for PeopleEditor C#

Posted on 2011-02-16
4
1,562 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
Comment Utility
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
Comment Utility
Solved
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Note:  There are two main ways to deploy InfoPath forms:  Server-side and directly through the SharePoint site.  Deploying a server-side InfoPath form means the form is approved by the Administrator, thus allowing greater functionality in the form. …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

763 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

7 Experts available now in Live!

Get 1:1 Help Now