Monday, August 27, 2018

How to resolve error "The user does not exist or is not unique." when use Set-SPOUser for SharePoint online?


In order to support daily SharePoint online activities, there is a need to add a group to all SPO site as site collection admin. Before our company come up a site provisioning process that will add the group to each site collection, there are some sites created from SharePoint UI and Groups that do not have that specific group as site collection admin. When we use PowerShell to add that group, we got the following error.

“Set-SPOUser : The user does not exist or is not unique.”

We have same issues in the self-provisioning Azure function PowerShell to add some users to SPO groups like owner group. After debugging the issues, we identified that is related to SharePoint online how to deal with the groups. The group will not have email and here is the way to add them to site collection admin or add into SharePoint group.

You need to find the SID of the group like "c:0t.c|tenant|????????-????-????-????-????????????" from SPO UI. You can first check the permission for that group in SPO and you will find the SID for the group as we discussed in previous blog. Then you can add as site collection admin.

$username = "spoadmin.onmicrosoft.com"
$password = "password"

$secpasswd = ConvertTo-SecureString $password -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ($username, $secpasswd)

Connect-SPOService -Url "https://mycompany-admin.sharepoint.com/" -Credential $creds


$siteUrl = "https://mycompany.sharepoint.com/teams/departmentsite"
$SiteCollectionAdmin = "c:0t.c|tenant|????????-????-????-????-????????????"
Set-SPOUser -site $siteUrl -LoginName $SiteCollectionAdmin -IsSiteCollectionAdmin $True

You can use the group LookupValue value if you need to add to groups. Here is PnP PowerShell example.

Connect-PnPOnline -url $siteUrl -Credentials $creds 

$owner = “group1@mycompany.com”
if($owner.Email -ne $null -and $owner.Email -ne "")
{
       Add-PnPUserToGroup -LoginName $owner.Email -Identity 3
}
else
{
       Add-PnPUserToGroup -LoginName $owner.LookupValue -Identity 3
}

Now you should be able to deal the groups inside SharePoint online using PowerShell.

No comments:

Post a Comment