3
votes

Working on a WPF application (C# 4.0).

The system stores a number of Word documents in SharePoint. Word can directly edit those documents, as SharePoint exposes document libraries to Office via WebDAV.

In order to launch Word to edit these documents, our WPF app uses Microsoft.Office.Interop.Word.

Discovered (through simply trying it) that the only difference between using Interop.Word to open a local document vs. a document from SharePoint via WebDAV is that the ref object FileName you pass is the URL instead of a local path string.

This all just works:

var wordApplication = 
    new Microsoft.Office.Interop.Word.Application { Visible = true };

object filePath = 
    "http://PathToSharepoint.com/DocumentLibrary/DocumentName.doc";

object missing = Missing.Value;
object readOnly = false;
object isVisible = true;

Document reportDoc = wordApplication.Documents.Open(
    ref filePath,
    ref missing,
    readOnly,
    ref missing,
    ref missing,
    ref missing,
    ref missing,
    ref missing,
    ref missing,
    ref missing,
    ref missing,
    ref isVisible);

reportDoc.Activate();

HOWEVER, if the Windows user running the application isn't a domain user with permissions to the document library, at best Word will prompt them for a username and password, and at worst sometimes just throw a COMException.

The user has already logged into our WPF app with the same credentials that they would need to supply, and we have the username and securePassword in memory. However, I can't see any obvious way of how you'd supply those credentials to Word.

Anyone got any clue how I could provide something akin to a NetworkCredential to this?

2

2 Answers

0
votes

Did you try to execute the code impersonated as the user whose user name and password you have? There are many examples how to do that on the web, e.g. http://www.codeproject.com/KB/dotnet/UserImpersonationInNET.aspx. I can't test it right now myself, but it should work...

0
votes

Assuming you have windows 8.1. The credentials used by Office are stored in your Windows Credential Manager. Access this by going to Start > Search for "Credential Manager" > then click on Windows Credentials.

The credential should look similar to this:

MicrosoftOffice15_Data:orgid:<emailadress>

Using this api http://credentialmanagement.codeplex.com you are able to store a credential yourself in the Windows Credential Store using:

var cm = new Credential { Target = "MicrosoftOffice15_Data:orgid:[email protected]", PersistanceType = PersistanceType.LocalComputer, Password = "yourverysecurepasswordhere" };
cm.Save();

Then when you open the document using Word for example it opens it without prompting for your credentials.

Tested on SharePoint Online with Office 2013 and Windows 8.1.