Showing posts with label Check Sharepoint User Role through Object Model Code. Show all posts
Showing posts with label Check Sharepoint User Role through Object Model Code. Show all posts

Wednesday, 28 May 2014

Check Sharepoint User Role through Object Model Code

protected bool CheckUserRole()
{
bool blStatus=true;
SPSite siteCollection = null;
SPWeb topLevelSite = null;
SPList list = null;
string strRoleName = string.Empty;
try
{
siteCollection = new SPSite(System.Configuration.ConfigurationManager.AppSettings["SPDocumentLibrary"]);
topLevelSite = siteCollection.OpenWeb();
SPRoleDefinitionBindingCollection usersRoles = topLevelSite.AllRolesForCurrentUser;
//Full Control
SPRoleDefinition roleDefinition0 = topLevelSite.RoleDefinitions[0];
//Design
SPRoleDefinition roleDefinition1 = topLevelSite.RoleDefinitions[1];
//"Contribute"
SPRoleDefinition roleDefinition2 = topLevelSite.RoleDefinitions[2];
//"Read"
SPRoleDefinition roleDefinition3 = topLevelSite.RoleDefinitions[3];
//"Limited Access"
SPRoleDefinition roleDefinition4 = topLevelSite.RoleDefinitions[4];
//"View Only"
SPRoleDefinition roleDefinition5 = topLevelSite.RoleDefinitions[5];
if (usersRoles.Contains(roleDefinition0))
{
strRoleName=topLevelSite.RoleDefinitions[0].Name;
}
else if (usersRoles.Contains(roleDefinition1))
{
strRoleName=topLevelSite.RoleDefinitions[1].Name;
}
else if (usersRoles.Contains(roleDefinition2))
{
strRoleName=topLevelSite.RoleDefinitions[2].Name;
}
else if (usersRoles.Contains(roleDefinition3))
{
strRoleName=topLevelSite.RoleDefinitions[3].Name;
}
else if (usersRoles.Contains(roleDefinition4))
{
strRoleName=topLevelSite.RoleDefinitions[4].Name;
}
else if (usersRoles.Contains(roleDefinition5))
{
strRoleName=topLevelSite.RoleDefinitions[5].Name;
}
if ((strRoleName == "Read") || (strRoleName == "Limited Access") || (strRoleName == "View Only"))
{
blStatus = false;
}
else
{
blStatus = true;
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
topLevelSite.AllowUnsafeUpdates = false;
siteCollection.AllowUnsafeUpdates = false;
siteCollection.Dispose();
topLevelSite.Dispose();
}
return blStatus;
}