Nick Grattan's Blog

About Microsoft SharePoint, .NET, Natural Language Processing and Machine Learning

Opening SharePoint documents for Edit from Client Applications

with 7 comments

The ActiveX component “SharePoint.OpenDocuments” is used to open SharePoint documents ready for editing. It is documented here and is usually called from JScript code in a web page. But what about if you want to open a SharePoint document from a client application, such as a .NET WinForm or WPF application?

You might try the following code using the full URL to the document:

System.Diagnostics.Process.Start(docUrl); 

This will open the document in the client application (e.g. Microsoft Word), but it will always be opened read-only.

The following code will create an version specific instance of SharePoint.OpenDocuments and then call the EditDocument method to open the document:

        public static void OpenDocumentForEdit(string docUrl)
        {
            Type t = null;
             // get the correct version specific version...
            t = Type.GetTypeFromProgID("SharePoint.OpenDocuments.1");
            if (t == null)
                t = Type.GetTypeFromProgID("SharePoint.OpenDocuments.2");
            if (t == null)
                t = Type.GetTypeFromProgID("SharePoint.OpenDocuments.3");

            if (t == null)
            {
                System.Diagnostics.Process.Start(docUrl);    // best we can do, will open read-only
                return;
            }
            Object o = Activator.CreateInstance(t);
            object[] openParms = { docUrl, string.Empty };
            t.InvokeMember("EditDocument",
                System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance,
                null, o, openParms);
        }

Note that different versions of Office install different versions, and this code attempts to open the version installed on the client application. If none could be found it opens the document, albeit in read-only mode. When the method is called the following dialog will be presented to the user:

About these ads

Written by Nick Grattan

December 31, 2010 at 7:29 pm

7 Responses

Subscribe to comments with RSS.

  1. Great post Nick for client applications. After lot of search on the web, finally I got here.

    Still I am unable to use your code as error at Object o = Activator.CreateInstance(t) prevents me to move on.

    Error is ” {System.Runtime.InteropServices.COMException (0×80040154): Retrieving the COM class factory for component with CLSID {9203C2CB-1DC1-482D-967E-597AFF270F0D} failed due to the following error: 80040154.
    at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandle& ctor, Boolean& bNeedSecurityCheck)
    at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean fillCache)
    at System.RuntimeType.CreateInstanceImpl(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean fillCache)
    at System.Activator.CreateInstance(Type type, Boolean nonPublic)

    I see in Registry that “SharePoint.OpenDocuments.3″ & “SharePoint.OpenDocuments.4″ have the same data {9203C2CB-1DC1-482D-967E-597AFF270F0D}

    Kalyan

    July 5, 2012 at 7:27 pm

  2. I got the answer compiling the code in x86 rather than Any CPU (or) x64 solved the problem.

    Kalyan

    July 5, 2012 at 7:48 pm

    • Kalyan, Glad you found a solution and thanks for posting your question. Regards, Nick.

      Nick Grattan

      July 6, 2012 at 6:49 am

  3. Authentication Issue

    Nick,

    I am trying to override the default system windows credentials(Kalyan) in my Client Ojbect Model using ‘TestLogin’ and tried executing your activator.createinstance method for editing office documents using client applications.
    Both the logins do exist on SharePoint portals with
    Kalyan -> Full Control
    TestLogin -> visitor permission

    But the document opens up using the credentials of Kalyan rather than TestLogin.

    Question: How can a document be opened using TestLogin rather than the default windows login (Kalyan)?

    Kalyan

    July 11, 2012 at 1:17 pm

    • I guess you will need to impersonate the “TestLogin” user, have a look at WindowsImpersonationContext: http://msdn.microsoft.com/en-us/library/system.security.principal.windowsimpersonationcontext.aspx. I have not tried this, but I think it’s the solution for you. Regards, Nick.

      Nick Grattan

      July 12, 2012 at 7:06 am

      • Hi Nick,
        I was looking for how to open/edit SharePoint document from remote application and this post helped me, but i need to pass authentication token also but looks like there is no option to do it. I also tried impersonating as you mentioned in the above link and then call the code to open/edit document but i see office client is getting opened using the logged in user account but not impersonated account.
        Any idea how to achieve this?

        Mallikarjun

        January 3, 2014 at 12:25 pm

  4. Hi Nick, i have the same issue than Kalyan.
    I cant compile the code in x86, because i am using a sharepoint event receiver and can not deploy the solution.
    Any idea to resolve it?

    Javier Caballero

    September 28, 2013 at 6:57 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 60 other followers

%d bloggers like this: