Easy way to check user’s permission on SharePoint Online site in the web part

Easy way to check user’s permission on SharePoint Online site in the web part

Easy way to check user’s permission on SharePoint Online site in the web part

Hello Everyone! 🎉 Happy New Year 2021 🎉 I hope you have a wonderful holiday, good vibes, and a nice party 🍻. This blog is the 2nd content which’s I write in English. In the previous blog, I explained about SharePoint group & site permission levels(more). Today, I will share the easy way to check user’s permission on the SharePoint site in our custom web part.

In the web part, There is provided the permission values of the current user in WebPartContext. Therefore, you can see that user’s permission value via console.log it in the render method in HelloWorldWebPart.ts file

    console.log(this.context.pageContext.web.permissions);

Sharepoint check user permission values This value comes from the summary of the user’s permission (more detail)

You can see it returns the number value of High and Low. But, For what? How can we know the user permission from these data?

*For an example below, I created a new HelloWorld web part and plug it on my experiment site via append “/_layouts/15/workbench.aspx” in URL.

  1. I create the “checkFullControlPermission” method in HelloWorldWebPart.ts for checking that the user has a Full Control role on site. For this role, we can check the user’s permission from the ability to manageWeb enum.
    import { SPPermission } from '@microsoft/sp-page-context';

    private checkFullControlPermission = (): boolean => {
        //Full Control group can add item to list/library and mange web.
        let permission = new SPPermission(this.context.pageContext.web.permissions.value);
        let isFullControl = permission.hasPermission(SPPermission.manageWeb);

        return isFullControl;
    }
  1. I create the “checkEditorPermission” method in the same file for checking that the user has an Editor role on site. About the editor role, we can check the user’s permission from the ability to addListItems enum.
    private checkEditorPermission = () => {
        //Editor group can add item on list/library via addListItems permission
        let permission = new SPPermission(this.context.pageContext.web.permissions.value);
        let isMemberPermission = permission.hasPermission(SPPermission.addListItems);

        return isMemberPermission;
    }
  1. I create the “checkReadPermission” method in the same file for checking that the user has a Reader role on site. About the reader role, we can check the user’s permission from the ability to viewListItems enum.
    private checkReadPermission = () => {
        //Reader group can read item on list/library via viewListItems permission
        let permission = new SPPermission(this.context.pageContext.web.permissions.value);
        let isReadPermission = permission.hasPermission(SPPermission.viewListItems);

        return isReadPermission;
    }
  1. After that, I call these methods in a render() method of HelloWorldWebPart.ts file and print this permission checking in console.log
    public render(): void {
        let isFullControl = this.checkFullControlPermission();
        let isEditor = this.checkEditorPermission();
        let isReader = this.checkReadPermission();

        console.log(`FullControl: ${isFullControl}`);
        console.log(`Member: ${isEditor}`);
        console.log(`Reader: ${isReader}`);

        const element: React.ReactElement<IHelloWorldProps> = React.createElement(HelloWorld, { description: this.properties.description });
        ReactDom.render(element, this.domElement);
    }

Sharepoint check user permission logging The full control role returns like this picture.

  1. Next, I pass the username, isFullControl, isEditor, and isReader as Props of HelloWorld.tsx component.

Sharepoint check user permission example props passing code Sharepoint check user permission example props code

  1. Lastly, I print the user’s role on the web part UI via checking that flags like a picture below.
  • Full Control role: They must have all parameters as true.
  • Editor role: They must have permission as true on isEditor and isReader.
  • Reader role: They must have permission as true on isReader only.

Sharepoint check user permission example full code Sharepoint check user permission result in UI


Yay!!! This is an easy way to check user’s permission on SharePoint site in SPFx. For this checking, It’s just the easy way to check on SPFx with default site permission. If you have complex permission on your site, you can adjust an enum of SPPermission to proper to your site permission checking.

I hope this blog will help you get a solution to check the user’s permission and can apply this solution to your web part. Bye Bye 👋

P.S. Lastly, if anyone is interested in reading more about the technology of Mycos Company, you can follow and read further content on their Medium page at https://medium.com/mycostech

Reference:

Related Posts

Let's check our site's accessibility easily using 'Accessibility Insights for Web' on Microsoft Edge.

Let's check our site's accessibility easily using 'Accessibility Insights for Web' on Microsoft Edge.

Hello to all the readers who have come across this article. Lately, I've been quite busy and it's taken me a while to find some free time to write on Medium. Today, I want to share some knowledge tha

read more
Let's try to create Power Automate connected to SQL Server for sending Leave Approval Email using Low code together.

Let's try to create Power Automate connected to SQL Server for sending Leave Approval Email using Low code together.

Hello everyone! I've been away for a long time from blogging due to various events that have kept me busy, making it challenging to find time for writing. In my latest article, I briefly introduced P

read more
Conditional Formatting (Fx) in PowerBI Custom Visual

Conditional Formatting (Fx) in PowerBI Custom Visual

👋 Hi Everyone 👋 During this time, I have a chance to implement the PowerBI Custom Visual with my team. And we spent a lot of time researching the conditional formatting (Fx) and we found many inter

read more
An Introduction to Microsoft's Power Automate

An Introduction to Microsoft's Power Automate

Today, we're introducing Microsoft's Power Automate, formerly known as Microsoft Flow. If you're familiar with Microsoft Power Platform services such as Dynamics 365, SharePoint, Power Apps, PowerBI,

read more
Pass Through Data Over IServiceProvider.CreateScope()

Pass Through Data Over IServiceProvider.CreateScope()

[ASP.NET] In some cases you may encounter the situation that you need to pass through some particular data over a new scope of Service Provider.For instance, when you implement a solution that inte

read more
Write Unit Tests for React Hooks using react-hooks-testing-library

Write Unit Tests for React Hooks using react-hooks-testing-library

Hooks in React are a feature that has drastically changed the way we write React. It's like undergoing plastic surgery in Korea, where some developers love the new look, while others prefer the old o

read more
Scan code with Credential Scanner on Azure DevOps

Scan code with Credential Scanner on Azure DevOps

🥳 Happy New Year 2023! 🥳Wishing everyone a great year ahead!Now, let's get down to business. Today, I'm going to introduce you to a handy tool that checks for leaked passwords, secrets, certifi

read more
SharePoint Group & Permission levels in SharePoint Online

SharePoint Group & Permission levels in SharePoint Online

Hello everyone 👋 !!! This is the 1st time that I wrote the SharePoint Online blog in English. This blog explains permission levels in SharePoint Online. Once you create a new site in SharePoint Onli

read more
Speed up and make your SPFx reloads quick and easy with the SPFx-Fast-Serve tool.

Speed up and make your SPFx reloads quick and easy with the SPFx-Fast-Serve tool.

Hello everyone! I'm back! In the past period, I've been busy expanding my knowledge, reading some Microsoft Learning materials, trying out new things, and handling various tasks, which left me with l

read more
What is SharePoint? How does it work? Let's take a look together! 😆

What is SharePoint? How does it work? Let's take a look together! 😆

Hello everyone who stumbled upon and is reading this content. After spending a considerable time exploring various content on Medium, I wanted to share a little bit of my knowledge. Having delved int

read more