Sunday, February 27, 2011

Display permissions assigned to a list programmatically

In my prevoius post explained how to display the views that assigned to a list. By that you can get the views assigned to a perticular list. then what about the permissions assigned to a list. Share point people also described a class for permissions as SPPermissions. But later they extended the SPPermissions class and they deprecated SPPermissions class. But still it is available in the list. By using SPPermissions class we can get the permissions assigned to a list. But they implemented SPRoleAssignment class with thw additional features. they added some extra properties in this class. So it is good to use the SPRoleAssignment class using instead of SPPermissions class.

Here the code to display the permissions assigned to a list.
// display the list permissions.
public static void DisplayListPermissions(string siteUrl)
{     
   if(!string.IsNullOrEmpty(siteUrl)
   {
      using (SPSite currentSite = new SPSite(siteUrl)
      {
          using (SPWeb currentWeb = currentSite.OpenWeb())
          {
             if (currentWeb.Exists)
             {                     
                SPListCollection listCollection = currentWeb.Lists;
                Console.WriteLine("Enter title of the list");
                string listTitle = Console.ReadLine();
                if (string.IsNullOrEmpty(listTitle))
                {
                    Console.WriteLine("list title not be empty");
                }
                else
                {
                   currentList= listCollection[listTitle];
                    if (currentList != null)
                    {
                       foreach (SPRoleAssignment role in currentList.RoleAssignments)
                       {
                          foreach (SPRoleDefinition permission in role.RoleDefinitionBindings)
                          {
                              Console.WriteLine(permission.Name + "->" + role.Member.Name);
                          }
                       }
                     }
                     else
                     {
                        Console.WriteLine("list is null");
                     }
                }
                else
                {
                      Console.WriteLine("the web site is not exists in the current site");
                }
             }
         }
   else
   {
      Console.WriteLine("site url must not be null or empty");
   }
}                
In the above code we are looping the SPRoleAssignent for the list objects and through the role assignment we are looping the SPRoleDefinition. In the role definition we will get the each permissions and users whose having the cirtain permissions in the list.

Share this

0 Comment to "Display permissions assigned to a list programmatically"

Post a Comment