0

I need to find out whether a user is in a group. If he is, then I could show or hide properties.

I have the below code, can this be used or adapted to find out whether a user is in a 'manager group'?

let grupy = sp.web.currentUser.groups.getByName("Właściciele witryny Portal UAM").users;
console.log(grupy);

One Question is about a properties and second is about current user logged.

Here are all my properties:

 protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration
 {

let contextWeb =  this.context.pageContext.web;
let permission = new SPPermission(contextWeb.permissions.value);
let hasPermission = permission.hasPermission(SPPermission.createGroups);

if(hasPermission)
{
return {
  pages: [
    {
      header: {
        description: 'Panel globalny Toolboxa',
        link1: strings.Link1FieldLabel,
        link2: strings.Link2FieldLabel
      },
      groups: [
        {
          groupName: 'Właściwości Toolbox',
          groupFields: [
            PropertyPaneHorizontalRule(),
            PropertyPaneTextField('adres_Listy', {
              label: "Adres listy docelowej",
              disabled: !hasPermission
            }),
            PropertyFieldListPicker('lists', {
              label: 'Wybierz listę',
              selectedList: this.properties.lists,
              includeHidden: false,
              orderBy: PropertyFieldListPickerOrderBy.Title,
              disabled: !hasPermission,
              onPropertyChange: this.onPropertyPaneFieldChanged.bind(this),
              properties: this.properties,
              context: this.context,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'listPickerFieldId'
            }),
            PropertyPaneCheckbox('checkboxProperty5', {
              text: this.checkboxProperty5,
              checked: true,
              disabled: !hasPermission
            }),
            PropertyFieldPicturePicker('ToolBoxLogo', {
              label: 'Wybierz zdjęcie',
              initialValue: this.properties.ToolBoxLogo,
              readOnly: true,
              previewImage: true,
              allowedFileExtensions: '.gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png',
              disabled: !hasPermission,
              onPropertyChange: this.PictureChange.bind(this),
              render: this.render.bind(this),
              disableReactivePropertyChanges: this.disableReactivePropertyChanges,
              properties: this.properties,
              context: this.context,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'ToolboxLogoId'
            }),
            PropertyPaneHorizontalRule(),
            PropertyPaneTextField('zaokragloneRogi', {
              label: "Podaj jak duże zaokrąglenia",
              disabled: !hasPermission
            }),
            PropertyFieldColorPicker('color', {
              label: 'Wybierz kolor tła:',
              initialColor: this.properties.newValue,
              onPropertyChange: this.BackgroundColor.bind(this),
              render: this.render.bind(this),
              disableReactivePropertyChanges: this.disableReactivePropertyChanges,
              properties: this.properties,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'colorFieldId'
            }),
            PropertyFieldColorPicker('colorText', {
              label: 'Wybierz kolor tekstu:',
              initialColor: this.properties.newValueText,
              onPropertyChange: this.TextColor.bind(this),
              render: this.render.bind(this),
              disableReactivePropertyChanges: this.disableReactivePropertyChanges,
              properties: this.properties,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'colorTextFieldId'
            }),
          ]
        }
      ]
    },
    {
      header: {
        description: 'Panel do zarządzania wierszami w Toolboxie',
        link1: strings.Link1FieldLabel,
        link2: strings.Link2FieldLabel
      },
      groups: [
        {
          groupName: 'Widoczność wierszy - Określamy które wiersze będą widoczne',
          groupFields: [
            PropertyPaneHorizontalRule(),
            PropertyPaneCheckbox('checkboxProperty1', {
              text: this.checkboxProperty1,
              checked: true
            }),
            PropertyPaneCheckbox('checkboxProperty2', {
              text: this.checkboxProperty2,
              checked: true,
            }),
            PropertyPaneCheckbox('checkboxProperty3', {
              text: this.checkboxProperty3,
              checked: true
            }),
            PropertyPaneCheckbox('checkboxProperty4', {
              text: this.checkboxProperty4,
              checked: true,
            })
          ]
        },
        {
          groupName: 'Wiersz 1',
          groupFields: [
            PropertyPaneLabel('Opis', { text: 'Za pomocą Toolboxa możesz na uczelni dodać przydatne linki.' }),
            PropertyPaneHorizontalRule(),
            PropertyPaneTextField('Nazwa1', {
              label: "Nazwa linku nr 1",

            }),
            PropertyPaneTextField('link1', {
              label: "Kafel nr 1"
            }),
            PropertyFieldPicturePicker('picture1', {
              label: 'Wybierz zdjęcie',
              initialValue: this.properties.picture1,
              readOnly: true,
              previewImage: true,
              allowedFileExtensions: '.gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png',
              disabled: false,
              onPropertyChange: this.PictureChange.bind(this),
              render: this.render.bind(this),
              disableReactivePropertyChanges: this.disableReactivePropertyChanges,
              properties: this.properties,
              context: this.context,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'picturePickerFieldId1'
            }),
            PropertyPaneHorizontalRule(),
            PropertyPaneTextField('Nazwa2', {
              label: "Nazwa link nr 2"
            }),
            PropertyPaneTextField('link2', {
              label: "Kafel nr 2"
            }),
            PropertyFieldPicturePicker('picture2', {
              label: 'Wybierz zdjęcie',
              initialValue: this.properties.picture2,
              readOnly: true,
              previewImage: true,
              allowedFileExtensions: '.gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png',
              disabled: false,
              onPropertyChange: this.PictureChange.bind(this),
              render: this.render.bind(this),
              disableReactivePropertyChanges: this.disableReactivePropertyChanges,
              properties: this.properties,
              context: this.context,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'picturePickerFieldId2'
            }),
            PropertyPaneHorizontalRule(),
            PropertyPaneTextField('Nazwa3', {
              label: "Nazwa linku nr 3"
            }),
            PropertyPaneTextField('link3', {
              label: "Kafel nr 3"
            }),
            PropertyFieldPicturePicker('picture3', {
              label: 'Wybierz zdjęcie',
              initialValue: this.properties.picture3,
              readOnly: true,
              previewImage: true,
              allowedFileExtensions: '.gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png',
              disabled: false,
              onPropertyChange: this.PictureChange.bind(this),
              render: this.render.bind(this),
              disableReactivePropertyChanges: this.disableReactivePropertyChanges,
              properties: this.properties,
              context: this.context,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'picturePickerFieldId3'
            }),
            PropertyPaneHorizontalRule(),
            PropertyPaneTextField('Nazwa4', {
              label: "Nazwa linku nr 4"
            }),
            PropertyPaneTextField('link4', {
              label: "Kafel nr 4"
            }),
            PropertyFieldPicturePicker('picture4', {
              label: 'Wybierz zdjęcie',
              initialValue: this.properties.picture4,
              readOnly: true,
              previewImage: true,
              allowedFileExtensions: '.gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png',
              disabled: false,
              onPropertyChange: this.PictureChange.bind(this),
              render: this.render.bind(this),
              disableReactivePropertyChanges: this.disableReactivePropertyChanges,
              properties: this.properties,
              context: this.context,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'picturePickerFieldId4'
            }),
            PropertyPaneHorizontalRule()
          ]
        },
        {
          groupName: 'Wiersz 2',
          groupFields: [
            PropertyPaneHorizontalRule(),
            PropertyPaneTextField('Nazwa5', {
              label: "Nazwa linku nr 5"
            }),
            PropertyPaneTextField('link5', {
              label: "Kafel nr 5"
            }),
            PropertyFieldPicturePicker('picture5', {
              label: 'Wybierz zdjęcie',
              initialValue: this.properties.picture5,
              readOnly: true,
              previewImage: true,
              allowedFileExtensions: '.gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png',
              disabled: false,
              onPropertyChange: this.PictureChange.bind(this),
              render: this.render.bind(this),
              disableReactivePropertyChanges: this.disableReactivePropertyChanges,
              properties: this.properties,
              context: this.context,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'picturePickerFieldId5'
            }),
            PropertyPaneHorizontalRule(),
            PropertyPaneTextField('Nazwa6', {
              label: "Nazwa linku nr 6"
            }),
            PropertyPaneTextField('link6', {
              label: "Kafel nr 6"
            }),
            PropertyFieldPicturePicker('picture6', {
              label: 'Wybierz zdjęcie',
              initialValue: this.properties.picture6,
              readOnly: true,
              previewImage: true,
              allowedFileExtensions: '.gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png',
              disabled: false,
              onPropertyChange: this.PictureChange.bind(this),
              render: this.render.bind(this),
              disableReactivePropertyChanges: this.disableReactivePropertyChanges,
              properties: this.properties,
              context: this.context,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'picturePickerFieldId6'
            }),
            PropertyPaneHorizontalRule(),
            PropertyPaneTextField('Nazwa7', {
              label: "Nazwa linku nr 7"
            }),
            PropertyPaneTextField('link7', {
              label: "Kafel nr 7"
            }),
            PropertyFieldPicturePicker('picture7', {
              label: 'Wybierz zdjęcie',
              initialValue: this.properties.picture7,
              readOnly: true,
              previewImage: true,
              allowedFileExtensions: '.gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png',
              disabled: false,
              onPropertyChange: this.PictureChange.bind(this),
              render: this.render.bind(this),
              disableReactivePropertyChanges: this.disableReactivePropertyChanges,
              properties: this.properties,
              context: this.context,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'picturePickerFieldId7'
            }),
            PropertyPaneHorizontalRule(),
            PropertyPaneTextField('Nazwa8', {
              label: "Nazwa linku nr 8"
            }),
            PropertyPaneTextField('link8', {
              label: "Kafel nr 8"
            }),
            PropertyFieldPicturePicker('picture8', {
              label: 'Wybierz zdjęcie',
              initialValue: this.properties.picture8,
              readOnly: true,
              previewImage: true,
              allowedFileExtensions: '.gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png',
              disabled: false,
              onPropertyChange: this.PictureChange.bind(this),
              render: this.render.bind(this),
              disableReactivePropertyChanges: this.disableReactivePropertyChanges,
              properties: this.properties,
              context: this.context,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'picturePickerFieldId8'
            }),
            PropertyPaneHorizontalRule()
          ]
        },
        {
          groupName: 'Wiersz 3',
          groupFields: [
            PropertyPaneHorizontalRule(),
            PropertyPaneTextField('Nazwa9', {
              label: "Nazwa linku nr 9"
            }),
            PropertyPaneTextField('link9', {
              label: "Kafel nr 9"
            }),
            PropertyFieldPicturePicker('picture9', {
              label: 'Wybierz zdjęcie',
              initialValue: this.properties.picture9,
              readOnly: true,
              previewImage: true,
              allowedFileExtensions: '.gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png',
              disabled: false,
              onPropertyChange: this.PictureChange.bind(this),
              render: this.render.bind(this),
              disableReactivePropertyChanges: this.disableReactivePropertyChanges,
              properties: this.properties,
              context: this.context,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'picturePickerFieldId9'
            }),
            PropertyPaneHorizontalRule(),
            PropertyPaneTextField('Nazwa10', {
              label: "Nazwa linku nr 10"
            }),
            PropertyPaneTextField('link10', {
              label: "Kafel nr 10"
            }),
            PropertyFieldPicturePicker('picture10', {
              label: 'Wybierz zdjęcie',
              initialValue: this.properties.picture10,
              readOnly: true,
              previewImage: true,
              allowedFileExtensions: '.gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png',
              disabled: false,
              onPropertyChange: this.PictureChange.bind(this),
              render: this.render.bind(this),
              disableReactivePropertyChanges: this.disableReactivePropertyChanges,
              properties: this.properties,
              context: this.context,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'picturePickerFieldId10'
            }),
            PropertyPaneHorizontalRule(),
            PropertyPaneTextField('Nazwa11', {
              label: "Nazwa linku nr 11"
            }),
            PropertyPaneTextField('link11', {
              label: "Kafel nr 11"
            }),
            PropertyFieldPicturePicker('picture11', {
              label: 'Wybierz zdjęcie',
              initialValue: this.properties.picture11,
              readOnly: true,
              previewImage: true,
              allowedFileExtensions: '.gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png',
              disabled: false,
              onPropertyChange: this.PictureChange.bind(this),
              render: this.render.bind(this),
              disableReactivePropertyChanges: this.disableReactivePropertyChanges,
              properties: this.properties,
              context: this.context,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'picturePickerFieldId11'
            }),
            PropertyPaneHorizontalRule(),
            PropertyPaneTextField('Nazwa12', {
              label: "Nazwa linku nr 12"
            }),
            PropertyPaneTextField('link12', {
              label: "Kafel nr 12"
            }),
            PropertyFieldPicturePicker('picture12', {
              label: 'Wybierz zdjęcie',
              initialValue: this.properties.picture12,
              readOnly: true,
              previewImage: true,
              allowedFileExtensions: '.gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png',
              disabled: false,
              onPropertyChange: this.PictureChange.bind(this),
              render: this.render.bind(this),
              disableReactivePropertyChanges: this.disableReactivePropertyChanges,
              properties: this.properties,
              context: this.context,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'picturePickerFieldId12'
            }),
            PropertyPaneHorizontalRule()
          ]
        },
        {
          groupName: 'Wiersz 4',
          groupFields: [
            PropertyPaneHorizontalRule(),
            PropertyPaneTextField('Nazwa13', {
              label: "Nazwa linku nr 13"
            }),
            PropertyPaneTextField('link13', {
              label: "Kafel nr 13"
            }),
            PropertyFieldPicturePicker('picture13', {
              label: 'Wybierz zdjęcie',
              initialValue: this.properties.picture13,
              readOnly: true,
              previewImage: true,
              allowedFileExtensions: '.gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png',
              disabled: false,
              onPropertyChange: this.PictureChange.bind(this),
              render: this.render.bind(this),
              disableReactivePropertyChanges: this.disableReactivePropertyChanges,
              properties: this.properties,
              context: this.context,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'picturePickerFieldId13'
            }),
            PropertyPaneHorizontalRule(),
            PropertyPaneTextField('Nazwa14', {
              label: "Nazwa linku nr 14"
            }),
            PropertyPaneTextField('link14', {
              label: "Kafel nr 14"
            }),
            PropertyFieldPicturePicker('picture14', {
              label: 'Wybierz zdjęcie',
              initialValue: this.properties.picture14,
              readOnly: true,
              previewImage: true,
              allowedFileExtensions: '.gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png',
              disabled: false,
              onPropertyChange: this.PictureChange.bind(this),
              render: this.render.bind(this),
              disableReactivePropertyChanges: this.disableReactivePropertyChanges,
              properties: this.properties,
              context: this.context,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'picturePickerFieldId14'
            }),
            PropertyPaneHorizontalRule(),
            PropertyPaneTextField('Nazwa15', {
              label: "Nazwa linku nr 15"
            }),
            PropertyPaneTextField('link15', {
              label: "Kafel nr 15",
            }),
            PropertyFieldPicturePicker('picture15', {
              label: 'Wybierz zdjęcie',
              initialValue: this.properties.picture15,
              readOnly: true,
              previewImage: true,
              allowedFileExtensions: '.gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png',
              disabled: false,
              onPropertyChange: this.PictureChange.bind(this),
              render: this.render.bind(this),
              disableReactivePropertyChanges: this.disableReactivePropertyChanges,
              properties: this.properties,
              context: this.context,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'picturePickerFieldId15'
            }),
            PropertyPaneHorizontalRule(),
            PropertyPaneTextField('Nazwa16', {
              label: "Nazwa linku nr 16"
            }),
            PropertyPaneTextField('link16', {
              label: "Kafel nr 16",
            }),
            PropertyFieldPicturePicker('picture16', {
              label: 'Wybierz zdjęcie',
              initialValue: this.properties.picture16,
              readOnly: true,
              previewImage: true,
              allowedFileExtensions: '.gif,.jpg,.jpeg,.bmp,.dib,.tif,.tiff,.ico,.png',
              disabled: false,
              onPropertyChange: this.PictureChange.bind(this),
              render: this.render.bind(this),
              disableReactivePropertyChanges: this.disableReactivePropertyChanges,
              properties: this.properties,
              context: this.context,
              onGetErrorMessage: null,
              deferredValidationTime: 0,
              key: 'picturePickerFieldId16'
            }),
            PropertyPaneHorizontalRule()
          ]
        }
      ]
    }
  ]
};
}
else{
    return {
      pages: [
        {
          header: {
            description: 'Panel globalny Toolboxa',
            link1: strings.Link1FieldLabel,
            link2: strings.Link2FieldLabel
          },
          groups: [
            {
              groupName: strings.BasicGroupName,
              groupFields: [
                PropertyPaneTextField('description', {
                  label: strings.DescriptionFieldLabel
                })
              ]
            }
          ]
        }
      ]
    };
   }
  }
}
1
  • Where is the part where you are calling the user's group? Sep 17, 2019 at 15:17

2 Answers 2

0

Try using something like below:

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/Groups?$select=Title", SPHttpClient.configurations.v1)  
    .then((groupResponse: SPHttpClientResponse) => {
        groupResponse.json().then((groupsData: any) => {  
            var groups = groupsData.value;

            groups.forEach(group => {
                if(group.Title == "Managers Group") {
                    //User Present in a Group
                }
            });
        });
    });
8
  • It csan't recognize me "Props". Please tell me what should I write there.
    – Grzegorz Z
    Sep 17, 2019 at 15:10
  • 1
    You can use this.context.pageContext. Sep 17, 2019 at 15:32
  • And mów to put A link to specyfy site?
    – Grzegorz Z
    Sep 17, 2019 at 16:14
  • 1
    You can get the site URL either using this.context.pageContext.web.absoluteUrl or this.props.context.pageContext.web.absoluteUrl. Sep 17, 2019 at 17:08
  • 1
    Generally props and state is use in react based webpart. Are you able to get the current user membership in group? Sep 18, 2019 at 2:27
0

Try this if you are checking membership of SINGLE group only:

import { SPHttpClient, SPHttpClientResponse, ISPHttpClientOptions } from '@microsoft/sp-http'; 

var absoluteUri = this.props.context.pageContext.web.absoluteUrl;

this.props.context.spHttpClient.get(absoluteUri + "/_api/web/sitegroups/GetByName('Your Group Name')/Users?$filter=Id%20eq " + currentUserId, SPHttpClient.configurations.v1)  
    .then((groupResponse: SPHttpClientResponse) => {
        groupResponse.json().then((data: any) => {  
            if(data.value.length > 0){
                //User is present in the group
            }
        });
    });
4
  • But the Visual Studio code don't recognise "props"
    – Grzegorz Z
    Sep 17, 2019 at 14:51
  • 1
    Can you please post your complete code in the question? Sep 17, 2019 at 14:57
  • I posted all My Properties configuration. I want to do when someone is at group he see all properties. and if someone is not in properties, he don't see nothing. He can only pass description to propertie panel.
    – Grzegorz Z
    Sep 17, 2019 at 15:05
  • What does means props ? In my account they not there.
    – Grzegorz Z
    Sep 17, 2019 at 21:50

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