spo user add [options]
Adds user to a specific web
| Option | Description |
| ----------------------- | ----------------------------------------- |
| -u, --webUrl <webUrl> | Url of the web to list the users within|
| --email <email> |Email of the user to be added|
| --group <group> |The SharePoint group name to which the user to be added|
| -o, --output [output] | Output type. json,text. Default text |
| --verbose | Runs command with verbose logging |
| --debug | Runs command with debug logging |
Adds user with email john.[email protected] to the Viewers group of web _https://contoso.sharepoint.com/sites/mysite_
spo user add --webUrl "https://contoso.sharepoint.com/sites/mysite" --email "[email protected]" --group "Team Site Members"
Unfortunately, there isn't a REST API for adding users. The SOAP API can be used to achieve this in two calls.
The first call would get the SharePoint group id and the http call looks like this.
POST /_vti_bin/client.svc/ProcessQuery
BDOY
`<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="${config.applicationName}" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009"><Actions><ObjectPath Id="16" ObjectPathId="15" /><ObjectIdentityQuery Id="17" ObjectPathId="15" /><Query Id="18" ObjectPathId="15"><Query SelectAllProperties="true"><Properties /></Query></Query><ObjectPath Id="20" ObjectPathId="19" /><Query Id="21" ObjectPathId="19"><Query SelectAllProperties="true"><Properties /></Query><ChildItemQuery SelectAllProperties="true"><Properties /></ChildItemQuery></Query></Actions><ObjectPaths><Method Id="15" ParentId="5" Name="GetByName"><Parameters><Parameter Type="String">Team Site Members</Parameter></Parameters></Method><Property Id="19" ParentId="15" Name="Users" /><Property Id="5" ParentId="3" Name="SiteGroups" /><Property Id="3" ParentId="1" Name="Web" /><StaticProperty Id="1" TypeId="{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}" Name="Current" /></ObjectPaths></Request>`
where Team Site Members value in the XML has to be replaced by the --group option value specified by the user of the CLI.
The second call would use the group id returned by the first call to add the user to the group
POST /_vti_bin/client.svc/ProcessQuery
BDOY
`<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="${config.applicationName}" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009"><Actions><ObjectPath Id="28" ObjectPathId="27" /><ObjectIdentityQuery Id="29" ObjectPathId="27" /></Actions><ObjectPaths><Method Id="27" ParentId="19" Name="AddUser"><Parameters><Parameter ObjectPathId="25" /></Parameters></Method><Property Id="19" ParentId="15" Name="Users" /><Method Id="25" ParentId="3" Name="EnsureUser"><Parameters><Parameter Type="String">[email protected]</Parameter></Parameters></Method><Identity Id="15" Name="65df629f-d0f6-2000-295f-0aa52be8fdc4|740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:ed45f182-74d9-4b2a-abf7-c39b4a2f0a29:g:7" /><Property Id="3" ParentId="1" Name="Web" /><StaticProperty Id="1" TypeId="{3747adcd-a3c3-41b9-bfab-4a64dd2f1e0a}" Name="Current" /></ObjectPaths></Request>`
where [email protected] is the --email option and the 65df629f-d0f6-2000-295f-0aa52be8fdc4|740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:ed45f182-74d9-4b2a-abf7-c39b4a2f0a29:g:7 value is the group id from the first request.
Add-SPOUser -
https://docs.microsoft.com/en-us/powershell/module/sharepoint-online/add-spouser?view=sharepoint-ps
Add-PnPUserToGroup -
https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/add-pnpusertogroup?view=sharepoint-ps
I raised a pull request for the #1691.
I would like to do work with this command.
Can you please assign to me?
All yours @dips365! Appreciate your help 馃憤
@VelinGeorgiev @waldekmastykarz
Hey,
I am working on this command.
I would like to put my view on add user using the SharePoint Rest API instead of SOAP request.
I worked with SharePoint Rest API to add user in group in power automate.
$.ajax
({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/GetById(" + GroupId + ")/users",
type: "POST",
data: JSON.stringify({
'__metadata': {
'type': 'SP.User'
},
'LoginName': '<<User Login Name>>'
}),
headers:
{
"Accept": "application/json;odata=verbose",
"Content-Type": "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val()
},
success: function (data, status, xhr) {
console.log('Success');
},
error: function (xhr, status, error) {
console.log("Failed");
}
});
I believe that SharePoint Rest API should be work if Office 365 CLI Command.
Should i go with Rest API rather than SOAP request?
Thought ?
Thanks,
Dipen Shah
REST is definitely the preferred approach, so if you can implement the desired command functionality with REST, then go for it 馃槉
Please go with the rest as Garry mentioned. I did not know that REST existed for this. Just bear in mind email is not loginName. You will have to turn email input value to a loginName value.
Thanks @garrytrinder & @VelinGeorgiev
Will keep in mind regarding conversion of login name into email address.
Hey @dips365 just checking in, are you still working on this command?
@garrytrinder Yes. Working with test cases.
Logic is developed.
@garrytrinder I stuck to write up test cases for this command.
Can you guide me how to write test cases for command which includes multiple request ?
In this case, I send request to get group id and pass it to another request to add user to group by Id which we get from last request.
Hey @dips365 if your command logic is complete and you are just stuck on completing the tests, you can create a draft pull request, that will let us review your code and help guide you in how to complete the required tests.
Thank you so much @garrytrinder . I will do that
@garrytrinder Created draft pull request.
Most helpful comment
@garrytrinder Yes. Working with test cases.
Logic is developed.