Thursday, January 17, 2013

Project Server 2010 - Get group members emails

In one of my previous posts, I showed you how can you get email addresses of all members of a Project Server group using SQL query.

But, SQL query isn't practical if you want to use it in code behind, nor does Microsoft advise accessing project Servers Published database from code behind through SQL queries. It is always better to use PSI functions.

This is the code for accessing user emails using PSI functions:

  
public static bool GetMembersEmails(Guid projectUid, string pGroupName)
{
   var logService = new LogService();
   var sessionService = new PSSessionService()
   {
       HostName = "serverName", // your PWA host name
       SiteName = "pwa" // your PWA site name
   };

   // we need this user for LoginContext (it can be read from app settings)
   string _user = "Username";
   string _userPwd = "Pass";
   string _userDomain = "Domain";

   using (var _loginCtx = new LoginContext(_userDomain + "\\" + _user, _userPwd, sessionService))
   {

       sessionService.SetLoginContext(_loginCtx);
       FluentPS.Services.Impl.PsiContextService psiContextService = new PsiContextService();
       FluentPS.Services.Impl.PSISvcsFactory psiSvcsFactory = new PSISvcsFactory(sessionService, psiContextService);

       FluentPS.WebSvcProject.Project svcProject = psiSvcsFactory.CreateSvcClient<FluentPS.WebSvcProject.Project>();
       FluentPS.WebSvcResource.Resource svcResource = psiSvcsFactory.CreateSvcClient<FluentPS.WebSvcResource.Resource>();
       FluentPS.WebSvcSecurity.Security svcSecurity = psiSvcsFactory.CreateSvcClient<FluentPS.WebSvcSecurity.Security>();

       try
       {
            List<Guid> _userGuidList = new List<Guid>();
            List<string> _emailsList = new List<string>();

            Guid _groupGuid = Guid.Empty;
            using (var dsSecurityGroups = svcSecurity.ReadGroupList())       
            {
                 SecurityGroupsDataSet.SecurityGroupsDataTable _groups = dsSecurityGroups.SecurityGroups;
                 for (int i = 0; i < _groups.Count; i++)
                 {
                      if (_groups[i].WSEC_GRP_NAME == pGroupName.Trim().Split('@')[0])
                      {
                           _groupGuid = _groups[i].WSEC_GRP_UID;
                      }
                 }
                
if (_groupGuid.Equals(Guid.Empty))  return null;
            }

            using (var dsSecurityGroupsMem = svcSecurity.ReadGroup(_groupGuid))
            {
                 //read group members' uids
                 for (int i = 0; i < dsSecurityGroupsMem.GroupMembers.Count; i++)
                 {
                      Guid _userGuid = dsSecurityGroupsMem.GroupMembers[i].RES_UID;
                      _userGuidList.Add(_userGuid);
                 }
            }

            foreach (Guid _resUid in _userGuidList)
            {
                 using (var dsResource = svcResource.ReadResource(_resUid))
                 {
                      var resourceRow = dsResource.Resources.FindByRES_UID(_resUid);
                      _emailsList.Add(resourceRow.WRES_EMAIL);                                         }
            }

            return _emailsList;
 
       }
       catch (Exception ex)
       {
            return false;
       }
   }
}

1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete