SPFX - Get what permission group user has in modern?
-
13-01-2021 - |
Question
Is it possible too get the user security group? I can't seem to find any information about doing this in SPFX, just for current web and list...
It should be possible having a div that renders the webpart of they have permission else hide it ?
Kind regards
Solution
I would use the two REST-Endpoints suggested in my comment. First call to get the user ID and after that the second call to get the groups in which the user is member.
import { SPHttpClient, SPHttpClientResponse, ISPHttpClientOptions } from '@microsoft/sp-http';
var absoluteUri = this.props.context.pageContext.web.absoluteUrl;
this.props.context.spHttpClient.get(absoluteUri + "/_api/Web/CurrentUser?$select=ID",
SPHttpClient.configurations.v1)
.then((userResponse: SPHttpClientResponse) => {
userResponse.json().then((user: any) => {
var userId = user.Id;
this.props.context.spHttpClient.get(absoluteUri + "/_api/Web/GetUserById(" + userId + ")/Groups",
SPHttpClient.configurations.v1)
.then((groupResponse: SPHttpClientResponse) => {
groupResponse.json().then((groupsData: any) => {
var groups = groupsData.value;
groups.forEach(group => {
if(group.Title == "TARGETGROUP") {
// DISPLAY DIV
}
});
});
});
});
});
You have to get the current Context from the WebPart. In my example I use a component and get the context from the properties (this.props.context).
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange