Skip to main content

How to check permissions of user on sharepoint list using csom.

In this article, we are going to learn "How to check the permissions of user on SharePoint list using csom " or permissions of user using code. In provider-hosted SharePoint Add-ins, we use the csom code and to check the permission of the user on a particular list, once we get the effective permission of particular user on the SharePoint list and then by using that permission we can check the particular kind of permission like Addlistitem, Editelistitem, etc.

In this example, we have created a console application to check whether the user has edit permission on the list or not.
 Pass the client context, email of the user and list name on which you want to check the permission of the user.



using System;

using System.Security;

using Microsoft.SharePoint.Client;

namespace PracticeCsom

{

    class Program

    {

        static void Main(string[] args)

        {
           string siteUrl = "https://***.sharepoint.com/sites/***/";
          using (ClientContext clientContext = new ClientContext(siteUrl))
         {
            SecureString SecurePassword = GetSecureString("Passwpord");
         clientContext.Credentials = new SharePointOnlineCredentials("****@****.onmicrosoft.com",           SecurePassword);
         bool hasPermission = CheckPermissionOnDocumentlibrary(clientContext,                 "*****@***.onmicrosoft.com", "metalist");

             if (hasPermission)
              {
                 Console.WriteLine("User have permission");
              }
           else
           {
              Console.WriteLine("User do not have permission on the list");
             }

            }

  }

 public static bool CheckPermissionOnDocumentlibrary(ClientContext clientContext, string userEmail, string listName)
{
      bool haspermission = false;
     try {
           if (clientContext != null)
            {
           
             Web web = clientContext.Web;
              List list = web.Lists.GetByTitle(listName);
             clientContext.Load(list);
             User user = web.EnsureUser(userEmail);
             clientContext.Load(user);
             clientContext.ExecuteQuery();
             var permissions = list.GetUserEffectivePermissions(user.LoginName);
             clientContext.ExecuteQuery();
            if (permissions.Value.Has(PermissionKind.AddListItems))
             {
                 haspermission = true;
              }
             }
        return haspermission;
       }
     catch (Exception ex)
     {
        return haspermission;
     }
 }

   private static SecureString GetSecureString(String Password)
  {
      SecureString oSecurePassword = new SecureString();
      foreach (Char c in Password.ToCharArray())
       {
         oSecurePassword.AppendChar(c);
       }
       return oSecurePassword;
   }
}
}


Copy the code and paste in your console application and you can copy the method and use it wherever you want either in provider-hosted add-in or in csom code.

Happy Coding

Comments

Post a Comment

Popular posts from this blog

Permission XML for SharePoint Add-in

In this article, we will be discussing the permission XMLs, which are used to provide permission to SharePoint add-in. You can set the App only permission, by setting "true" or "false" in  AllowAppOnlyPolicy=“true” in these permission XMLs.   If add-in granted permission to tenant scope, It includes all children in scope. Before granting access to the app, need to register an app. Follow this link for step by step guide to  How to register an app/add-in in SharePoint   Providing access to add-in at tenant level: To provide access at tenant level user must have the tenant admin level permission. To grant this permission tenant admin can use this URL   https://tenantName-admin.sharepoint.com/_layouts/appinv.aspx Search the App Id (Client ID) and it will fetch all the information which you provide at time of registration of app.   Assign full control at tenant level : <AppPermissionRequests AllowAppOnlyPolicy="true">   <AppPermissionRequest  Scop

How to register an app in SharePoint

In this article, we are going to learn how to register an app/add-in in SharePoint online. If you have created a Sharepoint provider-hosted add-in or other SharePoint solution and to access the SharePoint site or list using the app/add-in you need to register the add-in on SharePoint and to grant permission. Register app in SharePoint 1. Login into SharePoint and Navigate to register an app page. Use the URL mentioned below:          [Sitecollection URL]/_layouts/15/appregnew.aspx 2.  Once you navigate to the Register app page, a form will open. You need to fill all the fields on the form. Client Id and Client Secret  generated when we click on the generate button available next to the fields.  Title : Name of the add-in which you want to give. App Domain : Where your domain is hosted, for the local environment you can place             "www.localhost.com". Do not include HTTP or HTTPS in App Domain. Redirect URL :  Insert the redirect URL, for t

How to create list from template in sharepoint

In this article, we are going to learn how to create a list from list template or using(.stp) file or we can say create list from existing list. Once you save the list as a template, It appears into List template option under the web designer gallery on site setting page. Steps to create list from list template: 1 Move to the site setting section where you have saved the list template. 2. When you click on the List templates, you will redirect to the List template gallery and from the list template gallery. 3. Now click on the File in ribbon and click on the upload document. 4. Browse the location where your list template is downloaded. I am uploading the template of an address list. 5.  After browsing the template, now you can give the name what you want to give to the list template and click on save. 6. Now the list template file appears into the list template gallery. Now click on the gear  icon and click on the Add an app. 7. On clicking add an app,