As part of our Flow Type Layout Reporting Consulting service, we received an interesting request. With Microsoft SharePoint in combination with Microsoft Word, you can open documents directly from your SharePoint website and load it into the local MS Word installation. After editing, the document can be saved back to the repository.

In order to maintain templates for reporting applications, this approach is perfect in order to give users the full power of thick-client applications in combination with centralized documents and templates.

We have created a very basic demo project that shows this procedure. The solution consists of two parts the Web application and a client application to edit the document. The client application will be registered with a specific protocol. The browser is looking for registered protocols when the user is clicking a link. The protocol is connected to an application through the registry database.

You might have already seen some hyperlink protocols such as mailto:, callto: or skype:. For this purpose, we are creating our own protocol called ClientApplication. The following reg file must be created and double-clicked to add the values to the database.

REGEDIT4

[HKEY_CLASSES_ROOT\ClientApplication]
@="URL:ClientApplication Protocol"
"URL Protocol"=""

[HKEY_CLASSES_ROOT\ClientApplication\shell]

[HKEY_CLASSES_ROOT\ClientApplication\shell\open]

[HKEY_CLASSES_ROOT\ClientApplication\shell\open\command]
@="\"C:\\ClientApplication.exe\" \"%1\""

You can download the registry file here: ClientApplication.reg

Make sure that the path C:\\ClientApplication.exe\ matches the path on your machine for the primary output of the ClientApplication.

More information about these protocols can be found in this MSDN article:

Registering an Application to a URI Scheme

  1. After loading the Visual Studio 2012 solution, compile and start the Web application. The website is very simple and basically shows only 3 links to documents that are stored on the server. Pay attention to the status bar when hovering over a link. Our custom protocol ClientApplication: is shown.

    Load and save documents from a server
  2. Click on any document. All browsers will show a similar message box asking to confirm that an application on your computer is allowed to open this file type.

    Load and save documents from a server

    Confirm with Allow in order to load the document.

  3. Thanks to the protocol, the browser starts our associated ClientApplication which loads the document with a simple HttpWebRequest.

    Load and save documents from a server
  4. Make some changes to the document and click the green Save back button.

    Load and save documents from a server

When you repeat the steps and load the same document again, you will notice that the changes have been saved properly.

The ClientApplication receives command line arguments which is basically the name of the document to load. Simple HttpWebRequests are used to get the stream which is converted into a byte array and then loaded into TX Text Control.

static void Main(string[] args)
{
        string sFileLocation = args[0].Substring("ClientApplication:".Length);

        // create a new httpp request and load the document into a byte array
        HttpWebRequest myHttpWebRequest =
                (HttpWebRequest)HttpWebRequest.Create("http://" + sFileLocation);
        HttpWebResponse myHttpWebResponse =
                (HttpWebResponse)myHttpWebRequest.GetResponse();

        System.IO.Stream stream = myHttpWebResponse.GetResponseStream();

        byte[] data = null;

        // convert the stream to an array
        var memoryStream = new MemoryStream();
        stream.CopyTo(memoryStream);
        data = memoryStream.ToArray();          

        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);

        // open the form and pass byte array and file location
        Application.Run(new Form1(data, sFileLocation));
}
private byte[] bDocument;
private string sFileLocation;

public Form1(byte[] data, string location)
{
        bDocument = data;
        sFileLocation = location;

        InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
        // load the document from the byte array
        textControl1.Load(bDocument,
                TXTextControl.BinaryStreamType.WordprocessingML);
}

Conclusion: Creating your own protocol is a very efficient way to load, modify and save documents directly from and to a Website using a thick-client Windows application. TX Text Control provides the required components for client-side and server-side processing.

Try it on your own and download the sample solution. At least a trial version of TX Text Control Server for ASP.NET (incl. Windows Forms) is required.