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

Asynchronously gets the maximum index of the collection of resources in the current project. Important: This API works only in Project 2016 on Windows desktop.

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

Parameters

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.

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 getMaxResourceIndexAsync 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 The highest index number in the current project's resource collection.

Remarks

You can use the returned value with the getResourceByIndexAsync method to get resource GUIDs. A resource collection does not contain a resource at the 0 index.

Example

The following code example calls getResourceTaskIndexAsync to get the maximum index of the collection of resources in the current project. Then it uses the returned value and the getResourceByIndexAsync method to get each resource GUID.

The example assumes that 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";
    var resourceGuids = [];

    // 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.
            app.initialize();
            $('#get-info').click(getResourceInfo);
        });
    };

    // Get the maximum resource index, and then get the resource GUIDs.
    function getResourceInfo() {
        getMaxResourceIndex().then(
            function (data) {
                getResourceGuids(data);
            }
        );
    }

    // Get the maximum index of the resources for the current project.
    function getMaxResourceIndex() {
        var defer = $.Deferred();
        Office.context.document.getMaxResourceIndexAsync(
            function (result) {
                if (result.status === Office.AsyncResultStatus.Failed) {
                    onError(result.error);
                }
                else {
                    defer.resolve(result.value);
                }
            }
        );
        return defer.promise();
    }

    // Get each resource GUID, and then display the GUIDs in the add-in.
    // There is no 0 index for resources, so start with index 1.
    function getResourceGuids(maxResourceIndex) {
        var defer = $.Deferred();
        for (var i = 1; i <= maxResourceIndex; i++) {
            getResourceGuid(i);
        }
        return defer.promise();
        function getResourceGuid(index) {
            Office.context.document.getResourceByIndexAsync(index,
                function (result) {
                    if (result.status === Office.AsyncResultStatus.Succeeded) {
                        resourceGuids.push(result.value);
                        if (index == maxResourceIndex) {
                            defer.resolve();
                            $('#message').html(resourceGuids.toString());
                        }
                    }
                    else {
                        onError(result.error);
                    }
                }
            );
        }
    }
    function onError(error) {
        app.showNotification(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
Minimum permission level ReadDocument
Add-in types Task pane
Library Office.js
Namespace Office

Support history


Version Changes
1.1 Introduced

See also

Other resources

getResourceByIndexAsync

AsyncResult object

ProjectDocument object