October 6, 2022

Samir Daoudi's Technical Blog

SharePoint, Office 365, C#, .Net, SQL Server News, Tips, Ebooks, Events, Webinars ….etc

SharePoint, remove all permissions from a document or list item

private void RemoveAllPermissions(SPListItem CurrentlistItem) Guid siteId = CurrentlistItem eb ite D; Guid webId = CurrentlistItem D; D; int itemId = CurrentlistItem D; ite //re-fetch the item SPListItem item = list item emove(i);

This chunk of code is for use with care, it would be useful in case you have a logic behind a scene which assigns specific permissions depending on your logic.

You have to be in elevated Privileges mode to run this code.

 

private void RemoveAllPermissions(SPListItem CurrentlistItem)
{
Guid siteId = CurrentlistItem.Web.Site.ID;
Guid webId = CurrentlistItem.Web.ID;
Guid listId = CurrentlistItem.ParentList.ID;
int itemId = CurrentlistItem.ID;
SPUserToken token = CurrentlistItem.Web.Site.SystemAccount.UserToken;

//run role removal under new instance of the site running with system privileges for security
using (SPSite site = new SPSite(siteId, token))
{
using (SPWeb web = site.OpenWeb(webId))
{
//re-fetch the item
SPList list = web.Lists[listId];
SPListItem item = list.GetItemById(itemId);

//break role inheritance and remove all roles
item.BreakRoleInheritance(true);
web.AllowUnsafeUpdates = true;

SPRoleAssignmentCollection SPRoleAssColn = item.RoleAssignments;
for (int i = SPRoleAssColn.Count – 1; i >= 0; i–)
{
SPRoleAssColn.Remove(i);
}
}
}
}

Leave a comment

You may have missed

Show Buttons
Hide Buttons

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

%d bloggers like this: