sharepointnullreferenceexceptionspgroup

Unable to Check Permission of a SharePoint Group


I tried to use examples from the net to check if a group has a specific permission in my SP site.

Below is my code snippet:

if (roleAssignment.Member is SPGroup)
{
    //displays as 1
    Console.WriteLine(roleAssignment.RoleDefinitionBindings.Count)

    if (roleAssignment.RoleDefinitionBindings.Contains(SPContext.Current.Web.RoleDefinitions["Full Control"]))
    {
        //code not reached
    }
}

I met the following error:

System.NullReferenceException: Object reference not set to an instance of an object.

My roleAssignment object exists, and passes the spgroup check.

May I know how can I troubleshoot this error?

Thank you.


Solution

  • You could try this to check if the roleassignment is a group:

    if (roleAssignment.Member.PrincipalType == Microsoft.SharePoint.Client.Utilities.PrincipalType.SharePointGroup)