REST API - Retrieve Group Members
-
07-02-2021 - |
Question
We are using SharePoint Online. I am planning to add a flow to send out an email notification whenever a new page is added to the site.
To do that, I need the list of members of that site cause I don't want to send it to everyone or anyone.
I am unable to retrieve the group members through REST API. When I fire the following REST call, I am getting the response as follows, but not the actual members and their email addresses
https://dhdigitalcoin.sharepoint.com/sites/OpenSourceTechTalk/_api/web/sitegroups/getbyname('Open%20Source%20Tech%20Talk%20Members')/users
<entry>
<id>https://dhdigitalcoin.sharepoint.com/sites/OpenSourceTechTalk/_api/Web/GetUserById(7)</id>
<category term="SP.User" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
<link rel="edit" href="Web/GetUserById(7)"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Alerts" type="application/atom+xml;type=feed" title="Alerts" href="Web/GetUserById(7)/Alerts"/>
<link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Groups" type="application/atom+xml;type=feed" title="Groups" href="Web/GetUserById(7)/Groups"/>
<title/>
<updated>2019-06-03T12:47:56Z</updated>
<author>
<name/>
</author>
<content type="application/xml">
<m:properties>
<d:Id m:type="Edm.Int32">7</d:Id>
<d:IsHiddenInUI m:type="Edm.Boolean">false</d:IsHiddenInUI>
<d:LoginName>c:0o.c|federateddirectoryclaimprovider|92b9f118-885e-4c23-a103-de7ce46d7421</d:LoginName>
<d:Title>Open Source Tech Talk Members</d:Title>
<d:PrincipalType m:type="Edm.Int32">4</d:PrincipalType>
<d:Email>OpenSourceTechTalk@xxx.onmicrosoft.com</d:Email>
<d:Expiration/>
<d:IsEmailAuthenticationGuestUser m:type="Edm.Boolean">false</d:IsEmailAuthenticationGuestUser>
<d:IsShareByEmailGuestUser m:type="Edm.Boolean">false</d:IsShareByEmailGuestUser>
<d:IsSiteAdmin m:type="Edm.Boolean">false</d:IsSiteAdmin>
<d:UserId m:null="true"/>
<d:UserPrincipalName m:null="true"/>
</m:properties>
</content>
</entry>
Is there a way I can retrieve the actual members?
Am I taking the right approach towards building email notifications?
Solution
If you want to get all the users in your SharePoint site you can use :
/_api/web/siteusers?
Update:
Try using below endpoint to get the Site Members from Members group:
_api/Web/SiteGroups/GetByName('SiteName Members')/users
To get the specific property for user, you can add $select
query as given below:
_api/Web/SiteGroups/GetByName('SiteName Members')/users?$select=Email,Id
You can get the following user properties using this endpoint:
- Groups
- Id
- IsHiddenInUI
- IsSiteAdmin
- LoginName
- PrincipalType
- Title
- UserId
You can find everything related to Users, groups, and roles using REST API in below article(Must visit, this is a very helpful article): Users, groups, and roles using REST API.
OTHER TIPS
You are getting back the actual users, just not their email address. You need to select Email, like:
_spPageContextInfo.webAbsoluteUrl + "/_api/web/sitegroups/getbyname('Open%20Source%20Tech%20Talk%20Members')/users?$select=Email"
This will get back a response like:
{
value: [
0: {
Email: "mcsheaj@domain.com"
},
length: 2
]
}
assuming you set an accept header like:
headers: {
'accept': 'application/json;odata=nometadata'
}
Just to keep an extra piece of information, since I can't add comments yet.
Here is a guide I have followed, which unfortunately though throws an error at some point due to mismatch between the JSON and the scheme.
[https://www.intelogy.co.uk/blog/assigning-microsoft-flow-approvals-to-sharepoint-groups/][1]
I think this can be solved by retrieving the JSON once and then pasting it to the Parser in order to re-create the correct schema, but following the solutions above (kudos to Joe McShea and Ganesh Sanap), as to pre-filter looking only for the email of users makes it way easier!
Just use the following as an HTTP GET request to Sharepoint:
_api/web/sitegroups/getbyname('Open%20Source%20Tech%20Talk%20Members')/users?$select=Email"
add the accept headers as mentioned above and use the following as a schema when you need to Parse the JSON
{
"type": "object",
"properties": {
"d": {
"type": "object",
"properties": {
"results": {
"type": "array",
"items": {
"type": "object",
"properties": {
"__metadata": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"uri": {
"type": "string"
},
"type": {
"type": "string"
}
}
},
"Email": {
"type": "string"
}
},
"required": [
"__metadata",
"Email"
]
}
}
}
}
}
}
```
Then you can create an array with all the emails to use in your Approval.
It works fine with a custom Sharepoint group in the site when members are added individually.
I have not tested if you can invite AD groups and get the members too, if that's what you are looking for.
[1]: https://www.intelogy.co.uk/blog/assigning-microsoft-flow-approvals-to-sharepoint-groups/