Pnpjs: Permission problem currentUserHasPermissions is undefined

Created on 23 Nov 2018  路  4Comments  路  Source: pnp/pnpjs

  • [ ] Enhancement
  • [ ] Bug
  • [ X] Question
  • [ ] Documentation gap/issue

Hello. I want to check if currentuser has permission to edit ListItem

   pnp.sp.web.lists.getByTitle(listName).items.filter("ID eq " + itemID + "").get().then(function (data) {
        if (data.length > 0) {
            SP.SOD.executeFunc('SP.js', 'SP.ClientContext', function () {
                pnp.sp.web.lists.getByTitle(listName).items.filter("ID eq " + itemID + "").get().then((item) => {
                    item.currentUserHasPermissions(SP.PermissionKind.editListItem).then((haspermission) => {
                        debugger;
                      })
                 }).catch(function (err) {

                });
            });
        }
    });

I do not know why currentUserHasPermissions is undefined...Thank you very much...

code answered question

All 4 comments

Hi @enti333,

You have issues in the code. Let me spotlight some of them:

  • Mixing JSOM for no reason, in PnPjs there is an enumerator with PermissionsKind.
  • Selecting absolutely the same items data twice and treating result array of items metadata as Item instance object:

image

Item instance should be received with items.getById(itemId) to be chained with currentUserHasPermissions method. Please check this out:

import { sp, PermissionKind } from '@pnp/sp';

const listName = 'Custom';
const itemId = 8;

const list = sp.web.lists.getByTitle(listName);

list.items.getById(itemId).currentUserHasPermissions(PermissionKind.EditListItems)
    .then(result => console.log(`You ${result ? 'have' : 'have no'} permissions`))
    .catch(_ => console.log(`Item doesn't exist or you no permissions`));
    /** If no such an item, or list, or no read permissions at all, there will be an error 
     *   in a console and network tab, however the code still manages to interpret it
     *   as no permissions to an item, which is true
     */

Thank you very much for your help. I understand it and I know that my code was not good because of making two times call is not effective...But the problem is...that I was thinking that if user doesn't have permission to editListItem the item data will be not loaded...That means first I load data and then I check if user has permission to edit the data...If Not I disable all inputs... So I do not only need to check if user has permission but also I need to load data in one call....Hmm...Thank you again for your help.

@enti333,

Loading then checking permissions is a viable option, especially when dealing with a few items. But your second get items call is redundant, and can be replaced with the list.items.getById(itemId).currentUserHasPermissions.

Btw, you can also retrieve EffectiveBasePermissions for multiple items in one physical call and check if the base permissions correspond to a required kind like this:

list.items.select('*,EffectiveBasePermissions').get()
    .then(items => {
        return items.map(item => {
            return {
                ...item,
                hasPermissions: list.hasPermissions(
                    item.EffectiveBasePermissions,
                    PermissionKind.EditListItems
                )
            };
        });
    })
    .then(console.log)
    .catch(console.log);

Yes yes I have corrected my code with getbyid. Again thank you veru much for your help.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

simonagren picture simonagren  路  3Comments

jcosta33 picture jcosta33  路  3Comments

un-dres picture un-dres  路  3Comments

AJIXuMuK picture AJIXuMuK  路  3Comments

ahsanranjha picture ahsanranjha  路  3Comments