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

JavaScript API for Office v1.1

Asynchronously gets the value of the specified field for the specified resource in a resource view.

Last modified: September 17, 2014

Office.context.document.getResourceFieldAsync(resourceId, fieldId[, options][, callback]);

resourceId

The GUID of the resource. Required.

fieldId

The ID of the target field, as a ProjectResourceFields constant or its corresponding integer value. 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.

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

For the getResourceFieldAsync 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

Contains the fieldValue property, which represents the value of the specified field.

First call the getSelectedResourceAsync method to get the resource GUID, and then pass it as the resourceId argument to getResourceFieldAsync. If the active view is not a resource view (for example a Resource Usage or Resource Sheet view), or if no resource is selected in a resource view, getSelectedResourceAsync 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 getSelectedResourceAsync to get the GUID of the resource that's currently selected in a resource view. Then it gets three resource field values by calling getResourceFieldAsync 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(getResourceInfo);
        });
    };

    // Get the GUID of the resource and then get the resource fields.
    function getResourceInfo() {
        getResourceGuid().then(
            function (data) {
                getResourceFields(data);
            }
        );
    }

    // Get the GUID of the selected resource.
    function getResourceGuid() {
        var defer = $.Deferred();
        Office.context.document.getSelectedResourceAsync(
            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 resource.
    function getResourceFields(resourceGuid) {
        var targetFields =
            [Office.ProjectResourceFields.Name, Office.ProjectResourceFields.Units, Office.ProjectResourceFields.BaseCalendar];
        var fieldValues = ['Name: ', 'Units: ', 'Base calendar: '];
        var index = 0; 
        getField();

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

            // If the call is successful, get the field value and then get the next field.
            else {
                Office.context.document.getResourceFieldAsync(
                    resourceGuid,
                    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);
    }
})();

Supported clients

Project Standard 2013, Project Professional 2013, and Project 2013 SP1

Libraries

Office.js, Project-15.js

Namespace

Office

Show:
© 2014 Microsoft