none
Set group owner using REST API RRS feed

  • Question

  • I'm trying to use the REST API to set the owner of a SharePoint group. I can successfully create the group using REST, but I can't change the owner. I've tried the following.

    1 - Specifying a group owner while creating the group

    Endpoint: .../_api/web/sitegroups
    Method: POST
    Headers:
    Content-Type: application/json; odata=verbose
    Body:
    { '__metadata': { 'type': 'SP.Group' }, 'Title': '<title>', 'Description': '<description>', 'Owner': { '__metadata': { 'type': 'SP.Principal' }, 'Title': '<owner_title>'}}

    This returns a 500 error: "The specified name is already in use. Please try again with a new name". In other words, the owner title I specified already exists, which is correct - I'm trying to make an existing principal the owner of the new group.

    2 - Merging a new group owner into an existing group

    Endpoint: .../_api/web/sitegroups(<group_id>)
    Method: POST
    Headers:
    Content-Type: application/json; odata=verbose
    X-HTTP-Method: MERGE
    Body:
    { '__metadata': { 'type': 'SP.Group' }, 'Owner': { '__metadata': { 'type': 'SP.Principal' }, 'Title': '<owner_title>'}}
    

    This returns a 500 error with a Microsoft.SharePoint.SPException: "Exception from HRESULT: 0x80131904". The trace logs don't shed any light on it.

    3 - Using the owner endpoint directly

    Endpoint: .../_api/web/sitegroups(<group_id>)/owner
    Method: POST
    Headers:
    Content-Type: application/json; odata=verbose
    X-HTTP-Method: PUT
    Body:
    { '__metadata': { 'type': 'SP.Principal' }, 'Title': '<owner_title>'}
    
    This throws various 400 errors. If the current owner of the group is a user, I get the error "The required property 'Email' does not exist in the message" - i.e. it wants SP.User properties. If the current owner of the group is another group, I get the error "The required property 'AllowMembersEditMembership' does not exist in the message." - i.e. it wants SP.Group properties.

    I've tried various adaptations of the three approaches above, such as specifying additional SP.Principal properties or passing SP.User or SP.Group objects instead, but with no success. Any ideas?

    Tuesday, January 14, 2014 1:51 PM

Answers

  • As we chatted on email.  There doesn't seem to be any combo that will set the owner.  Only way to do it is to use the CSOM XML method and post to client.svc\ProcessQuery:

    POST http://weburl/_vti_bin/client.svc/ProcessQuery HTTP/1.1
    X-RequestDigest: 0xAE382F0A8F11688BA9BE66739F84443892CE5E5452BA3E2622F6013D9D97EA8A8D9476463EDC503F700EB24F45024150D0DEEB2F40B160CD88BA7C7B4769BECD,15 Jan 2014 00:11:02 -0000
    Content-Type: text/xml

    Host: www.sanspug.org

    Cookie: FedAuth=blah
    Content-Length: 612

    Expect: 100-continue

    Accept-Encoding: gzip, deflate

     

    XML body is:

     

    <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="45" ObjectPathId="32" Name="Owner">

    <Parameter ObjectPathId="33" />

    </SetProperty>

    <Method Name="Update" Id="46" ObjectPathId="32" />

    </Actions>

    <ObjectPaths>

    <Identity Id="32" Name="740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:a1452dcc-8fc4-4631-8ada-97cb204810f1:g:9" />

    <Identity Id="33" Name="740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:a1452dcc-8fc4-4631-8ada-97cb204810f1:g:7" />

    </ObjectPaths></Request>

    The ids are randomly generated and can be anything you want, as long as they correlate (setproperty and method objectpathid is the object you are updating).  The most important part is the “Name” parts. The first part is the typeid of the object (in this case is means SPGroup), the second part as you can see if the site guid id.  The response does in fact return json if you tell it too:


    Chris Givens CEO, Architecting Connected Systems Blog Twitter

    • Marked as answer by JRJ Lee Monday, January 20, 2014 10:21 AM
    Sunday, January 19, 2014 7:38 PM

All replies

  • As we chatted on email.  There doesn't seem to be any combo that will set the owner.  Only way to do it is to use the CSOM XML method and post to client.svc\ProcessQuery:

    POST http://weburl/_vti_bin/client.svc/ProcessQuery HTTP/1.1
    X-RequestDigest: 0xAE382F0A8F11688BA9BE66739F84443892CE5E5452BA3E2622F6013D9D97EA8A8D9476463EDC503F700EB24F45024150D0DEEB2F40B160CD88BA7C7B4769BECD,15 Jan 2014 00:11:02 -0000
    Content-Type: text/xml

    Host: www.sanspug.org

    Cookie: FedAuth=blah
    Content-Length: 612

    Expect: 100-continue

    Accept-Encoding: gzip, deflate

     

    XML body is:

     

    <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="45" ObjectPathId="32" Name="Owner">

    <Parameter ObjectPathId="33" />

    </SetProperty>

    <Method Name="Update" Id="46" ObjectPathId="32" />

    </Actions>

    <ObjectPaths>

    <Identity Id="32" Name="740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:a1452dcc-8fc4-4631-8ada-97cb204810f1:g:9" />

    <Identity Id="33" Name="740c6a0b-85e2-48a0-a494-e0f1759d4aa7:site:a1452dcc-8fc4-4631-8ada-97cb204810f1:g:7" />

    </ObjectPaths></Request>

    The ids are randomly generated and can be anything you want, as long as they correlate (setproperty and method objectpathid is the object you are updating).  The most important part is the “Name” parts. The first part is the typeid of the object (in this case is means SPGroup), the second part as you can see if the site guid id.  The response does in fact return json if you tell it too:


    Chris Givens CEO, Architecting Connected Systems Blog Twitter

    • Marked as answer by JRJ Lee Monday, January 20, 2014 10:21 AM
    Sunday, January 19, 2014 7:38 PM
  • Thanks Chris. The CSOM XML approach works well.

    Monday, January 20, 2014 10:25 AM
  • I bumped into the same error (Exception from HRESULT: 0x80131904) while trying to set the associated visitors group of a subsite. I was sending the HTTP PUT-method to the _api/Web/AssociatedVisitorGroup endpoint. Is this possible via this endpoint or am I on the wrong track to get this working?

    Tuesday, January 21, 2014 6:33 PM
  • Yeah, I had the same problem with AssociatedVisitorGroup etc - I'm now using the CSOM XML approach for those as well. You'd need to:

    Send a POST request to <site URL>/_vti_bin/client.svc/ProcessQuery

    Include a Content-Type: text/xml header (no other headers required if you're sending from a workflow, otherwise add an X-RequestDigest header)

    Set the body to the following:

    <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="AssociatedVisitorGroup">
          <Parameter ObjectPathId="3" />
        </SetProperty>
        <Method Name="Update" Id="4" ObjectPathId="2" />
      </Actions>
      <ObjectPaths>
        <Identity Id="2" Name="{0}:site:{1}:web:{2}" />
        <Identity Id="3" Name="{0}:site:{1}:g:{3}" />
      </ObjectPaths>
    </Request>

    Replace the placeholders as follows:

    {0} is the root of all object paths, always 740c6a0b-85e2-48a0-a494-e0f1759d4aa7

    {1} is the ID (GUID) of the current SPSite

    {2} is the ID (GUID) of the SPWeb that you want to associate the group with

    {3} is the integer ID (i.e. the principal ID) of the SharePoint group

    Replace the text AssociatedVisitorGroup with AssociatedOwnerGroup or AssociatedMemberGroup if you want to set those properties - no other changes required.


    Tuesday, January 21, 2014 8:37 PM
  • Thanks a lot!! I got it working via the Chrome rest client. Do you also know how to do this in a SPD workflow? Apparently you can only pass a dictionary as request body. When I define the XML in a string variable, build a dictionary from that variable (as Dictionary from Json) and pass that dictionary as request, I get an error during workflow execution:

    Details: An unhandled exception occurred during the execution of the workflow instance. Exception details: System.FormatException: The input source is not correctly formatted. ---> System.Xml.XmlException: Encountered unexpected character '<'. at System.Xml.XmlExceptionHelper.ThrowXmlException(XmlDictionaryReader reader, XmlException exception) at System.Runtime.Serialization.Json.XmlJsonReader.ReadAttributes() at System.Runtime.Serialization.Json.XmlJsonReader.Read() at Microsoft.Workflow.Common.Json.JXmlToJsonValueConverter.MoveToRootNode(XmlDictionaryReader jsonReader) at Microsoft.Workflow.Common.Json.JXmlToJsonValueConverter.JXMLToJsonValue(XmlDictionaryReader jsonReader) --- End of inner exception stack trace --- at Microsoft.Workflow.Common.Json.JXmlToJsonValueConverter.JXMLToJsonValue(XmlDictionaryReader jsonReader) at Microsoft.Workflow.Common.Json.JXmlToJsonValueConverter.JXMLToJsonValue(Stream jsonStream, Byte[] jsonBytes) at Microsoft.Activities.DynamicValue.ParseJson(String json) at System.Activities.CodeActivity`1.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager) at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation) Exception from activity ParseDynamicValue Assign Set Workspace Permissions Sequence Microsoft.SharePoint.WorkflowServices.Activities.AppOnlySequence

    Wednesday, January 22, 2014 11:14 AM
  • That does seem to be a limitation in SPD - the Call HTTP Web Service activity is essentially limiting you to JSON requests by forcing you to use a Dictionary type. I did it by building a custom workflow activity in Visual Studio - the HttpSend activity in VS allows you to provide a string as the request body.
    Wednesday, January 22, 2014 11:28 AM
  • Thanks for suggesting the CSOM XML workaraound.

    What would be the equivalent of setting a SPUser as the group's owner?

    I also noticed that if I set a User as Group Owner using the People and Groups interface, then I get an error 500 Object reference not set to an instance of an object. when I hit the REST API url:

    /_api/Web/SiteGroups/GetById(8)/Owner

    However, when I set another Group as the owner, the Owner object seems to be properly set using the same URL above (status code 200 with data of the Group)

    Regardless, the OwnerTitle property seems to be set correctly in both cases.

    Is this expected?

    Thanks!

    Tuesday, May 6, 2014 8:21 AM
  • Hi Chowky

    In the CSOM XML approach, setting an SPUser as the group owner requires a slightly different XML structure. I wrote up the details of both approaches in this post.

    As for the Owner endpoint... I think we concluded it was a buggy implementation :-)

    Tuesday, May 6, 2014 8:47 AM
  • Jason,

    Great write up! Very useful stuff.

    Thanks for clearing up the 2nd point as well. I thought something messed up on my end somehow.

    Thank you!

    Tuesday, May 6, 2014 8:54 AM
  • This REST API bug was put on here about a year and a half ago, and I have had the same problem. Can it be fixed in the coming SharePoint updates??
    • Edited by mcantin Tuesday, August 11, 2015 1:52 PM
    Tuesday, August 11, 2015 1:52 PM
  • Hi @Chris

    do you know how to use SharePoint to Build the Request Body? what i build doesn't work.

    Thanks a lot.

    Wednesday, March 15, 2017 9:51 AM
  • Hi JRJ

    do you know how to build the request xml body in sharepoint designer? what i build does't work.

    Thanks a lot 

    Wednesday, March 15, 2017 9:53 AM
  • I keep referring back to this page for a response.  I've not seen any updates.  Has / will this be resolved in future updates?  Or, has anyone figured out how to achieve this in a SharePoint Designer 2013 Workflow (without developing a custom activity).  I am working in an environment that does not permit server side code deployment.  Any assistance would be greatly appreciated.
    Thursday, February 1, 2018 5:44 PM