Export All Disabled users in a SharePoint Form:
Add App.Config files for All web Applications:App.config:
<appSettings>
<add key="WebAppsName" value="http://WebappUrl1|http://web01:81|http://communities|http://Sp2010"/>
</appSettings>
string EmailID = string.Empty; string siteUrl = string.Empty; string fullname = string.Empty;
SPSecurity.RunWithElevatedPrivileges(delegate
{
try
{
// For CSV.
StreamWriter SW;
SW = File.AppendText("c:\\UserInfoListReport.csv");
SW.WriteLine("Site collection URL, User, Full Name, E-Mail");
// Reading Webapplication url's from config file
String ConfigValues = ConfigurationSettings.AppSettings["WebAppsName"];
String[] ObjWebapps = ConfigValues.Split('|');
foreach (String WebappsName in ObjWebapps)
{
SPWebApplication webapp = SPWebApplication.Lookup(new Uri(WebappsName));
foreach (SPSite site in webapp.Sites)
{
foreach (SPWeb web in site.AllWebs)
{
siteUrl = web.Url;
SPSite Objsite = new SPSite(siteUrl);
SPWeb objweb = Objsite.OpenWeb();
SPGroupCollection ObjAllGrps = objweb.Groups;
String ObjUsername = String.Empty;
SPUser ValidsUser = null;
foreach (SPGroup ObjUsers in ObjAllGrps)
{
foreach (SPUser Objuser in ObjUsers.Users)
{
try
{
String ObjLoginName = Objuser.LoginName;
EmailID = Objuser.Email;
fullname = Objuser.Name;
if (ObjLoginName.Contains("|"))
{
ObjUsername = ObjLoginName.Split('|')[1].ToString();
}
else
{
ObjUsername = ObjLoginName;
}
ValidsUser = objweb.EnsureUser(ObjUsername);
}
catch (Exception ex)
{
if (ObjUsername.Contains(@"\"))
{
// Writing into CSV file
SW.WriteLine(Objsite.Url.Replace(",", " ") + "," + ObjUsername + "," + fullname + "," + EmailID);
}
else
{
}
}
}
}
}
}
}
SW.Close();
}
catch (Exception ex)
{
}
});
No comments:
Post a Comment