Friday, November 6, 2009

Custom Workflow in Project Server 2007 - Part 2

For implementing this Workflow , we need to do following -
  1. Create a WSS Custom list.
  2. Create a Sharepoint user group for Approvers.
  3. Create a server side project publishing event handler.
  4. Create a custom workflow using Workflow Foundation (WF) and attach it to WSS list created in step 1.
  5. Deploy the solution


Step 1- Create a WSS Custom list
Create a WSS custom list at the root web of Project Web Access 2007 site.
  • Browse to the root Project Web Access 2007 site. http://server/pwa
  • Create a custom list(Site Actions ->Create) on root web – Name = Project Publishing;
  • Add following columns in 'Project Publishing' list -
    • Title = Single line of text – Holds the project name.
    • ProjectGuid = Single line of text = Holds the project ID that this item represents.
    • Status = Choice = Drop-down list for the approval status for this item. The choices are - Waiting for Approval, Rejected, and Approved.
    • Requester = Person or Group = Lists the user who requested the project publish.



Step 2 - Create a Sharepoint user group
Create the Publishing Approvers Sharepoint group on Project Web Access 2007 site.
  • Browse to the root Project Web Access 2007 site. http://server/pwa
  • Create the Publishing Approvers Sharepoint group(Site Actions - Site Settings -People and Groups -New menu -New Group) - Name= 'Publishing Approvers'
  • Add individual users in the group (New Menu -Add Users)


Step 3 - Create server side event receiver
We will create a single visual studio solution for step3 (Event Handler) & step 4 (Workflow).
  • Open Visual Studio .net 2005 and create a new project using 'Sequential Workflow Library template' - Name = EPMWF

  • Add following Project server & Sharepoint assembly references -

    Microsoft.Office.Project.Server.Events.dll
    Microsoft.Office.Project.Server.Library.dll
    Microsoft.SharePoint.dll
    Microsoft.SharePoint.Library.dll
    Microsoft.SharePoint.Security.dll
    Microsoft.SharePoint.WorkflowActions.dll

  • For Project Server dlls.Select the Browse tab in Add reference dialog and browse to the folder containing the Microsoft Office Project Server assemblies. Select them and click OK.
  • Delete Workflow1 file from Visual studio solution, which is created by default

  • Add following items in the visual studio project
    1. EPMWFConstants.cs - A static class that will hold constants needed by custom workflow & Event Handler.
    2. PublishingEventHandler.cs - A class file for handling project publishing event.
    3. PublishingWorkflow.cs - A Sequential Workflow item by Right click the project, select Add, and click on Sequential Workflow.

  • Coding EPMWFConstants.cs - This file will contain a static class that will hold constants needed for interfacing to WSS by the custom workflow & Event Handler.These constants will be string constants that will hold the custom list and columns details & Approver Group details. Add following constant in this class

    public const string CON_PublishListTitle = "Project Publishing";
    public const string CON_ProjectTitleColumn = "Title";
    public const string CON_ProjectGuidColumn = "GUID0";
    public const string CON_ApprovalStatusColumn = "Status";
    public const string CON_PublishRequestorColumn = "Requester";

    public const string CON_StatusApprovedText = "Approved";
    public const string CON_StatusRejectedText = "Rejected";
    public const string CON_StatusWaitingText = "Waiting for Approval";

    public const string CON_ApproversGroupName = "Publishing Approvers";


  • Coding PublishingEventHandler.cs - The event handler method first checks whether a list item for the project being published has already been created. If an item exists, the publish is allowed to proceed only if it has been approved. If the item does not exist, a new item is added, which initiates the custom workflow.

    • Check this for the structure of the class file for Event Handler.

    • Add following code in 'OnPublishing' method

      public override void OnPublishing(PSContextInfo contextInfo, ProjectPrePublishEventArgs e)
      {
      //Open PWA site Object
      using (SPSite site = new SPSite(contextInfo.SiteGuid))
      {
      //Open root web object
      using (SPWeb web = site.OpenWeb())
      {
      //create an object of the Publishing Approval custom list
      SPList list = web.Lists[EPMWFConstants.CON_PublishListTitle];

      string projectGuid = e.ProjectGuid.ToString("N").ToUpper();
      SPQuery query = new SPQuery();
      query.Query = "" + projectGuid + "";
      SPListItemCollection items = list.GetItems(query);
      SPListItem item;
      if (items == null items.Count == 0)
      {
      //Create a new list item
      item = list.Items.Add();
      item[EPMWFConstants.CON_ProjectGuidColumn] = projectGuid;
      item[EPMWFConstants.CON_ProjectTitleColumn] = e.ProjectName;
      item[EPMWFConstants.CON_ApprovalStatusColumn] = EPMWFConstants.CON_StatusWaitingText;
      item[EPMWFConstants.CON_PublishRequestorColumn] = web.Users[contextInfo.UserName];
      item.Update();
      }
      else
      {
      item = items[0];
      }

      //Cancel the publish event if the item isn’t approved
      switch ((string)item[EPMWFConstants.CON_ApprovalStatusColumn])
      {
      case EPMWFConstants.CON_StatusRejectedText:
      e.CancelReason = "Publishing for this project has been rejected.";
      e.Cancel = true;
      break;
      case EPMWFConstants.CON_StatusWaitingText:
      e.CancelReason = "Publishing for this project is pending approval.";
      e.Cancel = true;
      break;
      }
      }
      }
      }


For More details on Project server Event Handler check this article .


Part-3 : Create a custom workflow

No comments: