Powershell script for Creating Phase, Stage, PDPs in EPM 2013
As we all know that PowerShell is a very strong tool for performing task in Windows environment. In this blog post I'm assuming you have basic knowledge of PowerShell scripting and Project Server 2013 as we are here to learn how to create Stages, Phases and PDPs in Enterprise Project Management 2013 (Project Server 2013). For Windows PowerShell: Scripting Crash Course is a very good article written by Don Jones in MSDN.
People who have worked on custom workflows using Visual Studio in Project server must know that it uses unique id of each stage, which is automatically generated when the stages were created. So for moving workflow from one environment to another, we are required to the solution and update the Stage id of new environment. For every environment changing code and updating Stage id is not a feasible solution.
To overcome the above problem we have come across the solution of creating Stages programmatically and assigning our own Guids. And that is possible using code only not in UI. So in this post I'm going to explain you how to create Stages, Phases and PDPs programmatically using PowerShell Scripts.
We need some configuration files that will contain meta-data used during creation process e.g. Stage Name, Stage Uid, Phase Name etc.
Sample config files look like the below images:
Stage Config: Contains Stage name, StageUid, Phase name and workflow status PDP (default landing page for Stage).
We need another config file for storing list of visible PDPs in any stage
We need another config file for storing list of visible PDPs in any stage
Creating Project Detail Pages
Now since we have all the configuration files we'll start with some codes in powershell. As we are going to use client object model in powershell so we need to reference some dlls
We'll start with the creation of PDP first then Phases and lastly Stages. So, for creating PDP we need the SharePoint context of PWA site. Below script will return context object:
PDPs are nothing but webpart pages which are stored in "Project Detail Pages" library in SharePoint. So first we'll create the context object of library and create webpart pages using config file.
For creating web part page we have a stored template so now we'll read that template file and PDP Config file using below code snippet:
Now we'll iterate through the rows read from the config file and create project detail pages. You can see in the code we have used FileCreationInformation class and ListItem class of Client object model to create web part page. Once the web part page created in library we have updated the meta-data values that we have read from config.
Above code sounds familiar as it's a simple SharePoint client object model in Powershell.
We'll start with the creation of PDP first then Phases and lastly Stages. So, for creating PDP we need the SharePoint context of PWA site. Below script will return context object:
PDPs are nothing but webpart pages which are stored in "Project Detail Pages" library in SharePoint. So first we'll create the context object of library and create webpart pages using config file.
For creating web part page we have a stored template so now we'll read that template file and PDP Config file using below code snippet:
Now we'll iterate through the rows read from the config file and create project detail pages. You can see in the code we have used FileCreationInformation class and ListItem class of Client object model to create web part page. Once the web part page created in library we have updated the meta-data values that we have read from config.
Above code sounds familiar as it's a simple SharePoint client object model in Powershell.
Creating Phases
In next step we are going to create Phases by using Project server client object model. As you can see above we have reference to "Microsoft.ProjectServer.Client.dll" so now we are going to use it to create Phases and Stages. first we have to get the context of Project Server and then read the phase config file for creating Phases. Once we have project server context we can access objects of Project Server i.e. Phases, Stages, PDPs etc. So are loading Phase, Stage and PDP collection objects for later use.
We have Phase collection object, now we'll create Phase and add it to collection. For creating Phases we are going iterate through the rows return from config file and create Phase using PhaseCreationInformation class of Project Server Client object model by providing Phase name and description from the values returned from config file.
Till now we have successfully created PDPs and Phases using PowerShell scripts. Both PDPs and Phases are required before creating Stages.
We have Phase collection object, now we'll create Phase and add it to collection. For creating Phases we are going iterate through the rows return from config file and create Phase using PhaseCreationInformation class of Project Server Client object model by providing Phase name and description from the values returned from config file.
Till now we have successfully created PDPs and Phases using PowerShell scripts. Both PDPs and Phases are required before creating Stages.
Creating Stages and associating PDPs with Stages
In next step we are going to create Stages by using Phases and PDPs created in previous steps. For creating stages we need configuration values from StageConfig and StagePDP config.
You can see in above code we have used StageCreationInformation class of Project server client object model, which takes information from config files for creating Stages. But in the PhaseId field it need Guid of Phase so we have created a function to fetch Guid of Phase using Phase name.
And for ProjectDetailPages field we need a collection of StageDetailPageCreationInformation object. Since we have StagePDP config so we can read the rows from that config and create this collection. So for getting this collection we have created GetPDPCreationInfo function and passed $csvStagePDP $objPDPCollection $objStage.StageName as parameter. In this function we have used StageDetailPageCreationInformation class object which takes values from StagePDP config and fetch PDP Guid from another function GetPDPGuid and return desired collection
For WorkflowStatusPageId field we needed the PDP guid of Status page which we have fetched using GetPDPGuid function. We have used the Guid from the Stage config file and set it as Id of stage which will now eliminate the chances of automatically assignment of unique id to Stages. Now since we have set all the required field of StageCreationInformation object we can create the Stage.
You can see in above code we have used StageCreationInformation class of Project server client object model, which takes information from config files for creating Stages. But in the PhaseId field it need Guid of Phase so we have created a function to fetch Guid of Phase using Phase name.
And for ProjectDetailPages field we need a collection of StageDetailPageCreationInformation object. Since we have StagePDP config so we can read the rows from that config and create this collection. So for getting this collection we have created GetPDPCreationInfo function and passed $csvStagePDP $objPDPCollection $objStage.StageName as parameter. In this function we have used StageDetailPageCreationInformation class object which takes values from StagePDP config and fetch PDP Guid from another function GetPDPGuid and return desired collection
For WorkflowStatusPageId field we needed the PDP guid of Status page which we have fetched using GetPDPGuid function. We have used the Guid from the Stage config file and set it as Id of stage which will now eliminate the chances of automatically assignment of unique id to Stages. Now since we have set all the required field of StageCreationInformation object we can create the Stage.
Now you have seen how we can use Client object model in PowerShell script to perform our tasks, So you can write your own scripts and solve your projects of doing manual work multiple times for every environment instead create a script and run in every environment. You can download full script from my Project at codeplex. Enjoy the world of PowerShell scripting, Happy Coding😊😊
This comment has been removed by a blog administrator.
ReplyDelete