Get Site Collection Owners / Administrators report with site storage in MB's export to CSV Programatically.
SPSecurity.RunWithElevatedPrivileges(delegate
{
try
{
string siteUrl = string.Empty;
StreamWriter SW;
SW = File.AppendText("c:\\SiteCollAdminList_Usage.csv");
SW.WriteLine("Site Url, Site Storage (MB), Adnin Name, E-Mail");
String ConfigValues = ConfigurationSettings.AppSettings["WebAppsName"];
String[] ObjWebapps = ConfigValues.Split('|');
foreach (String WebappsName in ObjWebapps)
{
SPWebApplication webapp = SPWebApplication.Lookup(new Uri(WebappsName));
foreach (SPSite siteCol in webapp.Sites)
{
siteUrl = siteCol.Url;
SPSite site = new SPSite(siteUrl);
SPWeb web = site.OpenWeb();
FullControlUsers = new StringBuilder();
foreach (SPRoleAssignment roleAssignment in web.RoleAssignments)
{
if (roleAssignment.Member is SPUser)
{
foreach (SPRoleDefinition roleDefinition in roleAssignment.RoleDefinitionBindings)
{
if (roleDefinition.Name.Equals("Full Control"))
{
if (roleAssignment.Member.LoginName != null)
{
if (roleAssignment.Member.LoginName.Contains('#') || roleAssignment.Member.LoginName.Contains('.'))
{
String ObjSplitUsers = roleAssignment.Member.Name.ToString();
//FullControlUsers.Append(ObjSplitUsers + ";");
SW.WriteLine(siteUrl.Replace(",", " ") + "," + site.Usage.Storage / (1024 * 1024) + "," + ObjSplitUsers + "," + roleAssignment.Member.LoginName);
}
else
{
String ObjSplitUsers = roleAssignment.Member.Name.ToString();
//FullControlUsers.Append(ObjSplitUsers + ";");
SW.WriteLine(siteUrl.Replace(",", " ") + "," + site.Usage.Storage / (1024 * 1024) + "," + ObjSplitUsers + "," + roleAssignment.Member.LoginName);
}
}
}
}
}
if (roleAssignment.Member is SPGroup)
{
foreach (SPRoleDefinition roleDefinition in roleAssignment.RoleDefinitionBindings)
{
if (roleDefinition.Name.Equals("Full Control"))
{ //ContributeUsers = new StringBuilder();
var group = (SPGroup)roleAssignment.Member;
foreach (SPUser user in group.Users)
{
if (user != null)
{
SW.WriteLine(siteUrl.Replace(",", " ") + "," + site.Usage.Storage / (1024 * 1024) + "," + user.Name + "," + user.Email);
}
}
}
}
}
}
}
}
SW.Close();
}
catch(Exception ex)
{
}
});
No comments:
Post a Comment