Share via


Ewa.AsyncResult.getUserContext()

Applies to: apps for SharePoint | Excel Services | SharePoint Server 2013

In this article
Return Value
Remarks
Example

Gets the user context.

var value = Ewa.AsyncResult.getUserContext();

Return Value

Type: Object

Remarks

The [AsyncResult.getUserContext] method returns the same value that was passed into the asynchronous method call. As an example, consider the [Workbook.getRangeA1Async(addressA1,callback,userContext)] method where userContext is passed in as the last argument to the method call. The [userContext] parameter is provided as a way for callers to pass state through the asynchronous call. All asynchronous methods in the Excel Services JavaScript Object Model have the [userContext] parameter as the last parameter in their signatures.

Note

A null can be passed as the argument for the userContext parameter.

Example

The following code example adds a button and a text input box to the page and then adds code to the button onClick event that takes the range address entered into the text box (in A1 notation) and activates the specified range. The code uses the AsyncResult.getUserContext method to get the user state passed in to the callback method. The code example assumes that you are working with an Excel Web Access Web Part on SharePoint Server 2013.

<script type="text/javascript">
 
var ewa = null;
 
// Add event handler for onload event.
if (window.attachEvent) 
{ 
    window.attachEvent("onload", ewaOnPageLoad);    
} 
else 
{ 
    window.addEventListener("DOMContentLoaded", ewaOnPageLoad, false); 
}

// Add event handler for applicationReady event.
function ewaOnPageLoad() 
{ 
Ewa.EwaControl.add_applicationReady(getEwa); 
} 

function getEwa()
{        
    // Get a reference to the Excel Services Web Part.
    ewa = Ewa.EwaControl.getInstances().getItem(0);                                       
}              

function getRangeA1Button()
{
    // Get the A1 address entered.
    var a1Address = document.forms.aspnetForm.GetRangeAddress.value;
    
    // Call getRangeA1Async with the A1 address entered.
    ewa.getActiveWorkbook().getActiveSheet().getRangeA1Async(a1Address, getRangeA1Callback, null);    
}

function getRangeA1Callback(asyncResult)
{
    // getRangeA1Async returns a range object through the AsyncResult object
    var range = asyncResult.getReturnValue();
    
    // Activate the range that the user entered.
    // Pass in range as user context so it can be used in callback.
    range.activateAsync(0,2,activateAsyncCallback,range);    
}

function activateAsyncCallback(asyncResult)
{
    // activateAsync does not return a range through AsyncResult, so 
    // get the range from the user context passed in with activateAsync call.
    var range = asyncResult.getUserContext();
    
    // Display which address for which range was activated.
    window.status = "Range located at A1 address: " + range.getAddressA1() + " was activated.";
}

</script>
<input type="button" id="GetRange" VALUE="Get Range" onclick="getRangeA1Button()">
<input type="text" id="GetRangeAddress" value="Enter A1 Address">

See also

Reference

Ewa.AsyncResult Methods

Concepts

Ewa namespace