Wednesday, October 17, 2012

How to retrieve users permission using Sharepoint Object model code



SAMPLE CODE 

static void Main()
{
try
{
using (SPSite oSiteCollection = new SPSite("http://SiteURL/"))  
{
SPWebCollection collWebsites = oSiteCollection.AllWebs;
Console.WriteLine("Websites Count: {0}", collWebsites.Count);
Console.WriteLine(" ");
string rootwebtitle = oSiteCollection.RootWeb.Title;
foreach (SPWeb oWebsite in collWebsites)
{
Console.WriteLine("Site : " + oWebsite.Title);

string websiteurl = oWebsite.Url;
Console.WriteLine("Website Url : " + websiteurl);
allUser(websiteurl);
oWebsite.Dispose();
Console.WriteLine("--------------------");
}
}

Console.WriteLine("------Program Completed Please click enter to exit------");  
Console.ReadLine();
}
catch (Exception ex)
{
Console.WriteLine("Error occured" + ex.Message);
Console.WriteLine("------Program terminated due to error. Please click enter to exit------");
Console.ReadLine();
}


}


public static void allUser(string url)
{


using (SPSite site = new SPSite(url))
{

using (SPWeb web = site.OpenWeb())
{
foreach (SPUser user in web.AllUsers)  

{


if (user.Name != "NT AUTHORITY\\LOCAL SERVICE" && user.Name != "NT AUTHORITY\\Authenticated Users")
{
Console.WriteLine(user.Name);
SPRoleAssignment assignment = web.RoleAssignments.GetAssignmentByPrincipal(user);

Console.WriteLine("RoleAssignment Name :- " + assignment.Member + " :::: RoleAssignment Parent :- " + assignment.Parent);
SPRoleDefinitionBindingCollection roleDefinitions = assignment.RoleDefinitionBindings;

foreach (SPRoleDefinition roleDef in roleDefinitions)
{
Console.WriteLine("RoleDefinition Name :- " + roleDef.Name + " Base Permission :- " + roleDef.BasePermissions);
Console.WriteLine("________________________________________________________________________");
}
}

}

}

}
}

No comments:

Post a Comment