How To: Open a Word document from .NET Core web application, edit, and save back to server?

Viewed 1511

I am looking for modern examples on how to create a SharePoint-like integration with Microsoft Word but in an ASP/C# (.NET Core) web application. In other words, my goal is to click on a Word document from my webpage (file stored on my on-premise server), open in Word (desktop application), makes changes, and save back to the server. When I say SharePoint-like integration, I mean, opening and saving are handled automatically without the user having to be bothered by saving the file locally and manually uploading it back to the server.

I have found others asking the same question but with no concrete response and most were nearly a decade old. Here are some of the articles I found but of no help:

It looks like the Office Add-in platform (https://docs.microsoft.com/en-us/office/dev/add-ins/overview/office-add-ins#components-of-an-office-add-in) may be of use, but I don't necessarily want a Word add-in added to the ribbon. So not sure this is really what I want.

WebDAV may be what I need as discussed here (https://www.webdavsystem.com/ajaxfilebrowser/programming/opening-docs/open_save_docs_directly_to_server/) but not sure... and the article talks about Office 2007 so seems kind of dated.

Any help in guiding me to example, article, or forum that discusses current approaches to tackle this would greatly be appreciated.

A little more information: clients would be using Edge or Chrome browsers, on Windows 10 boxes, with Office 2016 (or later).

1 Answers

This question is very broad. There looks to be several parts.

  1. A plugin that can be configured to communicate with a webservice
  2. An Api that consumes data from the plugin
  3. The plugin should be able to save automatically

Here is some related documentation though some are specific to Excel, though may be able to be repurposed for Word

Also note that Office plugin development is JavaScript and/or TypeScript.

https://docs.microsoft.com/en-us/office/dev/add-ins/excel/custom-functions-web-reqs

https://docs.microsoft.com/en-us/office/dev/add-ins/excel/custom-functions-batching

https://docs.microsoft.com/en-us/javascript/api/word/word.document?view=word-js-preview#save__

There is not any code here so even writing a specific function would be making several assumptions, but from the documentation (with a couple add ins):

    // Run a batch operation against the Word object model.
    Word.run(function (context) {
    
    // Create a proxy object for the document.
    var thisDocument = context.document;

    // Queue a command to load the document save state (on the saved property).
    context.load(thisDocument, 'saved');    
    
    // Synchronize the document state by executing the queued commands, 
    // and return a promise to indicate task completion.
    return context.sync().then(function () {
        
        if (thisDocument.saved === false) {
            // Queue a command to save this document.
            thisDocument.save();
            
            // Synchronize the document state by executing the queued commands, 
            // and return a promise to indicate task completion.
            return context.sync().then(function () {
                // 
                // CODE to send your data to your server(s) via
                // API (post request, or something similar)
                //
                console.log('Saved the document');
            });
        } else {
            console.log('The document has not changed since the last save.');
        }
    });  
})
.catch(function (error) {
    console.log("Error: " + JSON.stringify(error));
    if (error instanceof OfficeExtension.Error) {
        console.log("Debug info: " + JSON.stringify(error.debugInfo));
    }
});
Related