Sharepoint - Using C# to open a word doc file stored in Sharepoint document library

13,027

This could potentially be a lot harder than it sounds. If you're trying to do this from the server-side object model, then you'll most likely be running in session 0. Session 0 is a special Windows session for services, which doesn't permit the spawning of new processes. To get around this, you'd have to get out of session 0 by using the Windows API to log a user in and then take control of their session. I'll go no further down this path, since you've not specified that you're actually doing this.

If you're just looking to download a word document stored in a document library on SharePoint and then start it on the client side, you can use the following to download and open the document:

ClientContext clientContext = new ClientContext("http://SharePoint/");
string serverRelativeUrlOfFile = "SharedDocuments/file.docx";
string fileDestinationPath = @"C:\Documents\file.docx";

using (FileInformation sharePointFile =
    Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, serverRelativeUrlOfFile))
{
    using (Stream destFile = System.IO.File.OpenWrite(fileDestinationPath))
    {
        byte[] buffer = new byte[8 * 1024];
        int byteReadInLastRead;
        while ((byteReadInLastRead = sharePointFile.Stream.Read(buffer, 0, buffer.Length)) > 0)
        {
            destFile.Write(buffer, 0, byteReadInLastRead);
        }
    }
}


System.Diagnostics.Process.Start(@"C:\Documents\file.docx");

EDIT: Note, this uses the SharePoint2010 Client object model, as the steps for the server-side object model are made considerably harder by session 0 isolation, and I won't detail that here unless it's requested.

EDIT: After the comments, it's clear that you're trying to use a Webpart to download and execute a file on the client side. The above code is from the SharePoint 2010 Client Object Model, but the C# version. Translating it to the JavaScript version should be trivial now that I've shown you the correct C# version. This will download the file to the client. Once the file is downloaded, execute the file using the following JavaScript/ActiveX:

 var objShell = new ActiveXObject("Shell.Application");
 var strArguments = "";
 var strDirectory = "";
 var strOperation = "open";
 var intShow = 1;
 objShell.ShellExecute(FileLocation, strArguments, strDirectory, strOperation, intShow);

Where the correct parameters are substituted. Note that this will only work on Internet Explorer -- as this obviously hilariously unsafe piece of ActiveX is blocked by every half-decent browser out there -- and I've only tested it on IE8.

Share:
13,027
iceraven
Author by

iceraven

Knows: 1) Basic Android Development 2) Java Coding 3) Vb Coding 4) C# Coding

Updated on July 29, 2022

Comments

  • iceraven
    iceraven over 1 year

    I'm coding using visual studio, C#. Like the above mentioned, is there any ways i can open a word file stored in Sharepoint's document library? Just have to open the word file for me to read. Do not have to edit or delete.

    I have tried: System.Diagnostics.Process.Start("iexplore.exe",url) but it doesn't seems to work.

    Tried the following codes. No problem displaying my label. But the word doc wouldn't launch as well.

    protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                int index = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = GridView1.Rows[index];
                this.Label1.Text = "You selected " + row.Cells[4].Text + ".";
                System.Diagnostics.Process.Start(@"C:\Users\Administrator\Desktop\Test\Test.docx");
            }  
        }
    
  • iceraven
    iceraven about 11 years
    Hi Ben H. I noticed what u did was to save the file to your computer and then to launch the file. Lets say i know the file to the Sharepoint file is at xxx/xxx/xxx.docx, why wouldnt System.Diagnostics.Process.Start("xxx/xxx/xxx.docx"); works?
  • iceraven
    iceraven about 11 years
    I'm running on windows server 2008 btw.
  • Ben H
    Ben H about 11 years
    To be frank, I'm not entirely sure. I know that it doesn't work when you try to use that method, but I'm not entirely sure why it doesn't work. Writing the file locally is the only way around this that I've found.
  • iceraven
    iceraven about 11 years
    i have tried System.Diagnostics.Process.Start(@"C:\Documents\file.docx"). Same issue here. the word document wouldn't launch. I will post a snippet of my codes on my original post.
  • Ben H
    Ben H about 11 years
    How are you executing the Process.Start method? Is this from a service, or from a user account? For instance, are you double-clicking a .exe file which executes Process.Start, or is a server-side event handler trying to execute Process.Start? EDIT: I see the code you've posted. Does the running process have access to that location? Is there an exception message accompanying the failure to start the process, or does it just do nothing? Furthermore, when the program is running, what does task manager say its Session ID is?
  • iceraven
    iceraven about 11 years
    I'm using VS2010 to code the webpart. Which will then be inserted in sharepoint 2010. I'm trying the webpart from Sharepoint 2010. It should be a user account.
  • Ben H
    Ben H about 11 years
    Ah, this is a webpart? That explains it. That's session 0 isolation, coupled with a misunderstanding of how Process.Start actually works: Process.Start will start the process on the server-side, not the client side. The only way to get this to work like Process.Start on the client side (that I know of) is through java script and ActiveX.
  • iceraven
    iceraven about 11 years
    do you have a solution for this? greatly appreciated!
  • Vivek Bernard
    Vivek Bernard about 11 years
    Or you could return it as a File Response/result so that the browser asks the user to open/save it.
  • Ben H
    Ben H about 11 years
    Good solution, Vivek. Certainly preferable to an ActiveX hack which is IE-specific.