Insert multi user or more than one user in Sharepoint list person or group field

Source: Internet
Author: User

Objective

In this article, I am going to explain, how to insert multi user or more than one user in Share Point list.

About Sharepoint list where value wocould be inserted

  1. There is a list calledTestinguser.
  2. There is one column in list calledUsertest.
  3. This column is of TypePerson or group.
  4. Allow multiple selections are true.
  5. Allow selection is setPeople and group.

Testinguser list looks like below,

How to insert?

Let
Us suppose, you need to add more than one user at single time in
Sharepoint list. Users 'values may come from an active directory or
Metadata. All the users are in a string as comma or semicolon separated
Value. Let us suppose users are in below format.

String usercontrolvalue = "Dhananjay, Arun, anoj, Patra, Mub ";

We need to inert these multiple users in a column of type people or group in Sharepoint list.

Step 1

First
We need to split name of all the users from the string. Since in our
Case users are comma separated so below Code will make a String Array
With users as value at different index.

String [] userarray = usercontrolvalue. Split (',');

Step 2

Now
We need to convert users as string to spfielduservalue. Below Function
Is taking user name as string and converting that
Spfielduservalue.

_ Web variable is denoting current Web where list is part.

Public spfielduservalue convertloginname (string userid)
{
Spuser requireduser = _ web. ensureuser (userid );
Spfielduservalue uservalue = new spfielduservalue (_ web, requireduser. ID, requireduser. loginname );
Return uservalue;
}

Step 3

We need to make instance of spfielduservaluecollection

Spfielduservaluecollection usercollection = new spfielduservaluecollection ();

Step 4

For all the users; we need to convert that as spfielduservalue then add that them to spfielduservaluecollection instance

For (INT I = 0; I <userarray. length; I ++)
{
Spfielduservalue usertoadd = convertloginname (userarray [I]);
Usercollection. Add (usertoadd );
}

Only we need to perform abve said task. For your reference entire code as below,

Entire code

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using Microsoft. SharePoint;

Namespace testingmultipleusre
{
Public partial class _ default: system. Web. UI. Page
{
Spweb _ web;
Spsite _ site;
Splist mylist
Protected void page_load (Object sender, eventargs E)
{
String usercontrolvalue = "Dhananjay, Arun, anoj, Patra, Mub ";
_ Site = new spsite ("http: // adfsaccount: 2222 /");
_ Web = _ site. openweb ();
Mylist = _ web. Lists ["testinguser"];
_ Web. allowunsafeupdates = true;
Mylist = _ web. Lists ["testinguser"];
Splistitem item = mylist. Items. Add ();
Spfielduservaluecollection usercollection = new spfielduservaluecollection ();
String [] userarray = usercontrolvalue. Split (',');
For (INT I = 0; I <userarray. length; I ++)
{
Spfielduservalue usertoadd = convertloginname (userarray [I]);
Usercollection. Add (usertoadd );
}
Item ["usertest"] = usercollection;
Item. Update ();
}
Public spfielduservalue convertloginname (string userid)
{
Spuser requireduser = _ web. ensureuser (userid );
Spfielduservalue uservalue = new spfielduservalue (_ web, requireduser. ID, requireduser. loginname );
Return uservalue;
}
}
}

Note:
Make sure all the users; you are adding are user of the Sharepoint.

Output

While running output wocould be as below
Conclusion

In this article, I have explained how to insert more than one user in Sharepoint list. Thanks for reading.

Resource: http://www.c-sharpcorner.com/uploadfile/dhananjaycoder/multiple-user-insertion-in-sharepoint-list/

Resource: http://blogs.msdn.com/ B /uksharepoint/archive/2009/02/17/quick-tip-using-the-sharepoint-person-or-group-field-in-code-part-1.aspx

(Do not recommended)

 

One of the coolest types of site column (or field) in Sharepoint is the 'person or group' field type. this allows you to select people from the gal using a nice little picker control. when users are selected, you benefit from all of the presence capabilities of SharePoint as shown in this screen shot:

 

 

I recently had to programmatically add users to one of these 'person or group' fields where I only had the login name (domain \ User) of the user I was adding as an input. it took me a while to work out how to populate a 'person or group' field from code so I thought I 'd share my findings. it is worth noting that my esteemed co-workers Nigel Bridport and Pete Mellish both helped with figuring out the right format for this.

 

In order to populate these fields, you need a very specific combination of the user ID (see below for how to get this), their full Login Name (domain \ User) and some magic characters (;#). the actual formula is as follows

 

<Userid >;# <userloginname>

 

For example

 

10; # Europe \ mkearn

 

In actual fact, I found that if you are only entering one person, you only need the user ID, but I think it is best to add the alias if you can. if you wish to add multiple people it is the same formula but the different people are separated by; # as shown below

 

<Userid1 >;#< userloginname1 >;#< userid2 >;#< userloginname2 >;#< userid3 >;#< userloginname3>

 

For example

 

10; # Europe \ mkearn; #3; # Europe \ ashleyy; # 12europe \ johnc

 

Word of warning .... If you want to add your 'Multiple person or group' field via a feature or site definition, there are some key things you need to know about multiple user fields. see George Bonney's great article which give you all of the details on how to do this.

 

So now we have the right formula, how do we actually use it?

 

The USERID relates to the ID of the user within the site collection. this is fine but the chances are that not all users are members of the site collection. to address this, the SharePoint OM has a handy little method called spweb. ensureuser () which accepts a login name of a user as a string and first checks if the user exists in the site collection, if it does not then it adds the user and returns an spuser object. please be aware that you may need to set spweb. allowunsafeupdates = true for this to work.

 

From the spuser object it is very simple to get the ID (spuser. id. tostring () and spuser. loginname. tostring () and then use simple string building to get the string in the right format for the 'person or group' field.

 

The following code is a simple example of a console application which accepts a semi-colon delimited list of aliases (I. e. domain \ User; domain \ User) and then adds them to a 'person or group' field called 'mypersonfield' in a list.

 

You can add this to the main method of a console application and so long as you add the necessary references to SharePoint DLLs (and the right using statements) it will work if you execute it on a Sharepoint Server (Be sure to change the values of site, list and the name of the 'mybody' field to match your setup ).

 

Console. writeline ("enter a; delimited list of domain \ alias that need to be added :");
String saliases = console. Readline (); // captures whatever the user entered
String svaluetoaddtofieldinsp = ""; // used to build the full string needed for the person Field

 

String sallcontacts = "";

 

Using (spsite site = new spsite ("http: // sites/site/yoursite "))
{
Site. allowunsafeupdates = true;
Using (spweb = site. rootweb)
{
Web. allowunsafeupdates = true;
String [] aaliases = saliases. Split (';');
Foreach (string salias in aaliases)
{
Spuser user = web. ensureuser (salias );
Sallcontacts + = user. Id. tostring () + "; #" + User. loginname. tostring () + ";#";
}
Web. Update ();
}
}

 

If (sallcontacts. endswith (";#"))
{
Sallcontacts = sallcontacts. substring (0, sallcontacts. Length-2 );
}

 

// Add the list item
Splist L = web. Lists ["<name of your list>"];
Splistitem li = L. items. Add ();
Li ["title"] = sallcontacts;
Li ["myperson"] = sallcontacts;
Li. Update ();
Console. writeline ("done ");

 

Using the infopath contact selector Control

 

One of the main places where you might use the technique described in
This article is in an infopath where you wish to use the users selected
In a contact selector control and promote them to SharePoint as
'Person or group'. Unfortunately, this is not quite as simple as it
Shocould be Because infopath does not natively understand the 'person or
Group 'field type in SharePoint so you cannot directly promote it
Property promotion.

 

The explanation of how you might do this in infopath is too lengthy
For this article so watch this space for a follow-up coming soon !!

 

That is the end of the article, I hope you found it useful.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.