Advertisement
cansik

Project Server 2010 - Update CustomField

Feb 28th, 2012
1,364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.60 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using ProjectServerCSVImport.PSS.Project;
  6. using System.Net;
  7. using System.Security.Principal;
  8. using System.Data;
  9.  
  10. namespace ProjectServerCSVImport
  11. {
  12.     class DemoClass
  13.     {
  14.         public void UpdateCustomField()
  15.         {
  16.             //Creating a new service client object
  17.             ProjectSoapClient projectSvc = new ProjectSoapClient();
  18.  
  19.             //Just if you need to authenticate with another account!
  20.             projectSvc.ClientCredentials.Windows.ClientCredential = new NetworkCredential("test", "test", "demo");
  21.             projectSvc.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
  22.  
  23.             //Guid of my project
  24.             Guid myProjectId = new Guid("{610c820f-dc74-476c-b797-1e61a77ed6c6}");
  25.  
  26.             //Guid of the custom field
  27.             Guid myCustomFieldId = new Guid("{cd879634-b3ee-44eb-87f7-3063a3523f45}");
  28.  
  29.             //creating a new sessionId and a new jobId
  30.             Guid sessionId = Guid.NewGuid(); //the sessionId stays for the whole updating process
  31.             Guid jobId = Guid.NewGuid(); //for each job, you give to the server, you need a new one
  32.  
  33.             //indicator if you have to update the project
  34.             Boolean updatedata = false;
  35.  
  36.             //loading project data from server
  37.             //Every change on this dataset will be updated on the server!
  38.             ProjectDataSet project = projectSvc.ReadProject(myProjectId, DataStoreEnum.WorkingStore);
  39.  
  40.             //To find your custom field, you have to search for it in the CustomFieldsRow
  41.             foreach (ProjectServerCSVImport.PSS.Project.ProjectDataSet.ProjectCustomFieldsRow row in project.ProjectCustomFields)
  42.             {
  43.                 //check if the GUID is the same
  44.                 if (row.MD_PROP_UID == myCustomFieldId)
  45.                 {
  46.                     //if yes, write it into the container
  47.                     row.NUM_VALUE = 12345;
  48.  
  49.                     //and set the indicater
  50.                     updatedata = true;
  51.                 }
  52.             }
  53.  
  54.             //update if you have changed anything
  55.             if (updatedata)
  56.             {
  57.                 //check out the project first
  58.                 projectSvc.CheckOutProject(myProjectId, sessionId, "custom field update checkout");
  59.  
  60.                 //send the dataset to the server to update the database
  61.                 bool validateOnly = false;
  62.                 projectSvc.QueueUpdateProject(jobId, sessionId, project, validateOnly);
  63.  
  64.                 //wait 4 seconds just to be sure the job has been done
  65.                 System.Threading.Thread.Sleep(4000);
  66.  
  67.                 //create a new jobId to check in the project
  68.                 jobId = Guid.NewGuid();
  69.  
  70.                 //CheckIn
  71.                 bool force = false;
  72.                 string sessionDescription = "updated custom fields";
  73.                 projectSvc.QueueCheckInProject(jobId, myProjectId, force, sessionId, sessionDescription);
  74.  
  75.                 //wait again 4 seconds
  76.                 System.Threading.Thread.Sleep(4000);
  77.  
  78.                 /* now the data is in the database
  79.                  * but the gui want display the new value
  80.                  * so we have to publish the project to display
  81.                  * the new data.
  82.                  *
  83.                  * Maybe this is weird, but it works for me.
  84.                  */
  85.  
  86.                 //again a new jobId to publish the project
  87.                 jobId = Guid.NewGuid();
  88.                 bool fullPublish = true;
  89.                 projectSvc.QueuePublish(jobId, myProjectId, fullPublish, null);
  90.  
  91.                 //maybe we should wait again ;)
  92.                 System.Threading.Thread.Sleep(4000);
  93.             }
  94.         }
  95.  
  96.         /// <summary>
  97.         /// Returns the GUID for a specified project
  98.         /// and sets the guid for this class
  99.         /// </summary>
  100.         /// <param name="client">soap service client</param>
  101.         /// <param name="projectname">name of the project</param>
  102.         /// <returns>Project GUID</returns>
  103.         public Guid GetGuidByProjectName(ProjectSoapClient client, string projectname)
  104.         {
  105.             Guid pguid = new Guid();
  106.             ProjectDataSet data = client.ReadProjectList();
  107.  
  108.             foreach (DataRow row in data.Tables[0].Rows)
  109.             {
  110.                 if (row[1].ToString() == projectname) //compare - case sensitive!
  111.                 {
  112.                     pguid = new Guid(row[0].ToString());
  113.                 }
  114.             }
  115.  
  116.             return pguid;
  117.         }
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement