1

I want to insert multiple users to a list field.I have an array,it contains data like below

[{"Login":"i:0#.f|membership|user1a@site.com","Name":"user1 name","Email":""},{"Login":"i:0#.f|membership|user2@site.com","Name":"user2 name","Email":""}]

This data set does not contain user ids.So how I get user Ids for these all users at a time.

I have a code, it's only getting one user at a time. here is my code

var result = Microsoft.SharePoint.Client.Utilities.Utility.ResolvePrincipal(clientContext, clientContext.Web, emailAddress, Microsoft.SharePoint.Client.Utilities.PrincipalType.User, Microsoft.SharePoint.Client.Utilities.PrincipalSource.All, null, true);
                    clientContext.ExecuteQuery();
                    if (result != null)
                    {
                        var user = clientContext.Web.EnsureUser(result.Value.LoginName);
                        clientContext.Load(user);
                        clientContext.ExecuteQuery();
                    }

Please tell me how to do this

2

I just have the solution for you:

first of all, if i can understand your list field which will contain multiple users is a column "Person or Group" type isn't it ?

If it's YES then follow me:

1- First you need to get the SharePoint User information using a REST request

/sites/Support/_api/Web/SiteUsers/GetByEmail('Type the email here')

Once you get that, the body request will have the following informations:

  • Metadata
  • Id
  • LoginName
  • Title
  • Email
  • ...etc

2- Filter and get the user Id from the REST request body

3- Keep doing that for all the users you want to have, and append the results (the Ids) to an Array

4- The most important thing you need to understand that for the Person or Group field, if you want to Assign users to this field (let's name it Persons) you need to update the field named PersonsId

YES we did, SharePoint has added Id to the field name

5- By using a REST request:

Method: POST

Uri:

_api/web/lists/getbytitle('Type list name here')/items(the ID of the item or the row in the list)

Headers:

{
  "Accept": "application/json;odata=verbose",
  "Content-Type": "application/json;odata=verbose",
  "IF-MATCH": "*",
  "X-Requestdigest": "xRequestDigest",
  "X-HTTP-Method": "PATCH"
}

Body (JSON):

  {
     '__metadata':{'type':'SP.Data.<List Name>ListItem'},
     'PersonsId': {
                  "__metadata": {
                                "type": "Collection(Edm.Int32)"
                                },
                                "results": [You array here] //for example [19,24]
                  }
  }

6- and voilà, the result (Column of type Person or Group updated using their Id returned by their emails)

enter image description here

I Hope this will help you, If you didn't understand please let me know, I will be happy to help you.

here is my linkedin: Aimen Boulahia .

Good luck.

6
  • 1
    thanks alot for your reply.Actually I want to get all the users Ids in a single SP API call.According to your answer I have to call multiple times – SupunAravinda Jan 6 '20 at 3:59
  • alright, if you want to get all the users, you can quite simply get them all using REST api and the follwing endpoint: – AIMEN BOULAHIA Jan 6 '20 at 10:29
  • /sites/Support/_api/Web/SiteUsers?$Select=Id – AIMEN BOULAHIA Jan 6 '20 at 10:29
  • This site contains many users, I think more than 500.So all the users getting method is hard case – SupunAravinda Jan 6 '20 at 11:12
  • so what type of users you want to get ? – AIMEN BOULAHIA Jan 6 '20 at 11:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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