Add multiple users to a SharePoint Group using single API Call

Published Aug 18 2021 12:10 AM 1,754 Views
New Contributor

Adding a user to a SharePoint group from custom solutions via REST API in SharePoint Framework (SPFx) solutions or Power Automate or any other custom solutions is a very common scenario. In those scenarios, we rely on the site groups api _api/web/sitegroups({GROUPID}) and add the LoginName in the Body. This will be a perfect solution if you just need to add only 1 user to the group.

Imagine scenarios where you have more than 1 user to be added to the group. Unfortunately, the above API does not support adding more than 1 user in the Payload. That being the case, for achieving the same result, you may have to call the API in a recursive fashion which is not a very efficient way of execution if you have quite a large number of users to be added and repeated API calls would not be an optimal solution when you design your application.

 

Alternate API endpoint to the rescue

Don’t worry, there is another endpoint that you can rely upon which will achieve the same result which is /_api/SP.Web.ShareObject. This is the same endpoint that is used when you add users to the Owners / Members / Visitors from the user interface of the Modern SharePoint site. This endpoint is used primarily for sharing the Site, Folder, Document, etc. This is the same endpoint that is used when you want to share with external users as well. It also has a property which is roleValue through which you can specify Id of the SharePoint Group. So the entire API call may look something like below.

 

POST :

https://TenantName.sharepoint.com/SiteName/_api/SP.Web.ShareObject

BODY :

{
    "url": "https://tenantname.sharepoint.com/sites/SiteName",
    "peoplePickerInput": "[{\"Key\":\"user1@tenantname.onmicrosoft.com\"},{\"Key\":\"user2@tenantname.onmicrosoft.com\"}]",
    "roleValue": "group:32"
}

 

Notice. This REST API is not officially documented by Microsoft, so it's not officially supported to be used externally by partners and customers. This means that the signature can be changed without further notice.

 

Details

As you can see the major property which is relevant for us is peoplePickerInput and roleValue.

 

peoplePickerInput : Used to give the list of Usernames that needs to be added the SharePoint Group. Its value is a JSON array of Key Value Pair ({\"Key\":\"user1@tenantname.onmicrosoft.com\"}) where the Username is given the Value.

 

roleValue : We will give the Id of the SharePoint Group to this value in the format group:{GROUPID}. This will be the Id of the SharePoint group to which the above-mentioned users will be added.

Since this API has a feature to share the site or document with external users also, it has many properties which will be relevant in those scenarios. But for us, since we are just adding the user to the SharePoint group, all we need is the above list of objects in the JSON body.

 

Caveats

With Power comes Responsility Caveats (Some)

Since we are using the API which is used for Sharing the Site, the API even supports adding the permission to the users albeit users are not part of your Active Directory. That being the case when you try to add the users who are not part of your AD, there will be 2 outcomes.

Sharing Options is “Anyone”

When you have external sharing enabled and are trying to add a user who is not part of your site there wouldn’t be any error thrown by the API, but instead, a Sharing Link will be generated. So if you want to ensure that there are no inadvertent sharing links, ensure that you are checking the presence of the user before you execute the endpoint. Otherwise, over time there will be a large number of sharing links cluttering in your web

In the other cases of sharing, when the user does not exist, you will get an error that will be thrown by the endpoint.

 

CLI for Microsoft 365

If you need to automate the same scenario or execute via scripts, you can use a command from CLI for Microsoft 365 which is m365 spo group user add. This command will allow you to add multiple users to SharePoint group. If you have an automation or provisioning solution which has this scenario, it is going to be super helpful.

 

Photo by Alex Suprun on Unsplash

%3CLINGO-SUB%20id%3D%22lingo-sub-2645034%22%20slang%3D%22en-US%22%3EAdd%20multiple%20users%20to%20a%20SharePoint%20Group%20using%20single%20API%20Call%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2645034%22%20slang%3D%22en-US%22%3E%3CP%3EAdding%20a%20user%20to%20a%20SharePoint%20group%20from%20custom%20solutions%20via%20REST%20API%20in%20SharePoint%20Framework%20(SPFx)%20solutions%20or%20Power%20Automate%20or%20any%20other%20custom%20solutions%20is%20a%20very%20common%20scenario.%20In%20those%20scenarios%2C%20we%20rely%20on%20the%20site%20groups%20api%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22language-plaintext%20highlighter-rouge%22%3E_api%2Fweb%2Fsitegroups(%7BGROUPID%7D)%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%20add%20the%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22language-plaintext%20highlighter-rouge%22%3ELoginName%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ein%20the%20Body.%20This%20will%20be%20a%20perfect%20solution%20if%20you%20just%20need%20to%20add%20only%201%20user%20to%20the%20group.%3C%2FP%3E%0A%3CP%3EImagine%20scenarios%20where%20you%20have%20more%20than%201%20user%20to%20be%20added%20to%20the%20group.%20Unfortunately%2C%20the%20above%20API%20does%20not%20support%20adding%20more%20than%201%20user%20in%20the%20Payload.%20That%20being%20the%20case%2C%20for%20achieving%20the%20same%20result%2C%20you%20may%20have%20to%20call%20the%20API%20in%20a%20recursive%20fashion%20which%20is%20not%20a%20very%20efficient%20way%20of%20execution%20if%20you%20have%20quite%20a%20large%20number%20of%20users%20to%20be%20added%20and%20repeated%20API%20calls%20would%20not%20be%20an%20optimal%20solution%20when%20you%20design%20your%20application.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22alternate-api-endpoint-to-the-rescue%22%20id%3D%22toc-hId--381973812%22%20id%3D%22toc-hId--380929983%22%3EAlternate%20API%20endpoint%20to%20the%20rescue%3C%2FH2%3E%0A%3CP%3EDon%E2%80%99t%20worry%2C%20there%20is%20another%20endpoint%20that%20you%20can%20rely%20upon%20which%20will%20achieve%20the%20same%20result%20which%20is%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.sharepoint.client.web.shareobject%3Fview%3Dsharepoint-csom%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%3CCODE%20class%3D%22language-plaintext%20highlighter-rouge%22%3E%2F_api%2FSP.Web.ShareObject%3C%2FCODE%3E%3C%2FA%3E.%20This%20is%20the%20same%20endpoint%20that%20is%20used%20when%20you%20add%20users%20to%20the%20Owners%20%2F%20Members%20%2F%20Visitors%20from%20the%20user%20interface%20of%20the%20Modern%20SharePoint%20site.%20This%20endpoint%20is%20used%20primarily%20for%20sharing%20the%20Site%2C%20Folder%2C%20Document%2C%20etc.%20This%20is%20the%20same%20endpoint%20that%20is%20used%20when%20you%20want%20to%20share%20with%20external%20users%20as%20well.%20It%20also%20has%20a%20property%20which%20is%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22language-plaintext%20highlighter-rouge%22%3EroleValue%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ethrough%20which%20you%20can%20specify%20Id%20of%20the%20SharePoint%20Group.%20So%20the%20entire%20API%20call%20may%20look%20something%20like%20below.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EPOST%20%3A%3C%2FP%3E%0A%3CDIV%20class%3D%22language-html%20highlighter-rouge%22%3E%0A%3CDIV%20class%3D%22highlight%22%3E%0A%3CPRE%20class%3D%22highlight%22%3E%3CCODE%3Ehttps%3A%2F%2FTenantName.sharepoint.com%2FSiteName%2F_api%2FSP.Web.ShareObject%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3EBODY%20%3A%3C%2FP%3E%0A%3CDIV%20class%3D%22language-json%20highlighter-rouge%22%3E%0A%3CDIV%20class%3D%22highlight%22%3E%0A%3CPRE%20class%3D%22highlight%22%3E%3CCODE%3E%3CSPAN%20class%3D%22p%22%3E%7B%3C%2FSPAN%3E%0A%20%20%20%20%3CSPAN%20class%3D%22nl%22%3E%22url%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E%3A%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22s2%22%3E%22https%3A%2F%2Ftenantname.sharepoint.com%2Fsites%2FSiteName%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E%2C%3C%2FSPAN%3E%0A%20%20%20%20%3CSPAN%20class%3D%22nl%22%3E%22peoplePickerInput%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E%3A%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22s2%22%3E%22%5B%7B%3C%2FSPAN%3E%3CSPAN%20class%3D%22se%22%3E%5C%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22s2%22%3EKey%3C%2FSPAN%3E%3CSPAN%20class%3D%22se%22%3E%5C%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22s2%22%3E%3A%3C%2FSPAN%3E%3CSPAN%20class%3D%22se%22%3E%5C%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22s2%22%3Euser1%40tenantname.onmicrosoft.com%3C%2FSPAN%3E%3CSPAN%20class%3D%22se%22%3E%5C%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22s2%22%3E%7D%2C%7B%3C%2FSPAN%3E%3CSPAN%20class%3D%22se%22%3E%5C%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22s2%22%3EKey%3C%2FSPAN%3E%3CSPAN%20class%3D%22se%22%3E%5C%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22s2%22%3E%3A%3C%2FSPAN%3E%3CSPAN%20class%3D%22se%22%3E%5C%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22s2%22%3Euser2%40tenantname.onmicrosoft.com%3C%2FSPAN%3E%3CSPAN%20class%3D%22se%22%3E%5C%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22s2%22%3E%7D%5D%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E%2C%3C%2FSPAN%3E%0A%20%20%20%20%3CSPAN%20class%3D%22nl%22%3E%22roleValue%22%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E%3A%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22s2%22%3E%22group%3A32%22%3C%2FSPAN%3E%0A%3CSPAN%20class%3D%22p%22%3E%7D%3C%2FSPAN%3E%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CFONT%20color%3D%22%23FF6600%22%3E%3CSTRONG%3ENotice.%3C%2FSTRONG%3E%3C%2FFONT%3E%20This%20REST%20API%20is%20not%20officially%20documented%20by%20Microsoft%2C%20so%20it's%20not%20officially%20supported%20to%20be%20used%20externally%20by%20partners%20and%20customers.%20This%20means%20that%20the%20signature%20can%20be%20changed%20without%20further%20notice.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22details%22%20id%3D%22toc-hId-308587662%22%20id%3D%22toc-hId-309631491%22%3EDetails%3C%2FH3%3E%0A%3CP%3EAs%20you%20can%20see%20the%20major%20property%20which%20is%20relevant%20for%20us%20is%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22language-plaintext%20highlighter-rouge%22%3EpeoplePickerInput%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Eand%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22language-plaintext%20highlighter-rouge%22%3EroleValue%3C%2FCODE%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CCODE%20class%3D%22language-plaintext%20highlighter-rouge%22%3EpeoplePickerInput%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3A%20Used%20to%20give%20the%20list%20of%20Usernames%20that%20needs%20to%20be%20added%20the%20SharePoint%20Group.%20Its%20value%20is%20a%20JSON%20array%20of%20Key%20Value%20Pair%20(%3CCODE%20class%3D%22language-plaintext%20highlighter-rouge%22%3E%7B%5C%22Key%5C%22%3A%5C%22user1%40tenantname.onmicrosoft.com%5C%22%7D%3C%2FCODE%3E)%20where%20the%20Username%20is%20given%20the%20Value.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CCODE%20class%3D%22language-plaintext%20highlighter-rouge%22%3EroleValue%3C%2FCODE%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3A%20We%20will%20give%20the%20Id%20of%20the%20SharePoint%20Group%20to%20this%20value%20in%20the%20format%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22language-plaintext%20highlighter-rouge%22%3Egroup%3A%7BGROUPID%7D%3C%2FCODE%3E.%20This%20will%20be%20the%20Id%20of%20the%20SharePoint%20group%20to%20which%20the%20above-mentioned%20users%20will%20be%20added.%3C%2FP%3E%0A%3CP%3ESince%20this%20API%20has%20a%20feature%20to%20share%20the%20site%20or%20document%20with%20external%20users%20also%2C%20it%20has%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fdotnet%2Fapi%2Fmicrosoft.sharepoint.client.web.shareobject%3Fview%3Dsharepoint-csom%23parameters%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Emany%20properties%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ewhich%20will%20be%20relevant%20in%20those%20scenarios.%20But%20for%20us%2C%20since%20we%20are%20just%20adding%20the%20user%20to%20the%20SharePoint%20group%2C%20all%20we%20need%20is%20the%20above%20list%20of%20objects%20in%20the%20JSON%20body.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH3%20id%3D%22caveats%22%20id%3D%22toc-hId--1498866801%22%20id%3D%22toc-hId--1497822972%22%3ECaveats%3C%2FH3%3E%0A%3CBLOCKQUOTE%3E%0A%3CP%3EWith%20Power%20comes%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CDEL%3EResponsility%3C%2FDEL%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3ECaveats%20(Some)%3C%2FP%3E%0A%3C%2FBLOCKQUOTE%3E%0A%3CP%3ESince%20we%20are%20using%20the%20API%20which%20is%20used%20for%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fsupport.microsoft.com%2Fen-us%2Foffice%2Fshare-a-site-958771a8-d041-4eb8-b51c-afea2eae3658%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ESharing%20the%20Site%3C%2FA%3E%2C%20the%20API%20even%20supports%20adding%20the%20permission%20to%20the%20users%20albeit%20users%20are%20not%20part%20of%20your%20Active%20Directory.%20That%20being%20the%20case%20when%20you%20try%20to%20add%20the%20users%20who%20are%20not%20part%20of%20your%20AD%2C%20there%20will%20be%202%20outcomes.%3C%2FP%3E%0A%3CH3%20id%3D%22sharing-options-is-anyone%22%20id%3D%22toc-hId-988646032%22%20id%3D%22toc-hId-989689861%22%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fsharepoint%2Fchange-external-sharing-site%23which-option-to-select%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ESharing%20Options%20is%20%E2%80%9CAnyone%E2%80%9D%3C%2FA%3E%3C%2FH3%3E%0A%3CP%3EWhen%20you%20have%20external%20sharing%20enabled%20and%20are%20trying%20to%20add%20a%20user%20who%20is%20not%20part%20of%20your%20site%20there%20wouldn%E2%80%99t%20be%20any%20error%20thrown%20by%20the%20API%2C%20but%20instead%2C%20a%20Sharing%20Link%20will%20be%20generated.%20So%20if%20you%20want%20to%20ensure%20that%20there%20are%20no%20inadvertent%20sharing%20links%2C%20ensure%20that%20you%20are%20checking%20the%20presence%20of%20the%20user%20before%20you%20execute%20the%20endpoint.%20Otherwise%2C%20over%20time%20there%20will%20be%20a%20large%20number%20of%20sharing%20links%20cluttering%20in%20your%20web%3C%2FP%3E%0A%3CP%3EIn%20the%20other%20cases%20of%20sharing%2C%20when%20the%20user%20does%20not%20exist%2C%20you%20will%20get%20an%20error%20that%20will%20be%20thrown%20by%20the%20endpoint.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22cli-for-microsoft-365%22%20id%3D%22toc-hId-978142928%22%20id%3D%22toc-hId-979186757%22%3ECLI%20for%20Microsoft%20365%3C%2FH2%3E%0A%3CP%3EIf%20you%20need%20to%20automate%20the%20same%20scenario%20or%20execute%20via%20scripts%2C%20you%20can%20use%20a%20command%20from%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fcli-microsoft365%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3ECLI%20for%20Microsoft%20365%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ewhich%20is%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CCODE%20class%3D%22language-plaintext%20highlighter-rouge%22%3Em365%20spo%20group%20user%20add%3C%2FCODE%3E.%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fcli-microsoft365%2Fcmd%2Fspo%2Fgroup%2Fgroup-user-add%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EThis%20command%3C%2FA%3E%3CSPAN%3E%26nbsp%3B%3C%2FSPAN%3Ewill%20allow%20you%20to%20add%20multiple%20users%20to%20SharePoint%20group.%20If%20you%20have%20an%20automation%20or%20provisioning%20solution%20which%20has%20this%20scenario%2C%20it%20is%20going%20to%20be%20super%20helpful.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EPhoto%20by%26nbsp%3B%3C%2FSPAN%3E%3CA%20href%3D%22https%3A%2F%2Funsplash.com%2F%40sooprun%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3EAlex%20Suprun%3C%2FA%3E%3CSPAN%3E%26nbsp%3Bon%20Unsplash%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2645034%22%20slang%3D%22en-US%22%3E%3CP%3EThis%20is%20a%20blog%20post%20which%20will%20explain%20methods%20to%20add%20multiple%20users%20to%20a%20SharePoint%20group%20using%20single%20API%20Call%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2645034%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EHow%20to%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Co-Authors
Version history
Last update:
‎Aug 24 2021 01:48 AM
Updated by: