Reputation: 29
We have a C# Windows application that uses PnP Core to interact with SharePoint files.
We have a function in our app that uploads files to a SharePoint folder that the user selects. At the moment, we are catching any permission-related issues during the file upload process.
Is there a way to check if the user has permissions to upload a file to the selected SharePoint folder, BEFORE attempting to upload it?
We started looking at the specific SharePoint permissions / roles of the user, but we were not sure if that was the best path, or also what to actually look for.
Upvotes: 1
Views: 710
Reputation: 245
You will need to check if the user has access to the folder. Essentially the user should have "Microsoft.SharePoint.Client.PermissionKind.AddListItems" at the folder level.
Here is a code sample for CSOM (you will need to adapt if using PnP):
User oUser = cilentContext.Web.CurrentUser;
ListItem oItem = cilentContext.Web.GetFolderByServerRelativePath('path_to-folder');
cilentContext.Load(oItem);
cilentContext.Load(oUser);
cilentContext.ExecuteQuery();
var oPermissions = oItem.GetUserEffectivePermissions(oUser.LoginName);
cilentContext.ExecuteQuery();
if (oPermissions.Value.Has(Microsoft.SharePoint.Client.PermissionKind.AddListItems))
{
return true;
}
else
{
return false;
}
Upvotes: 1