Monday, December 23, 2013

Project Server - check if project is checked-out to current user

In my previous posts on Project Server, I wrote on 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.
Today, I will show how to check (with PSI) if currently logged-in user is equal to the user to which project is checked-out. This can be useful many times, for example, if something is to be done with the project data from code, but someone else has checked-out the project, and, in that case the current user must not be able to edit project data.

For this small piece of code, I am using FluentPS library, like it is mentioned in post that I linked at the top of this post. 

This is the code in c#:



public static bool IsProjectCheckedOutToCurrentUser(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 = "dev";
     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.WebSvcLookupTable.LookupTable svcLookupTable = psiSvcsFactory.CreateSvcClient<FluentPS.WebSvcLookupTable.LookupTable>();
         FluentPS.WebSvcCustomFields.CustomFields svcCustomFields = psiSvcsFactory.CreateSvcClient<FluentPS.WebSvcCustomFields.CustomFields>();

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


         FluentPS.WebSvcProject.ProjectDataSet _projEntities = svcProject.ReadProjectEntities(projectUid, 32, FluentPS.WebSvcProject.DataStoreEnum.WorkingStore);
         FluentPS.WebSvcProject.ProjectDataSet _project = svcProject.ReadProject(projectUid, FluentPS.WebSvcProject.DataStoreEnum.WorkingStore);

         try
         {
             Guid _checkOutUser = Guid.Empty;
             Guid _currentUser = Guid.Empty;
             using (var dsProject = svcProject.ReadProject(projectUid, DataStoreEnum.WorkingStore))
             {
                 var projectRow = dsProject.Project.FindByPROJ_UID(projectUid);
                 if (projectRow == null)
                     return false;

                 if(!projectRow.IsPROJ_CHECKOUTBYNull())
                     _checkOutUser = projectRow.PROJ_CHECKOUTBY;
             }


             using (FluentPS.WebSvcResource.ResourceDataSet resourceDs = svcResource.ReadUserList(FluentPS.WebSvcResource.ResourceActiveFilter.All))
             {
                 foreach (var item in resourceDs.Resources)
                 {
                     if (item.WRES_ACCOUNT.Contains(HttpContext.Current.User.Identity.Name))
                     {
                         _currentUser = item.RES_UID;
                     }
                 }
             }

             if (_checkOutUser == Guid.Empty || _currentUser == Guid.Empty)
                 return false;

             if (_checkOutUser == _currentUser)
                 return true;
             else
                 return false;
                
         }
         catch (Exception ex)
         {
             return false;
         }
     }
 }

No comments:

Post a Comment