Check out my new blog at https://shibumiware.blogspot.com

Wednesday, January 28, 2009

mpFx: Simon Demo

Simon from Australia needed an example of how to use mpFx to create projects, resources, tasks, and assignments.  Here it is:

http://code.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=mpFx&DownloadId=4636.

Here is the source:

   1: using System;
   2: using System.Web.Services.Protocols;
   3: using Mcs.Epm.MicrosoftProject.mpFx;
   4: using Mcs.Epm.MicrosoftProject.mpFx.ProjectsWebService;
   5: using Mcs.Epm.MicrosoftProject.mpFx.ResourcesWebService;
   6: using Microsoft.Office.Project.Server.Library;
   7: using DataStoreEnum=Mcs.Epm.MicrosoftProject.mpFx.ProjectsWebService.DataStoreEnum;
   8: using Resource = Microsoft.Office.Project.Server.Library.Resource;
   9:  
  10: namespace EntityCreationDemo
  11: {
  12:     class Program
  13:     {
  14:         static void Main(string[] args)
  15:         {
  16:             try
  17:             {
  18:                 using (ProjectServer projectServer = new ProjectServer("http://projectserver/pwa", DataStoreEnum.WorkingStore, null))
  19:                 {
  20:                     string errorMessage;
  21:  
  22:                     Guid projectGuid = Guid.NewGuid();
  23:                     
  24:                     //  Create the project
  25:                     using (ProjectDataSet projectDataSet = EntityFactory.NewProject("Simon Test", projectGuid))
  26:                     {
  27:                         projectServer.Projects.Create(projectDataSet, false, true);
  28:                     }
  29:  
  30:                     // This is actually a bug of omission.  I should be updating the underlying project collection on creation.  
  31:                     // In order to use the current tech preview of mpFx, we brute force to refresh the projects collection 
  32:                     projectServer.Projects.Refresh();
  33:  
  34:                     //  Grab the project object
  35:                     EnterpriseProject project = projectServer.Projects[projectGuid];
  36:  
  37:                     Guid taskGuid;
  38:  
  39:                     //  Create the task
  40:                     project.CreateTask("Test Task",
  41:                                        DateTime.Now,
  42:                                        1000,
  43:                                        true,
  44:                                        true,
  45:                                        Guid.NewGuid(),
  46:                                        out taskGuid,
  47:                                        out errorMessage);
  48:  
  49:                     if (IsPsClientError(errorMessage))
  50:                     {
  51:                         return;
  52:                     }
  53:  
  54:                     Guid resourceGuid = Guid.NewGuid();
  55:  
  56:                     //  Create the resource
  57:                     using (ResourceDataSet resourceDataSet = EntityFactory.NewResource("Simon", Resource.Type.WorkResource, resourceGuid))
  58:                     {
  59:                         projectServer.Resources.Create(resourceDataSet, false, true);
  60:                     }
  61:  
  62:                     Guid assignmentGuid = Guid.NewGuid();
  63:  
  64:                     //  Create the assignment
  65:                     project.CreateAssignment(assignmentGuid, taskGuid, resourceGuid, true, true, true, Guid.NewGuid(), out errorMessage);
  66:  
  67:                     if (IsPsClientError(errorMessage))
  68:                     {
  69:                         return;
  70:                     }
  71:                 }
  72:             }
  73:             catch (MpFxException exception)
  74:             {
  75:                 Console.Write(Errors.ProcessMpFxException(exception));
  76:             }
  77:             catch (SoapException exception)
  78:             {
  79:                 Console.Write(Errors.ProcessMSProjectErrors(exception));
  80:             }
  81:             catch (Exception exception)
  82:             {
  83:                 Console.Write(exception.Message);
  84:             }
  85:  
  86:             Console.ReadKey();
  87:         }
  88:  
  89:         /// <summary>
  90:         /// Check the string returned by waitable calls to see if it is a a PSClientError
  91:         /// </summary>
  92:         /// <param name="errorMessage">The error message xml</param>
  93:         /// <returns></returns>
  94:         private static bool IsPsClientError(string errorMessage)
  95:         {
  96:             if (string.IsNullOrEmpty(errorMessage))
  97:             {
  98:                 return false;
  99:             }
 100:  
 101:             PSClientError clientError = new PSClientError(errorMessage);
 102:  
 103:             if (clientError.Count == 0)
 104:             {
 105:                 return false;
 106:             }
 107:  
 108:             Console.Write(Errors.FormatProjectError(clientError.GetAllErrors(), errorMessage));
 109:  
 110:             return true;
 111:         }
 112:     }
 113: }

No comments :

Disclaimer

Content on this site is provided "AS IS" with no warranties and confers no rights. Additionally, all content on this site is my own personal opinion and does not represent my employer's view in any way.