1

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

2
  • What exactly do you mean with "user security group"? Did you tried to call the REST-Endpoint /api/Web/CurrentUser and after that /api/Web/GetUserById(<USERID>)/Groups ?
    – Oskar
    Commented Apr 5, 2018 at 10:34
  • Hi! I mean if they are part of Read group ,designer or admin. So on certain pages the designer should see additional webparts that the Readers can't
    – Amazinjoey
    Commented Apr 5, 2018 at 10:46

1 Answer 1

2

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).

3
  • 4
    Btw its possible to call groups from currentuser: _api/Web/CurrentUser/Groups?$select=Title Commented Apr 5, 2018 at 11:10
  • Ahh...! You are right ;-) We don't need to call two REST-Endpoints.
    – Oskar
    Commented Apr 5, 2018 at 11:13
  • Saw @AndersAune comment now! Yea I also went with "/_api/Web/CurrentUser/Groups?$select=Title", nice to see that I did it the right way
    – Amazinjoey
    Commented Apr 5, 2018 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.