SharePoint 2016 Logo

There's a small snippet to check if current user permissions are enough to save list items. It can be used in SPFX webparts with PNP JS.

When to use it? For example, in WebParts, where user should save something in SPList and you want to avoid the errors. Or it also can be a check before updating list permissions.


import "@pnp/sp/webs";
import "@pnp/sp/lists";
import "@pnp/sp/items";
import "@pnp/sp/sites";
import "@pnp/sp/site-users/web";
import "@pnp/sp/sputilities";
import "@pnp/sp/security";
import "@pnp/sp/security/web";
....

public async checkUserPermissions(): Promise<boolean> {
  try {
    // Get the current user's effective base permissions for the site
    const currentUserPerms = await useSpfx().web.getCurrentUserEffectivePermissions()
    const hasContributePermissions: boolean = spfi().web.hasPermissions(currentUserPerms, PermissionKind.AddListItems)
    return hasContributePermissions;

  } catch (error) {
    console.error('Error checking permissions: ', error);
    return false;
  }
}