While working on permission i got a requirement to export site permissions an write to a text file separated by pipe line..
Please find the codes below
private void ExportPermissions()
{
StringBuilder strBuilderLogger_public = new StringBuilder();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite mySite = new SPSite("SITE URL"))
{
using (SPWeb myWeb = mySite.OpenWeb("WEB NAME"))
{
if (myWeb != null)
{
//Logging
strBuilderLogger_public.AppendLine("Site Name|List Name|List Item Name|Current Permissons|Parent Permissions|Has Unique Permissions");
string strSiteCurrentRoleDefinitions_public = GetRoleAssignmentsByObject(myWeb, null, null).ToString();
//Roles Logging
strBuilderLogger_public.AppendLine(myWeb.Title + "|||" + strSiteCurrentRoleDefinitions_public + "|" + "|TRUE");
foreach (SPList myList in myWeb.Lists)
{
if (!myList.Hidden)
{
string strListCurrentRoleDefinitions_public = GetRoleAssignmentsByObject(null, myList, null).ToString();
if (!myList.HasUniqueRoleAssignments)
{
strBuilderLogger_public.AppendLine(myWeb.Title + "|" + myList.ToString() + "||" + strListCurrentRoleDefinitions_public + "|" + strSiteCurrentRoleDefinitions_public + "|TRUE");
}
else
{
strBuilderLogger_public.AppendLine(myWeb.Title + "|" + myList.ToString() + "||" + strListCurrentRoleDefinitions_public + "|" + strSiteCurrentRoleDefinitions_public + "|FALSE");
}
SPQuery mySpQuery = new SPQuery();
mySpQuery.ViewAttributes = "Scope=\"RecursiveAll\"";
SPListItemCollection mySPListItemCollection = myList.GetItems(mySpQuery);
strBuilderLogger_public.AppendLine("Site Name|List Name|List Item Name|Current Permissons|Parent Permissions|Has Unique Permissions");
string strMyFileName = string.Empty;
foreach (SPListItem mySPListItem in mySPListItemCollection)
{
string strListItemName_public = string.Empty;
if (mySPListItem["Name"].ToString().ToUpper() != mySPListItem.DisplayName.ToUpper())
{
strListItemName_public = mySPListItem["Name"].ToString() + "_" + mySPListItem.DisplayName;
}
else
{
strListItemName_public = mySPListItem["Name"].ToString();
}
string strItemCurrentRoleDefinitions_public = string.Empty;
strItemCurrentRoleDefinitions_public = GetRoleAssignmentsByObject(null, null, mySPListItem).ToString();
//Checking Item Unique Permission
if (!mySPListItem.HasUniqueRoleAssignments)
{
strBuilderLogger_public.AppendLine(myWeb.Title + "|" + myList.ToString() + "|" + strListItemName_public + "|" + strItemCurrentRoleDefinitions_public + "|" + strListCurrentRoleDefinitions_public + "|TRUE");
}
else
{
strBuilderLogger_public.AppendLine(myWeb.Title + "|" + myList.ToString() + "|" + strListItemName_public + "|" + strItemCurrentRoleDefinitions_public + "|" + strListCurrentRoleDefinitions_public + "|FALSE");
}
}
}
}
//Logging
WriteLogEntry(strLoggerFileName_public, "Site Level Broken Permission Exported List", strBuilderLogger_public.ToString());
}
}
}
});
}
public void WriteLogEntry(string loggerFileName, string strTitle, string strMessage)
{
try
{
//Create Logger Folder if not exists
if (!System.IO.Directory.Exists(@"C:\logs\"))
{
System.IO.Directory.CreateDirectory(@"C:\logs\");
}
string strFileLoc = loggerFileName;
StreamWriter writer = new StreamWriter(strFileLoc, true);
writer.WriteLine(DateTime.Now.ToString() + " :- " + strTitle + System.Environment.NewLine + strMessage + System.Environment.NewLine + "");
writer.Close();
}
catch { }
}
private StringBuilder GetRoleAssignmentsByObject(SPWeb myWeb, SPList myList, SPListItem myListItem)
{
StringBuilder strBuilderLogger = new StringBuilder();
SPRoleAssignmentCollection myRoleAssignmentCollection;
if (myWeb != null)
{
myRoleAssignmentCollection = myWeb.RoleAssignments;
}
else if (myList != null)
{
myRoleAssignmentCollection = myList.RoleAssignments;
}
else
{
myRoleAssignmentCollection = myListItem.RoleAssignments;
}
foreach (SPRoleAssignment myRoleAssignment in myRoleAssignmentCollection)
{
SPPrincipal myPrincipal = myRoleAssignment.Member;
string strPrincipalNameAndRoleDefinition = myPrincipal.Name;
SPRoleDefinitionBindingCollection myRoleDefinitionBindingCollection = myRoleAssignment.RoleDefinitionBindings;
bool flag = false;
foreach (SPRoleDefinition myRoleDefinition in myRoleDefinitionBindingCollection)
{
if (myRoleDefinition.Name.ToUpper() != "Limited Access".ToUpper())
{
strPrincipalNameAndRoleDefinition += "-" + myRoleDefinition.Name;
flag = true;
}
}
//Logging
if (flag)
{
strBuilderLogger.Append(strPrincipalNameAndRoleDefinition + ",");
}
}
if (!string.IsNullOrEmpty(strBuilderLogger.ToString()))
{
string strBeforeFormat = string.Empty;
strBeforeFormat = strBuilderLogger.ToString();
strBeforeFormat = strBeforeFormat.Substring(0, strBeforeFormat.Length - 1);
strBuilderLogger = new StringBuilder();
strBuilderLogger.Append(strBeforeFormat);
}
return strBuilderLogger;
}
Please find the codes below
private void ExportPermissions()
{
StringBuilder strBuilderLogger_public = new StringBuilder();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite mySite = new SPSite("SITE URL"))
{
using (SPWeb myWeb = mySite.OpenWeb("WEB NAME"))
{
if (myWeb != null)
{
//Logging
strBuilderLogger_public.AppendLine("Site Name|List Name|List Item Name|Current Permissons|Parent Permissions|Has Unique Permissions");
string strSiteCurrentRoleDefinitions_public = GetRoleAssignmentsByObject(myWeb, null, null).ToString();
//Roles Logging
strBuilderLogger_public.AppendLine(myWeb.Title + "|||" + strSiteCurrentRoleDefinitions_public + "|" + "|TRUE");
foreach (SPList myList in myWeb.Lists)
{
if (!myList.Hidden)
{
string strListCurrentRoleDefinitions_public = GetRoleAssignmentsByObject(null, myList, null).ToString();
if (!myList.HasUniqueRoleAssignments)
{
strBuilderLogger_public.AppendLine(myWeb.Title + "|" + myList.ToString() + "||" + strListCurrentRoleDefinitions_public + "|" + strSiteCurrentRoleDefinitions_public + "|TRUE");
}
else
{
strBuilderLogger_public.AppendLine(myWeb.Title + "|" + myList.ToString() + "||" + strListCurrentRoleDefinitions_public + "|" + strSiteCurrentRoleDefinitions_public + "|FALSE");
}
SPQuery mySpQuery = new SPQuery();
mySpQuery.ViewAttributes = "Scope=\"RecursiveAll\"";
SPListItemCollection mySPListItemCollection = myList.GetItems(mySpQuery);
strBuilderLogger_public.AppendLine("Site Name|List Name|List Item Name|Current Permissons|Parent Permissions|Has Unique Permissions");
string strMyFileName = string.Empty;
foreach (SPListItem mySPListItem in mySPListItemCollection)
{
string strListItemName_public = string.Empty;
if (mySPListItem["Name"].ToString().ToUpper() != mySPListItem.DisplayName.ToUpper())
{
strListItemName_public = mySPListItem["Name"].ToString() + "_" + mySPListItem.DisplayName;
}
else
{
strListItemName_public = mySPListItem["Name"].ToString();
}
string strItemCurrentRoleDefinitions_public = string.Empty;
strItemCurrentRoleDefinitions_public = GetRoleAssignmentsByObject(null, null, mySPListItem).ToString();
//Checking Item Unique Permission
if (!mySPListItem.HasUniqueRoleAssignments)
{
strBuilderLogger_public.AppendLine(myWeb.Title + "|" + myList.ToString() + "|" + strListItemName_public + "|" + strItemCurrentRoleDefinitions_public + "|" + strListCurrentRoleDefinitions_public + "|TRUE");
}
else
{
strBuilderLogger_public.AppendLine(myWeb.Title + "|" + myList.ToString() + "|" + strListItemName_public + "|" + strItemCurrentRoleDefinitions_public + "|" + strListCurrentRoleDefinitions_public + "|FALSE");
}
}
}
}
//Logging
WriteLogEntry(strLoggerFileName_public, "Site Level Broken Permission Exported List", strBuilderLogger_public.ToString());
}
}
}
});
}
public void WriteLogEntry(string loggerFileName, string strTitle, string strMessage)
{
try
{
//Create Logger Folder if not exists
if (!System.IO.Directory.Exists(@"C:\logs\"))
{
System.IO.Directory.CreateDirectory(@"C:\logs\");
}
string strFileLoc = loggerFileName;
StreamWriter writer = new StreamWriter(strFileLoc, true);
writer.WriteLine(DateTime.Now.ToString() + " :- " + strTitle + System.Environment.NewLine + strMessage + System.Environment.NewLine + "");
writer.Close();
}
catch { }
}
private StringBuilder GetRoleAssignmentsByObject(SPWeb myWeb, SPList myList, SPListItem myListItem)
{
StringBuilder strBuilderLogger = new StringBuilder();
SPRoleAssignmentCollection myRoleAssignmentCollection;
if (myWeb != null)
{
myRoleAssignmentCollection = myWeb.RoleAssignments;
}
else if (myList != null)
{
myRoleAssignmentCollection = myList.RoleAssignments;
}
else
{
myRoleAssignmentCollection = myListItem.RoleAssignments;
}
foreach (SPRoleAssignment myRoleAssignment in myRoleAssignmentCollection)
{
SPPrincipal myPrincipal = myRoleAssignment.Member;
string strPrincipalNameAndRoleDefinition = myPrincipal.Name;
SPRoleDefinitionBindingCollection myRoleDefinitionBindingCollection = myRoleAssignment.RoleDefinitionBindings;
bool flag = false;
foreach (SPRoleDefinition myRoleDefinition in myRoleDefinitionBindingCollection)
{
if (myRoleDefinition.Name.ToUpper() != "Limited Access".ToUpper())
{
strPrincipalNameAndRoleDefinition += "-" + myRoleDefinition.Name;
flag = true;
}
}
//Logging
if (flag)
{
strBuilderLogger.Append(strPrincipalNameAndRoleDefinition + ",");
}
}
if (!string.IsNullOrEmpty(strBuilderLogger.ToString()))
{
string strBeforeFormat = string.Empty;
strBeforeFormat = strBuilderLogger.ToString();
strBeforeFormat = strBeforeFormat.Substring(0, strBeforeFormat.Length - 1);
strBuilderLogger = new StringBuilder();
strBuilderLogger.Append(strBeforeFormat);
}
return strBuilderLogger;
}