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
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
Thanks for the informative article. This is one of the best resources I have found in quite some time.
ReplyDeleteDot Net Training in T Nagar
Dot Net training in Velachery
Graphic design courses in porur
Python Training in Tambaram
Android Training in Anna Nagar
Spoken English Classes in Anna Nagar
Software Testing Training in T Nagar
Python training in chennai
Android Training in OMR
Software testing training in Anna Nagar