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:

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

    document2.jpg
     
  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.

    document3.jpg
     
  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.

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

    document5.jpg
     
  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>

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

       
    <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>
     
    </head>
     
    <body>
       
    <button onclick="getWholeDocument()">get document</button>
         
    <span>outputs:</span><div id="message"></div>
     
    </body>
    </
    html>

     
  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.

    document6.jpg
     
  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.
       
    $(document).ready(
    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 {
               
    $(
    "#setDatabtn").remove();
           
    }
       
    });
    }

    // Writes data to current selection.

    function
    setData(dataToInsert) {
       
    Office.context.document.setSelectedDataAsync(dataToInsert);
    }

    // Reads data from current selection.

    function
    getData(elementIdToUpdate) {
       
    Office.context.document.getSelectedDataAsync(Office.CoercionType.Text,
       
    function (result) {
           
    if (result.status == "succeeded") {
               
    document.getElementById(elementIdToUpdate).value = result.value;
         
      }
       
    });
    }

    function
    getWholeDocument() {
       
    Office.context.document.getAllContentAsync(
           
    "text",
           
    function (result) {
               
    if (result.status == "failed") {
                   
    write(result.error.name +
    ": " + result.error.message);
               
    }
               
    else {
                   
    write(result.value);
               
    }
           
    });
    }

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

    function
    write(message) {
       
    document.getElementById(
    '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.

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

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

    document9.jpg
     
  15. The output window will be like this:

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

    document11.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all