Partilhar via


Project Server 2010: Read groups and display user groups

Below sample code is to read all the group members and display the group names where the mentioned User name is present:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Data;

namespace CheckUserGroup
{
    class Program
    {
        static void Main(string[] args)
        {
            string resname = "User Name";
            Guid resguid = Guid.NewGuid();
            bool found = false;
            WebSvcSecurity.Security objSecurity = new WebSvcSecurity.Security();
            objSecurity.Credentials = CredentialCache.DefaultCredentials;
            objSecurity.Url = "https://servername/pwa/_vti_bin/psi/security.asmx";
            ResourceWS.Resource PSIResource = new ResourceWS.Resource();
            PSIResource.Url = "https://servername/pwa/_vti_bin/psi/resource.asmx";
            PSIResource.Credentials = CredentialCache.DefaultCredentials;

            ResourceWS.ResourceDataSet AllRes;
            AllRes = PSIResource.ReadUserList(ResourceWS.ResourceActiveFilter.Active);
            DataTable resultsres = (DataTable)AllRes.Tables[0];

            var result = from myRow in resultsres.AsEnumerable() where myRow.Field<string>("RES_NAME") == resname select myRow;
            foreach (DataRow row in result)
            {

                resguid = row.Field<Guid>("RES_UID");

            }

            WebSvcSecurity.SecurityGroupsDataSet GroupListDs = new WebSvcSecurity.SecurityGroupsDataSet();
           
            GroupListDs = objSecurity.ReadGroupList();
            foreach (WebSvcSecurity.SecurityGroupsDataSet.SecurityGroupsRow grow in GroupListDs.SecurityGroups)
            {
                WebSvcSecurity.SecurityGroupsDataSet GroupDs = new WebSvcSecurity.SecurityGroupsDataSet();
                GroupDs = objSecurity.ReadGroup(grow.WSEC_GRP_UID);
                DataTable results = (DataTable)GroupDs.GroupMembers;
                var res = from myRow in results.AsEnumerable() where myRow.Field<Guid>("RES_UID") == resguid select myRow;
                foreach (DataRow row in res)
                {
                    found = true;
                    Console.Write(grow.WSEC_GRP_NAME);

                }
               
            }

            if (!found)
            {
                Console.Write("User is not present in any groups");
            }

            Console.ReadLine();
        }
    }
}

Happy coding ......Cheers .. Ajith