Export (0) Print
Expand All

ProjectDocument.getTaskAsync method (JavaScript API for Office v1.1)

JavaScript API for Office v1.1

Asynchronously gets the specified task's name, assigned resources, and the ID of the task in the synchronized SharePoint task list.

Last modified: September 18, 2014

Office.context.document.getTaskAsync(taskId [,options][, callback]);

taskId

The GUID of the task. Required.

options

The following optional parameter:

asyncContext

Type: array, boolean, null, number, object, string, or undefined

A user-defined item of any type that is returned in the AsyncResult object without being altered. Optional.

For example, you can pass the asyncContext argument by using the format {asyncContext: 'Some text'} or {asyncContext: <object>}.

callback

Type: function

A function that is invoked when the method call returns, where the only parameter is of type AsyncResult. Optional.

When the callback function executes, it receives an AsyncResult object that you can access from the parameter in the callback function.

For the getTaskAsync method, the returned AsyncResult object contains following properties:

Name

Description

asyncContext

The data passed in the optional asyncContext parameter, if the parameter was used.

error

Information about the error, if the status property equals failed.

status

The succeeded or failed status of the asynchronous call.

value

Contains the following properties:

  • The taskName property is the name of the task.

  • The wssTaskId property is the ID of the task in the synchronized SharePoint task list. If the project is not synchronized with a SharePoint task list, the value is 0.

  • The resourceNames property is the comma-separated list of the names of resources that are assigned to the task.

Before you call the getTaskAsync method, call the getSelectedTaskAsync method to get the GUID of the task.

The following code example calls getSelectedTaskAsync to get the task GUID of the currently selected task. Then it calls getTaskAsync to get the properties for the task that are available from the JavaScript API for Office.

The example assumes your app has a reference to the jQuery library and that the following page controls are defined in the content div in the page body:

<input id="get-info" type="button" value="Get info" /><br />
<span id="message"></span>
(function () {
    "use strict";

    // The initialize function must be run each time a new page is loaded.
    Office.initialize = function (reason) {
        $(document).ready(function () {

            // After the DOM is loaded, app-specific code can run.
            $('#get-info').click(getTaskInfo);
        });
    };

    // Get the GUID of the task, and then get local task properties.
    function getTaskInfo() {
        getTaskGuid().then(
            function (data) {
                getTaskProperties(data);
            }
        );
    }

    // Get the GUID of the selected task.
    function getTaskGuid() {
        var defer = $.Deferred();
        Office.context.document.getSelectedTaskAsync(
            function (result) {
                if (result.status === Office.AsyncResultStatus.Failed) {
                    onError(result.error);
                }
                else {
                    defer.resolve(result.value);
                }
            }
        );
        return defer.promise();
    }

    // Get local properties for the selected task, and then display it in the app.
    function getTaskProperties(taskGuid) {
        Office.context.document.getTaskAsync(
            taskGuid,
            function (result) {
                if (result.status === Office.AsyncResultStatus.Failed) {
                    onError(result.error);
                }
                else {
                    var taskInfo = result.value;
                    var output = String.format(
                        'Name: {0}<br/>GUID: {1}<br/>SharePoint task ID: {2}<br/>Resource names: {3}',
                        taskInfo.taskName, taskGuid, taskInfo.wssTaskId, taskInfo.resourceNames);
                    $('#message').html(output);
                }
            }
        );
    }

    function onError(error) {
        $('#message').html(error.name + ' ' + error.code + ': ' + error.message);
    }
})();

Supported clients

Project Standard 2013, Project Professional 2013, and Project 2013 SP1

Libraries

Office.js, Project-15.js

Namespace

Office

Show:
© 2014 Microsoft