Using GetAllContentAsync Method of Document Object in Visual Studio 2012

The getAllContentAsync method is used to return the entire document in the form of text or Base64 encoded form. This function contains some parameters which are described below:

  • Type of file : It can be "text" or "base 64". It defines the type of structure of data to be returned.
  • asyncContext : It defines the user-defined type, whether array type or boolean type or number type etc., to be be returned in the AsyncResult object.
  • Callback : It is a function called to return the result.

Let's have a look at the following steps to use this function:

  1. First go to Visual Studio 2012 RC.
  2. Now click on the File menu and choose New Project.
  3. A New Project Dialog Box opens like this:

  4. In this just expand the Visual C# node and under it just expand the Office/SharePoint node.

  5. Now select the Apps option from it and select the App for Office 2013 in the center pane and give a name to the application and click the ok button.

  6. The Create App for office dialog box will be displayed. In it by default the Task pane app option is selected and just click on Finish button.

  7. Now Visual Studio creates the project and it will display in Solution Explorer.

  8. Now develop the application and to design the appearance of the app we will add HTML code in the default page of the project.
  9. In this just remove the HTML code under the body tag and add the following code to add the button in the document:

    <!DOCTYPE html>

    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=Edge"/>

    <link rel="stylesheet" type="text/css" href="../Content/Office.css" />
    <link rel="stylesheet" type="text/css" href="../Content/App.css" />

    <script src="../Scripts/jquery-1.6.2.js"></script>
    <script src="../Scripts/Office/MicrosoftAjax.js"></script>
    <script src="../Scripts/Office/Office.js"></script>

    <!-- Add your JavaScript to the following file -->
    <script src="../Scripts/Document.js"></script>
    <button onclick="getWholeDocument()">get document</button>
    <span>outputs:</span><div id="message"></div>

  10. Now to use the getAllContentAsync function we will expand the Scripts option in the Solution Explorer and under this just expand the Office option and under it just open the document.js to show the default JavaScript file for the application.

  11. Now add the getAllContentAsync() function to show its use in the application as follows:

    // Add any initialization logic to this function.

    Office.initialize = function (reason) {

    // Checks for the DOM to load.
    function () {
    "#getDatabtn").click(function () { getData("selectedData"); });

    // Checks if setSelectedDataAsync is supported and adds appropriate click handler
    if (Office.context.document.setSelectedDataAsync) {
    "#setDatabtn").click(function () { setData("Sample data"); });
    else {

    // Writes data to current selection.

    setData(dataToInsert) {

    // Reads data from current selection.

    getData(elementIdToUpdate) {
    function (result) {
    if (result.status == "succeeded") {
    document.getElementById(elementIdToUpdate).value = result.value;

    getWholeDocument() {
    function (result) {
    if (result.status == "failed") {
    write( +
    ": " + result.error.message);
    else {

    // Function that writes to a div with id='message' on the page.

    write(message) {
    'message').innerText += message;

  12. Now select the project name in the Solution Explorer itself  and select the start Action property in the property window like this.

  13. Now just select the Microsoft Word from the dropdownlist as shown below.

  14. Now press F5 or click Start Debugging in the Debug menu to run the application.

  15. The output window will be like this:

  16. In this if we write something in the document then it will return the entire document like this: