Please specify what version of the library you are using: [ 1.3.5 ]
Please specify what version(s) of SharePoint you are targeting: [ Online ]
Creating a group and choosing a owner should put that User/Group has the group Owner
Updating the owner does not change the owner.
I don't find any documentation. Could I be missing something?
The group owner is the user that created the SharePoint group
Create a app that creates a group.
import { sp, Web, PrincipalSource, PrincipalType, PrincipalInfo } from "@pnp/sp";
sp.utility.resolvePrincipal(managersGroupName,
PrincipalType.SharePointGroup,
PrincipalSource.UserInfoList,
false,
false)
.then((managerGroup: PrincipalInfo) => {
web.siteGroups.add({
Title: groupName,
Description: groupName,
AllowRequestToJoinLeave: false,
AutoAcceptRequestToJoinLeave: false,
AllowMembersEditMembership: false,
OnlyAllowMembersViewMembership: false,
Owner: {
LoginName: managerGroup.LoginName,
Id: managerGroup.PrincipalId,
PrincipalType: managerGroup.PrincipalType
}
}).then((group:GroupAddResult )=>{
web.siteGroups.getById(group.data.Id).update({
Owner: {
LoginName: managerGroup.LoginName,
Id: managerGroup.PrincipalId,
PrincipalType: managerGroup.PrincipalType
}
});
});
});
I think it's "REST API" thing. Also didn't find a way of setting group owner via REST, have tried _api/Web/SiteGroups/GetById(7)/Owner and posting SP.Principal as well. Maybe I'm also missing something.
I'm also inclined to your conclusion.
But since there is a reply from the server if the parameters for the owner are incorrect it should be doing something.
If I use a invalid parameters like Department, I receive something like "SP.Principal does not have a parameter named Department". So it receive a SP.Principal.
In the above code there is no error. But the owner is not set correctly.
It works for me when I use the following method:
https://s-kainet.github.io/sp-rest-explorer/#/entity/SP.Group/func/SetUserAsOwner
import { SPHttpClient } from '@pnp/sp';
(async () => {
const siteObj = await sp.web.select('Url').get();
const endpoint = siteObj.Url + `/_api/web/sitegroups/getByName('Communication%20site%20Members')/SetUserAsOwner(10)`;
const client = new SPHttpClient();
const response = await client.post(endpoint, {
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
})
const result = await response.json();
})()
@KEMiCZA thanks! That's cool. I didn't know about this method existence. We can definitely add its support to PnPjs.
Compatibility issue we have is that this method does not exist on SharePoint 2019 onpremise (and I can assume it's not available on 2013 & 2016 as well?). Passing the owner with the regular update method doesn't work on SP onpremise either (tested on 2019).
Yep, 2019 was a fork of SPO. So this method while not presented in 2019 wasn't obviously in older versions.
Our main target is SPO - stuff will work or not in other versions but we don't exclude things. We can probably add a setOwner method to group or something similar?
Hi @KEMiCZA,
It's not working for groups. Only for users. Is there another method for groups?
Request URL: https://xxx.sharepoint.com/sites/Maintenance/_api/web/sitegroups
/getByName('Energy%20Management%20-%20Teste')/SetUserAsOwner(60)
Request Method: POST
Status Code: 200
Remote Address: 123.123.123.123:443
Referrer Policy: no-referrer-when-downgrade
Response:
{"odata.metadata":"https://xxx.sharepoint.com/sites/Maintenance/_api/$metadata#Edm.Null","odata.null":true}
But in the end the group owner stays the same like in the create/update group pnp entry.
Edited:
By the way. I found this github project/site that shows all the SharePoint rest api metadata.
It is updated automatically by a timer job. It might not be complete but it has lot of information including the SetUserAsOwner.
https://s-kainet.github.io/sp-rest-explorer
I think we should consider this a bug since we insert a value on a available option that is not saved in to the system.
@Dangerous-Mind You're right, SetUserAsOwner only works for a user id as argument.
Clearly the update method on the group must be an implementation issue/bug in the product (SharePoint) itself. I've tried all kinds of tricks to make the owner update through the normal REST endpoints, but without success.
We can register this issue as a bug and hopefully somebody at Microsoft can fix the original update method or add a new SetGroupAsOwner, with a group id as parameter, method.
One last thing we can try is to create a wrapper with the _/_vti_bin/client.svc/ProcessQuery_ method. Not sure if this is something you want in PnPjs @patrick-rodgers ? More info here & here
Made a soap request to change the owner as a group.
Only for Group as Group Owners:
private async setGroupOwner(groupId: number, ownerGroupId: number) {
const client = new SPHttpClient();
await Promise.all([sp.web.select("Url").get(), sp.site.select("Id").get()])
.then(async (siteData) => {
const siteObj = siteData[0];
const siteId = siteData[1];
const endpoint = siteObj.Url + `/_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.Id}:g:${groupId}" />
<Identity Id="3" Name="740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:${siteId.Id}:g:${ownerGroupId}" />
</ObjectPaths>
</Request>`;
await client.post(endpoint, {
headers: {
"content-type": "text/xml"
},
body: body
}).then((response) => {
const result = response.json();
console.log(result);
});
});
}
@koltyakov
If the feature add group has a option to set the owner and it doesn't do anything it should be a bug.
Can you please review the classification and check if I'm thinking things right? Thanks :)
It's also a new feature because of the new @KEMiCZA method.
@Dangerous-Mind, thanks for the workaround sample, it can be definitely helpful for folks looking for the same action, especially for those dealing with legacy SharePoint.
It's still not a bug, as there is nothing wrong in PnPjs, but an issue on SharePoint API side, which we can't fix.
Ok, Got it.
Thanks @koltyakov
Most helpful comment
It works for me when I use the following method:
https://s-kainet.github.io/sp-rest-explorer/#/entity/SP.Group/func/SetUserAsOwner