Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using ProjectServerCSVImport.PSS.Project;
- using System.Net;
- using System.Security.Principal;
- using System.Data;
- namespace ProjectServerCSVImport
- {
- class DemoClass
- {
- public void UpdateCustomField()
- {
- //Creating a new service client object
- ProjectSoapClient projectSvc = new ProjectSoapClient();
- //Just if you need to authenticate with another account!
- projectSvc.ClientCredentials.Windows.ClientCredential = new NetworkCredential("test", "test", "demo");
- projectSvc.ClientCredentials.Windows.AllowedImpersonationLevel = TokenImpersonationLevel.Impersonation;
- //Guid of my project
- Guid myProjectId = new Guid("{610c820f-dc74-476c-b797-1e61a77ed6c6}");
- //Guid of the custom field
- Guid myCustomFieldId = new Guid("{cd879634-b3ee-44eb-87f7-3063a3523f45}");
- //creating a new sessionId and a new jobId
- Guid sessionId = Guid.NewGuid(); //the sessionId stays for the whole updating process
- Guid jobId = Guid.NewGuid(); //for each job, you give to the server, you need a new one
- //indicator if you have to update the project
- Boolean updatedata = false;
- //loading project data from server
- //Every change on this dataset will be updated on the server!
- ProjectDataSet project = projectSvc.ReadProject(myProjectId, DataStoreEnum.WorkingStore);
- //To find your custom field, you have to search for it in the CustomFieldsRow
- foreach (ProjectServerCSVImport.PSS.Project.ProjectDataSet.ProjectCustomFieldsRow row in project.ProjectCustomFields)
- {
- //check if the GUID is the same
- if (row.MD_PROP_UID == myCustomFieldId)
- {
- //if yes, write it into the container
- row.NUM_VALUE = 12345;
- //and set the indicater
- updatedata = true;
- }
- }
- //update if you have changed anything
- if (updatedata)
- {
- //check out the project first
- projectSvc.CheckOutProject(myProjectId, sessionId, "custom field update checkout");
- //send the dataset to the server to update the database
- bool validateOnly = false;
- projectSvc.QueueUpdateProject(jobId, sessionId, project, validateOnly);
- //wait 4 seconds just to be sure the job has been done
- System.Threading.Thread.Sleep(4000);
- //create a new jobId to check in the project
- jobId = Guid.NewGuid();
- //CheckIn
- bool force = false;
- string sessionDescription = "updated custom fields";
- projectSvc.QueueCheckInProject(jobId, myProjectId, force, sessionId, sessionDescription);
- //wait again 4 seconds
- System.Threading.Thread.Sleep(4000);
- /* now the data is in the database
- * but the gui want display the new value
- * so we have to publish the project to display
- * the new data.
- *
- * Maybe this is weird, but it works for me.
- */
- //again a new jobId to publish the project
- jobId = Guid.NewGuid();
- bool fullPublish = true;
- projectSvc.QueuePublish(jobId, myProjectId, fullPublish, null);
- //maybe we should wait again ;)
- System.Threading.Thread.Sleep(4000);
- }
- }
- /// <summary>
- /// Returns the GUID for a specified project
- /// and sets the guid for this class
- /// </summary>
- /// <param name="client">soap service client</param>
- /// <param name="projectname">name of the project</param>
- /// <returns>Project GUID</returns>
- public Guid GetGuidByProjectName(ProjectSoapClient client, string projectname)
- {
- Guid pguid = new Guid();
- ProjectDataSet data = client.ReadProjectList();
- foreach (DataRow row in data.Tables[0].Rows)
- {
- if (row[1].ToString() == projectname) //compare - case sensitive!
- {
- pguid = new Guid(row[0].ToString());
- }
- }
- return pguid;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement