ProjectDocument.getResourceByIndexAsync method (JavaScript API for Office v1.1)
Asynchronously gets the GUID of the resource that has the specified index in the resource collection.
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.getResourceByIndexAsync(resourceIndex[, options][, callback]);
Parameters
resourceIndex
Type: number
The index of the resource in the collection of resources for the project. 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.
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 getResourceByIndexAsync 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 GUID of the resource as a string. |
Remarks
To get the maximum index of the collection of resources for the project, use the getMaxResourceIndexAsync method. A resource collection does not contain a resource at the 0 index.
Example
The following code example calls getMaxResourceIndexAsync to get the maximum index in the project's resource collection, and then calls getResourceByIndexAsync to get the GUID for each resource.
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 |