4

How to get all the SharePoint 2013 Site Groups using C# Client Side Object Model

  1. I am using VS 2013 on my laptop and SP 2013 is on the network
  2. I read that I have to use Credentials or equivalent but dont know how

    namespace ConsoleApplication5
    {
        class Program
        {
            static void Main(string[] args)
            {
                // ClientContext - Get the context for the SharePoint Site
                // SharePoint site URL - http://servername/sites/CSOM
                ClientContext clientContext = new
                ClientContext("http://sp2013/");
                // Get the SharePoint web
                Web web = clientContext.Web;
                // Get all the site groups
                GroupCollection groupColl = web.SiteGroups;
                clientContext.Load(groupColl);
                // Execute the query to the server
                clientContext.ExecuteQuery();
    
                foreach (Group group in groupColl)
                {
                // Display the group name
                Console.WriteLine(group.Title);
                }
                Console.ReadLine();
            }
        }
    }   
    

3 Answers 3

1

You can do this by Passing NetworkCredentials as below

ClientContext clientContext = new ClientContext("http://sp2013/");
clientContext.Credentials = new NetworkCredential("UserName", "Password","Domain");
6
  • I did try that option now it gives me 403 forbidden error and this account is the site collection administrator and also belongs to the owner group also i noticed that in SP2013 I have enabled dual authentication would that be a problem ?
    – user17970
    May 11, 2015 at 18:21
  • It does not matter if I am developing on my client machine ? , the same code works in SP2010 and does not work in SP2013 that is very strange .I am not able to get the c# code to display even the title
    – user17970
    May 11, 2015 at 19:22
  • It seems like issue related to your server configuration May 11, 2015 at 19:28
  • Ok here is what i found out sp2010 if I give the site collection root , then it returns an error , sp2013 i created a subsite and i gave the url for the subsite and it still gives me the 403 forbidden error , i also ran the same code on dev box same error clientContext.Credentials = new NetworkCredential("testuser1", "Password1","mydomain"); <--- is this right ?
    – user17970
    May 11, 2015 at 19:37
  • it is just an analogy
    – user17970
    May 11, 2015 at 19:41
1

to answer my own question SP2010 is by default windows authentication and SP2013 is claims based the below code worked for me

       ClientContext clientContext = new ClientContext("http://sp2013:27898");
        clientContext.ExecutingWebRequest += clientContext_ExecutingWebRequest;
        Web web = clientContext.Web;
        GroupCollection groupColl = web.SiteGroups;
        UserCreationInformation userInfo = new UserCreationInformation();

        clientContext.Load(groupColl);
        clientContext.ExecuteQuery();
        foreach (Group group in groupColl)
        {
            // Display the group name
            Console.WriteLine(group.Title);
        }
        Console.ReadLine();





    }


    static void clientContext_ExecutingWebRequest(object sender, WebRequestEventArgs e)
    {
        try
        {
            e.WebRequestExecutor.WebRequest.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
        }
        catch
        {
            throw;
        }
    }
0
1

The only possible issue may be that SharePoint site will have different authentication configured.

Use below code and replace the authenticationMethod with your authentication method and it will work.

In my case authenticationMethod was NTLM

using (ClientContext clientContext = new ClientContext(siteUrl))
{
    CredentialCache myCache = new CredentialCache();
    myCache.Add(new Uri(siteUrl), "authenticationMethod", new NetworkCredential(userName, password));
    clientContext.Credentials = myCache;
    Group grp = null;
    GroupCollection groupColl = clientContext.Web.SiteGroups;
    clientContext.Load(groupColl,
    groups => groups.Include(
    group => group.Title,
    group => group.Id));
    clientContext.ExecuteQuery();
    foreach (Group siteGroup in groupColl)
    {
        console.writeLine(siteGroup.Title);
    }
}
1
  • how to export these groups into an excel or csv file?
    – samolpp2
    May 31, 2017 at 9:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.