Wednesday, April 10, 2013

Project Server - Restart workflow from code

When you want to restart workflow in Project Server, you can do that manually in Project Server in two ways:

1. When you open one of your project, in SharePoint Ribbon you will see "Options" button and its submenu "Restart Workflow". By clicking this option, you can restart your workflow, but you can not select the stage to which you want to restart workflow. This option will always restart workflow to the first stage, to the beginning. 




2. Second option better in sense that you can select particular stage to which you want to restart your workflow. You can also select multiple projects and some other options.

This option is located in: Server settings --> Change or Restart Workflows (in section Workflow and Project Detail Pages).




 Downside of this two options is that it has to be done manually, and that is not very convenient when you have to do this on large number of projects. So the best way to do this is from code behind.



 RESTART FROM CODE:

Workflow can be restarted to any stage using SubmitStage function from FluentPS library (which I described in one of my previous posts).

Here is the example of usage of that function:




public void RestartStage(Guid projectUID, string stageName)
{
      var logService = new LogService();

      var sessionService = new PSSessionService()
      {
           HostName = "serverName", // your PWA host name
           SiteName = "pwa" // your PWA site name
      };

      PsiContextService psiContextService = new PsiContextService();
      PSISvcsFactory psiSvcsFactory = new PSISvcsFactory(sessionService, psiContextService);

      Project svcProject = psiSvcsFactory.CreateSvcClient<Project>();
 
      Workflow svcWf = psiSvcsFactory.CreateSvcClient<Workflow>();

      SPSPagesService svcPage = new SPSPagesService(sessionService);
      SPSharepointService svcSP = new SPSharepointService(sessionService, svcPage, logService);
      PSWorkflowService svcPSWf= new PSWorkflowService(logService, svcWf, svcSP);

      WorkflowDataSet workflowDS = svcWf.ReadAvailableEnterpriseProjectTypes();
      List<PSWorkflowStatus> wfStatus = svcPSWf.GetProjectWorkflowStatus(projectUID);

      //Enteprise project template UID
      Guid _eptUID = new Guid();//

      //Stage UID
      Guid _stageGuid = new Guid();

      //Getting the stage GUID
      string _phaseName = "";

      for (int i = 0; i < wfStatus.Count; i++)
      {
           if (wfStatus[i].StageName == stageName)
           {
                _stageGuid = wfStatus[i].StageUid;
                _phaseName = wfStatus[i].PhaseName;
                break;
           }
      }
     
      //getting the enterprise project type GUID
      for (int i = 0; i < workflowDS.EnterpriseProjectType.Count; i++)
      {
          if (workflowDS.EnterpriseProjectType[i].ENTERPRISE_PROJECT_TYPE_NAME == _phaseName)
          {
                _eptUID = workflowDS.EnterpriseProjectType[i].ENTERPRISE_PROJECT_TYPE_UID;
                break;
          }
      }

           //RESTART to the stage
      Guid _restartGuid = svcPSWf.SubmitStage(projectUID, _eptUID, false, _stageGuid);
     
}

1 comment: