ProjectDocument.getSelectedTaskAsync method (JavaScript API for Office v1.0)

JavaScript API for Office v1.0

Asynchronously gets the GUID of the selected task.

Last modified: June 12, 2014

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

options

Type: object

Specifies the following optional parameters.

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.

callback

Type: object

A function with a parameter of type AsyncResult, which is invoked when the callback returns. Required.

When the function that you pass to the callback parameter executes, it receives an AsyncResult object that you can access from the parameter in the callback function. Use the AsyncResult.value property to get the GUID of the selected task. The GUID value is a string.

To determine the success or failure of the operation, use the AsyncResult.status property. If the operation fails, use the AsyncResult.error property to access an Error object that provides information about the error.

The GUID of a task is more useful in Project apps than the task ID number (for example, the ID of the first task in the Gantt chart is 1). The task GUID can be used to access task information in Project Server, such as tasks in a SharePoint project that is synchronized with Project Server in Visibility mode.

If you save the task GUID in a local variable, you can also use it as a parameter for the getTaskAsync method and the getTaskFieldAsync method.

Note Note

If no task is selected in one of the views that show tasks, for example the Gantt Chart view or the Task Usage view, the getSelectedTaskAsync function returns an error.

In the following example, the showTaskGuid function uses an anonymous function as the callback parameter. The callback function shows the text value of the GUID for the selected task.

In the following code, the textBox object is a reference to a textarea control in an HTML file, for example: <textarea id="textBox" rows="6" cols="25"></textarea>

var _projDoc;
var taskGuid;

// The initialize function is required for all apps.
Office.initialize = function (reason) {
    // Checks for the DOM to load using the jQuery ready function.
    $(document).ready(function () {
    // After the DOM is loaded, app-specific code can run.
    _projDoc = Office.context.document;
    });
}

function showTaskGuid() {
    _projDoc.getSelectedTaskAsync(
        function (asyncResult) {
            if (asyncResult.status === Office.AsyncResultStatus.Failed) {
                var error = asyncResult.error;
                textbox.value = error.name + ': ' + error.message;
            } else {
                var taskGuid = asyncResult.value;
                textbox.value = 'GUID of selected task:\n' + taskGuid;
            }
        }
    );
}

Supported clients

Project Standard 2013 and Project Professional 2013

Libraries

Office.js, Project-15.js

Namespace

Office

Show:
© 2014 Microsoft