I'm trying to show all Item level Permissions in a SharePoint list Column. I already solved this using PowerShell, however it was so slow to run sequentially, I've decided I need a C# Solution:
I have a Multiple "People / Group" field called "WhoHasAccess" which has been added to the "Item" Content Type. This ensures that all files and folders will inherit this column.
What I want is for the Column to be populated with all the User Objects, SharePoint Group Objects, and AD Security Group Objects that have access to the item.
The problem is, I am new to C#. I do not have much of a clue on how to create this, although I would like something that listens for Role Assignment change events, or if that isn't possible perhaps a Custom Field that shows current item permissions.
In PowerShell, I handled getting Folder Permissions like this :
foreach ($Folder in $List.Folders)
{
if ($Folder.RoleAssignments.Count -gt 0)
{
[Microsoft.SharePoint.SPFieldUserValueCollection]$UserCollection = New-Object Microsoft.SharePoint.SPFieldUserValueCollection
foreach ($Role in $Folder.RoleAssignments)
{
$User = $Role.Member.Name
foreach ($Roledef in $Role.RoleDefinitionBindings | where {$_.Name -ne "Limited Access"})
{
if ($Role.Member.Name -like "*,*" -or $Role.Member.Name -like "*\*") #Handles the UserNames on our Domain
{
#Is User
$SPUser = $Web.EnsureUser($Role.Member.Name)
$UserFieldValue = New-Object Microsoft.SharePoint.SPFieldUserValue($Web, $SPUser.ID, $SPUser.LoginName)
$UserCollection.Add($UserFieldValue)
}
else
{
#Is Group
$SPGroup = $Web.SiteGroups.GetByName($Role.Member.Name)
$UserFieldValue = new-object Microsoft.SharePoint.SPFieldUserValue($Web, $SPGroup.ID, $SPGroup.LoginName)
$UserCollection.Add($UserFieldValue)
}
}
}
}
$Folder["WhoHasAccess"] = $UserCollection
$Folder.Update()
}
and I handled Item Permissions like this :
foreach ($Item in $List.Items)
{
if ($Item.RoleAssignments.Count -gt 0)
{
[Microsoft.SharePoint.SPFieldUserValueCollection]$UserCollection = New-Object Microsoft.SharePoint.SPFieldUserValueCollection
foreach ($Role in $Item.RoleAssignments)
{
$User = $Role.Member.Name
foreach ($Roledef in $Role.RoleDefinitionBindings | where {$_.Name -ne "Limited Access"})
{
if ($Role.Member.Name -like "*,*" -or $Role.Member.Name -like "*\*") #Handles the UserNames on our Domain
{
#Is User
$SPUser = $Web.EnsureUser($role.Member.Name)
$UserFieldValue = new-object Microsoft.SharePoint.SPFieldUserValue($Web, $SPUser.ID, $SPUser.LoginName)
$UserCollection.Add($UserFieldValue)
}
else
{
#Is Group
$SpGroup = $web.SiteGroups.GetByName($role.Member.Name)
$UserFieldValue = new-object Microsoft.SharePoint.SPFieldUserValue($Web, $SPGroup.ID, $SPGroup.LoginName)
$UserCollection.Add($UserFieldValue)
}
}
}
}
$Item["WhoHasAccess"] = $UserCollection
$Item.Update()
}
I expect to get a few down-votes for posting such a n00b question, but any help provided would be valuable to me as I figure out SharePoint and C#!
To Summarise, I am looking for :
- An Event Receiver that listens for Role Assignment changes at the item level
- When the Event occurs, the Users and Groups are added to the field "WhoHasAccess"
or
- A Custom Field that shows the current Item Permissions.