static void Main(string[] args)
{
System.IO.StreamReader Sr = new System.IO.StreamReader(@"D:\Test\SitUrls.txt");
System.IO.StreamWriter ErrorSw = new System.IO.StreamWriter(@"D:\Test\ErrorLog.txt");
System.IO.StreamWriter Sw = new System.IO.StreamWriter(@"D:\Test\Owner.csv");
Sw.WriteLine("Site Url, Group Name, User Name, User Email");
while (!Sr.EndOfStream)
{
var urlsdetail = Sr.ReadLine();
string[] urldetails = urlsdetail.Split(';');
var url = urldetails[0];
var type = urldetails[1];
url = url.Trim();
using (ClientContext clientContext = new ClientContext(url))
{
Web oWebsite = clientContext.Web;
string web;
if(type.ToLower()=="online")
{
var login = "[email protected]";
var password = "XXXXX";
var securePassword = new SecureString();
foreach (char c in password)
{ securePassword.AppendChar(c); }
var onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
clientContext.Credentials = onlineCredentials;
web = url;
}
clientContext.Load(oWebsite, website => website.Title, website => website.Lists, website => website.ServerRelativeUrl);
clientContext.ExecuteQuery();
Microsoft.SharePoint.Client.GroupCollection groupColl = oWebsite.SiteGroups;
clientContext.Load(groupColl, groups => groups.Include(group => group.Title, group => group.Id));
clientContext.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.Group siteGroup in groupColl)
{
string groupName = siteGroup.Title;
UserCollection usersColl = siteGroup.Users;
clientContext.Load(usersColl, users => users.Include(user => user.Title, user => user.Email));
clientContext.ExecuteQuery();
foreach (User user in usersColl)
{
string userName = user.Title;
string email = user.Email;
Sw.WriteLine("\"" + urlsdetail + "\"," + "\"" + groupName + "\"," + "\"" + userName + "\"," + "\"" + email + "\"");
}
}
}
}
Sw.Close();
ErrorSw.Close();
Console.WriteLine("Executed Successfully");In this code we will learn how to generic CSOM code to call any version of sharepoint.
