1

I am creating Sp groups and the Owner gets set to whoever runs the script. I am trying to set the owner to another group or user and it is not working. The SP.Group documentation says it should work as cited in this question: Changing the owner of a group via REST API

The user who asked this question didn't add if it worked for him after all or not.

This is what I am trying:

{
        var url = tenant + "/_api/Web/SiteGroups/GetById('"+group.Id+"')/owner";
        var body = {
           "LoginName":user.LoginName
           
        };
       
        var headers = { 
           
            "X-HTTP-Method":"MERGE",
            "IF-MATCH": "*"
          };

        var options = {
            body:JSON.stringify(body),
            headers:headers
        };

        return this._context.spHttpClient.post(url, SPHttpClient.configurations.v1,options)
        .then((response:SPHttpClientResponse) => { return response.json(); })
        .then((res:any) => {
            if(res && res.Id){
                return res as Person;
            } else {
                return null;
            }
        });
    }

and I get the error:

code: "-1, Microsoft.SharePoint.Client.UnknownError"
message: "Unknown Error"

Is it possible to do it or not? Has anybody been successful trying this? Any help would be appreciated.

Thanks.

0

There is a method called SetUserAsOwner – you can set a user as a group owner.

/_api/web/sitegroups/getByName('GrouName')/SetUserAsOwner(<User ID>)
| improve this answer | |
  • Would this method work for groups too? the ideal is to use a group as an owner – Lore.A Jun 17 at 20:52
  • This will not work to set a group as the owner. This is a known issue and the SharePoint team has declined to fix it. github.com/SharePoint/sp-dev-docs/issues/… – Joshua Cook Jun 17 at 21:54
  • @JoshuaCook - Thanks for your comment :-) I have added your comment and updated the answer for better visibility of the visitors. – GSD - SharePoint Jun 23 at 7:41
0

As you are trying to do it in SPFx webpart, I will recommend you to use SP PnP JS to update the owner of a site group as this library has the endpoint for it.

Syntax:

import { sp } from "@pnp/sp";
import "@pnp/sp/webs";
import "@pnp/sp/site-groups";

// Update the owner with a user id
await sp.web.siteGroups.getById(7).setUserAsOwner(4);

Documentation: Updating the owner of a site group

Note:

You can also get the group by its name, like:

// get the group using the group's name
const groupName = "Classic Team Visitors";
let grp = await sp.web.siteGroups.getByName(groupName)();

For complete code you can check the below link:

Can't add a Owner to a SharePoint group

| improve this answer | |
  • I'll give this one a try and report back. Thanks – Lore.A Jun 17 at 21:11
  • On Dec 13th in that thread you linked (Can't add a Owner to a SharePoint Group) they specifically call out that this will not work for setting a Group as the owner, they blame it on a deficiency in SharePoint, give a workaround using the old 2007 ProcessQuery service end point and call it a day. – Joshua Cook Jun 17 at 21:52
0

Many folks in the community raised this question about this - they tried to update the group owner using the REST API but they have not been succeeded, ended up with error.

Some of the similar threads are still opened (un-answered):

As an alternate you could try with the PnP PowerShell:

SharePoint Online: Change Group Owner using PnP PowerShell

#Config Variables
$SiteURL = "https://crescenttech.sharepoint.com/Sales"
$GroupName="Sales Portal Members"
#Group owner variable: Can be existing group or user account Email
$GroupOwner = "Sales Portal Owners" #or Salaudeen@Crescent.com
  
#Connect PNP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)


#Set Group Owner
Set-PnPGroup -Identity $GroupName -Owner $GroupOwner

PowerShell to Change Group Owner in SharePoint Online:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
#Config Parameters
$SiteURL= "https://crescent.sharepoint.com"
$GroupName="Team Site Members"
$GroupOwnerName="Salaudeen@crescent.com"
#$GroupOwnerName="Team Site Owners"
 
#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
  
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred
    
    #Get the Group owner - Can be an another Group or User Account
    #$GroupOwner = $Ctx.Web.SiteGroups.GetByName($GroupOwnerName)
    $GroupOwner = $Ctx.Web.EnsureUser($GroupOwnerName)
     
    #Get the Group
    $Group = $Ctx.Web.SiteGroups.GetByName($GroupName)
 
    #Set the Group Owner
    $Group.Owner = $GroupOwner
    $Group.Update()
    $Ctx.ExecuteQuery()
 
    Write-host -f Green "Group Owner has been Updated!"
}
Catch {
    write-host -f Red "Error changing Group Owner!" $_.Exception.Message
}

Reference:

SharePoint Online: Change Group Owner using PowerShell

Conclusion - update

  • Based on the research and comments from other experts in the community, it is not possible using the REST API, Microsoft has stopped looking into this.

  • "This will not work to set a group as the owner. This is a known issue and the SharePoint team has declined to fix it" - Joshua Cook

| improve this answer | |
  • How can OP run this PowerShell script from SPFx webpart? – Ganesh Sanap Jun 16 at 11:21
  • Hi - This PowerShell script is not for SPFx - it is for one-time execution if someone wants to update the group owner periodically we can make use of this PowerShell script, again it is not meant for SPFx. :-) – GSD - SharePoint Jun 16 at 13:15
  • Yes, you are right. I am just pointing it out because OP used "SPFx webparts" tag in his question and he/she will not be able to use the PS in SPFx webpart. – Ganesh Sanap Jun 16 at 13:18
  • We already have a similar process that we are trying to replace with the SPFx webpart. We might need to leave it if there is no way to do it through the webpart. Thanks for the suggestion. – Lore.A Jun 17 at 21:09
  • @Lore.A - I have concluded the answer in the "Conclusion - update" section, for better visibility of the upcoming visitors. Could you please upvote (^) and mark as answered, if it has helped you - this will help to the upcoming visitors. – GSD - SharePoint Jun 23 at 7:43
0

This is the work around we did. It sets a Group as the Owner of another group.


 private SetOwner = (groupId,ownerGroupId) : Promise<SPHttpClientResponse> => {
    
        var siteUrl //set this
        var siteId  //set this
        var endpoint = siteUrl + `/_vti_bin/client.svc/ProcessQuery`;
        
        const body = 
            `<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="15.0.0.0" ApplicationName=".NET Library" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009">
                <Actions>
                    <SetProperty Id="1" ObjectPathId="2" Name="Owner">
                        <Parameter ObjectPathId="3" />
                    </SetProperty>
                    <Method Name="Update" Id="4" ObjectPathId="2" />
                </Actions>
                <ObjectPaths>
                    <Identity Id="2" Name="740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:${siteId}:g:${groupId}" />
                    <Identity Id="3" Name="740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:${siteId}:g:${ownerGroupId}" />
                </ObjectPaths>
            </Request>`;

        var headers = {
            "content-type": "text/xml"
        };

        var options = {
            body:body,
            header:headers
        };

        return this._context.spHttpClient.post(endpoint, SPHttpClient.configurations.v1,options)
        .then((response:SPHttpClientResponse) => { return response });
     }

| improve this answer | |

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.