Export (0) Print
Expand All

ProjectDocument.getTaskFieldAsync method (JavaScript API for Office)

apps for Office

Asynchronously gets the value of the specified field for the specified task.

Last modified: March 03, 2015

Hosts:

Project

Available in Requirement set

Selection

Added in

1.0

See all support details

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

Name

Type

Description

Support notes

taskId

string

The GUID of the task. Required.

fieldId

ProjectTaskFields

The ID of the target field. Required.

options

object

Specifies any of the following optional parameters

asyncContext

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.

callback

object

A function that is invoked when the callback returns, whose only parameter is of type AsyncResult.

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

For the getTaskFieldAsync 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 fieldValue property, which represents the value of the specified field.

First call the getSelectedTaskAsync method to get the task GUID, and then pass it as the taskId argument to getTaskFieldAsync. If the active view is not a task view (for example a Gantt Chart or Task Usage view), or if no task is selected in a task view, getSelectedTaskAsync returns a 5001 error (Internal Error). See addHandlerAsync method for an example that uses the ViewSelectionChanged event and the getSelectedViewAsync method to activate a button based on the active view type.

The following code example calls getSelectedTaskAsync to get the GUID of the task that's currently selected in a task view. Then it gets two task field values by calling getTaskFieldAsync recursively.

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 the task fields.
    function getTaskInfo() {
        getTaskGuid().then(
            function (data) {
                getTaskFields(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 the specified fields for the selected task.
    function getTaskFields(taskGuid) {
        var output = '';
        var targetFields = [Office.ProjectTaskFields.Priority, Office.ProjectTaskFields.PercentComplete];
        var fieldValues = ['Priority: ', '% Complete: '];
        var index = 0;
        getField();

        // Get each field, and then display the field values in the app.
        function getField() {
            if (index == targetFields.length) {
                for (var i = 0; i < fieldValues.length; i++) {
                    output += fieldValues[i] + '<br />';
                }
                $('#message').html(output);
            }

            // Get the field value. If the call is successful, then get the next field.
            else {
                Office.context.document.getTaskFieldAsync(
                    taskGuid,
                    targetFields[index],
                    function (result) {
                        if (result.status === Office.AsyncResultStatus.Succeeded) {
                            fieldValues[index] += result.value.fieldValue;
                            getField(index++);
                        }
                        else {
                            onError(result.error);
                        }
                    }
                );
            }
        }
    }

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

A checkmark (√) in the following matrix indicates that this method is supported in the corresponding Office host application. An empty cell indicates that the Office host application doesn't support this method.

For more information about Office host application and server requirements, see Requirements for running apps for Office.

Office for Windows desktop

Office for Windows RT

Office Online
(in browser)

Project

Available in requirement sets

Minimum permission level

ReadDocument

App types

Task pane

Library

Office.js

Namespace

Office

Version

Changes

1.0

Introduced

Show:
© 2015 Microsoft