Monday, December 30, 2013

Project Server - check if project is approved

This is my new post in my Project Server series. In this post, I've written short c# code (using PSI) on how to check if project in Project Server is already approved.

This short code comes in handy when you need to do some action only after the project is approved.

Other posts regarding Project Server PSI functions: how to use PSI in Project Server in simple way, how to get values from Custom Fields from code with PSI, how to restart workflow from code using PSI, get members of Project Server group using PSI, change EPT using PSI and how to update Custom Field using PSI.


This is the C# code:



public static bool IsProjectApproved(Guid projectUid)
{   
    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))
    {
         try
         {
              PsiContextService psiContextService = new PsiContextService();
              PSISvcsFactory psiSvcsFactory = new PSISvcsFactory(sessionService, psiContextService);

              Workflow _wfSvc = psiSvcsFactory.CreateSvcClient<Workflow>();
 
              WorkflowDataSet dtWf = _wfSvc.ReadWorkflowStatus(projectUid, true);

              // _max is the last ORDER of the Status in DB
              int _max = -1;
              bool _status = false;

              foreach (var item in dtWf.WorkflowStatus)
              {
                   if (item.STAGE_ORDER > _max)
                   {
                        _max = item.STAGE_ORDER;
                        if (item.STAGE_INFO.ToLower().Contains("project approved"))
                             _status = true;
                        else
                             _status = false;
                   }
              }
              return _status;
         }
         catch (Exception)
         {
              return false;
         }
               
    }
       
}

No comments:

Post a Comment