Friday, August 24, 2012

Sharepoint : Get User's Permission for Lists

Hi this is a small program that print the permission for a given user in a sharepoint site
this helps the admins to know whether a user has access to a particular list and what access.

This simply helps to know what a user can do in each list.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace PermissionViewer
{
    class Program
    {
        static void Main(string[] args)
        {
            //Get the site url
            Console.Write("Enter Site Url  : ");
            string siteURL = Console.ReadLine();

            //Get the user name of user
            Console.Write("Enter User Name : ");
            string UserName = Console.ReadLine();

            try
            {
                //Get the sharepoint site
                using (SPSite sharepointSite = new SPSite(siteURL))
                {
                    //open the site
                    using (SPWeb sharepointweb = sharepointSite.OpenWeb())
                    {
                        //Get each and every list in sharepoint site
                        foreach (SPList sharepointList in sharepointweb.Lists)
                        {
                            //Get roles assigned for each list
                            foreach (SPRoleAssignment assignedRoles in sharepointList.RoleAssignments)
                            {
                                //Check whether the role if for a user
                                if (assignedRoles.Member is SPUser)
                                {
                                    //get the user from the role
                                    SPUser user = (SPUser)assignedRoles.Member;
                                    //Check whether its the same user that is entered
                                    if (user.Name.Equals(UserName, StringComparison.OrdinalIgnoreCase))
                                    {
                                        //removes user
                                        //item.RoleAssignments.Remove(item1);

                                        //Get users permissions - Add Remove Etc
                                        foreach (SPRoleDefinition item in assignedRoles.RoleDefinitionBindings)
                                        {
                                            Console.WriteLine("List : " + sharepointList.Title + "\nAccess : " + item.Name + "\n");
                                        }
                                    }
                                }

                                //Check whether the role is for a security group
                                if (assignedRoles.Member is SPGroup)
                                {
                                    //Get the security group
                                    SPGroup grp = (SPGroup)assignedRoles.Member;

                                    //Iterate members of the group
                                    foreach (SPUser groupMember in grp.Users)
                                    {
                                        //Check whether the user is the entered user
                                        if (groupMember.Name.Equals(UserName, StringComparison.OrdinalIgnoreCase))
                                        {
                                            //Get users permissions - Add Remove Etc
                                            foreach (SPRoleDefinition item in assignedRoles.RoleDefinitionBindings)
                                            {
                                                Console.WriteLine("List : " + sharepointList.Title + "\nGroup : " + grp.Name + "\nAccess : " + item.Name + "\n");
                                            }
                                            //uncomment if you want to remove a person from group
                                            //grp.RemoveUser(user1);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }

            }
            catch (Exception Ex)
            {
                //Print if any exceptions occur
                Console.WriteLine("Error : "+Ex.Message);
            }
            //used make sure that the prompt is not closed
            Console.WriteLine("insert some value to exit");
            Console.ReadLine();
        }
    }
}