ProjectDocument.getSelectedTaskAsync method

Asynchronously gets the GUID of the selected task in a task view.

Hosts: Project
Available in Requirement set Selection
Added in 1.0
Office.context.document.getSelectedTaskAsync([options,] [callback]);

Parameters

Name Type Description Support notes
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.

Callback Value

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

For the getSelectedTaskAsync method, the returned AsyncResult object contains the 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 The GUID of the selected task as a string.

Remarks

The GUID of a task is more useful in Project add-ins 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 Project task information, such as tasks in a SharePoint project that is synchronized with Project Server in Visibility mode. You can also save the task GUID in a local variable and use it for the getTaskAsync and getTaskFieldAsync methods.

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.

Example

The following code example calls getSelectedTaskAsync to get the GUID of the task that's currently selected in a task view. Then it gets task properties by calling getTaskAsync.

The example assumes your add-in 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, add-in-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 add-in.
    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);
    }
})();

    


Support details

A capital Y 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 Office Add-ins.

Office for Windows desktop Office Online (in browser)
Project Y
Available in requirement sets Selection
Minimum permission level ReadDocument
Add-in types Task pane
Library Office.js
Namespace Office

Support history


Version Changes
1.0 Introduced

See also

Other resources

getTaskAsync method

AsyncResult object

ProjectDocument object