tirsdag den 25. marts 2014

Update Project from App Part on PDP-page

I have lately been working on an App for Project Server Online and in this App I included an App part. An App part to be included on a PDP-page, which to me seems to be the purpose of an App part in a Project Server App. The data entered in this App part should update the current project.
To find out how to do that also when the project manager had checked out the project turned out not to be easy at all. But in the following I will show sample code on how to write back to Project Server from a App part.
First I start getting all the project custom fields.
projContext = PS.ProjectContext.get_current();
customFields = projContext.get_customFields();
projContext.load(customFields);

// Run the request on the server.
projContext.executeQueryAsync(getCFComplete, getCFFailed);

function getCFFailed(sender, args) {
      alert("");
}

function getCFComplete(response) {
       var cfEnumerator = customFields.getEnumerator();
       // Save the details of each CF for later
       while (cfEnumerator.moveNext()) {
              var cf = cfEnumerator.get_current();
              customFieldData.push({
                     Id: cf.get_id(),
                     Name: cf.get_name(),
                     InternalName: cf.get_internalName()
              });
       }
       // Now get all projects
       GetProjects();
}

Now we have all the custom fields and is ready to get all the projects.

function GetProjects() {
      projects = projContext.get_projects();
      projContext.load(projects, "Include( Name, CreatedDate, Id, IsCheckedOut )");
      projContext.executeQueryAsync(updateProject, getCFFailed);
 }

In the following function, I have chosen a specific project to update and a specific custom field.

function updateProject() {
      var projectId = "b028d0ef-b597-e311-a659-00155d786a13";
      var projectEnumerator = projects.getEnumerator();
      var project;
      while (projectEnumerator.moveNext()) {
            var proj = projectEnumerator.get_current();
            if (proj.get_id() == projectId) {
                  project = proj;
            }
      }
      var isCheckOut = project.get_isCheckedOut();
      var checkInAgain = false;
      var draftProject;
      if (isCheckOut) {
            draftProject = project.get_draft();
      } else {
            draftProject = project.checkOut();
            checkInAgain = true;
      }
      var fieldName = "DemoTest";

      var publishJob = draftProject.publish(checkInAgain);
      //Monitor the job
      projContext.waitForQueueAsync(publishJob, 30, function(response) {
            if (response !== 4) {
                  alert(response);

      }

      });

}

That’s it. J

 


Ingen kommentarer:

Tilføj en kommentar