Read Site Groups using CSOM

Read Site Groups using CSOM

Read Site Groups using CSOM. In this post, I will give you code to Read Site Groups using CSOM. SharePoint group are a collection of resources. Each and every group has own permissions. In this code, you will get all SharePoint groups and resources also. You can also find the resources of every group with the help this code.[AdSense-A]

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security;
using Microsoft.ProjectServer.Client;
using Microsoft.SharePoint.Client;
using PRCLNT = Microsoft.ProjectServer.Client;

namespace Readsitegroupmember_Using_CSOM
{
    class Program
    {

        static string UserName = "OnlineUserName";
        static string Passwords = "Password";
        public static string OnlinePWA = "SiteURL";
        static void Main(string[] args)
        {
            using (PRCLNT.ProjectContext ProjectCont = new PRCLNT.ProjectContext(OnlinePWA))//PWA Url
            {
                SecureString passWord3 = new SecureString();
                foreach (char c in Passwords.ToCharArray()) passWord3.AppendChar(c);
                ProjectCont.Credentials = new SharePointOnlineCredentials(UserName, passWord3);
                ProjectCont.Load(ProjectCont.Web);
                ProjectCont.ExecuteQuery();
                ProjectCont.Load(ProjectCont.Web.SiteGroups);
                ProjectCont.ExecuteQuery();
                GroupCollection oSiteCollectionGroups = ProjectCont.Web.SiteGroups;
                int count = 0;
                foreach (Group oGroup in oSiteCollectionGroups)
                {
                    Console.WriteLine("Group Name: " + oGroup.Title);
                }
                ProjectCont.Load(oSiteCollectionGroups[count].Users);
                ProjectCont.ExecuteQuery();
                foreach (User oUser in oSiteCollectionGroups[count].Users)
                {
                    Console.WriteLine(string.Format("Name: {0}", oUser.Title));
                    ProjectCont.Load(ProjectCont.EnterpriseResources);
                    ProjectCont.ExecuteQuery();
                    if (ProjectCont.EnterpriseResources.Count() > 0)
                    {
                        var projectList = ProjectCont.LoadQuery(ProjectCont.EnterpriseResources);
                        ProjectCont.ExecuteQuery();
                        foreach (EnterpriseResource e in projectList)
                        {
                            if (e.Name == oUser.Title)
                            {
                               Console.WriteLine(string.Format("ID: {0}", e.Id, e.Name));
                            }
                        }
                    }

                }
            }
        }
        
    }
}

 


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

error: Content is protected !!