I am using following code to download SharePoint Document(Word) library and its working fine. However whenever i am trying to open this downloaded document, its keep asking document credentials.

using (ClientContext context = new ClientContext("<Site URL>"))
                    string filePath = @"Local Path";
                    context.Credentials = new NetworkCredential("username", "Password", "domain");
                    context.RequestTimeout = 6600000;
                    Web web = context.Web;
                    Microsoft.SharePoint.Client.List list = web.Lists.GetByTitle("<Doc Library>");

                    ListItem listItem = list.GetItemById(1);
                    Microsoft.SharePoint.Client.File file = listItem.File;

                    var fileRef = file.ServerRelativeUrl;
                    var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, fileRef);
                    var fileName = Path.Combine(filePath, (string)listItem.File.Name);
                    using (var fileStream = System.IO.File.Create(fileName))


Once I download the document, I am going to fetch content inside the documents with the help of "Microsoft.Office.Interop.Word.Application" API. Here I am getting all the content.

Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application();
ImpersonateUser iU = new ImpersonateUser();
iU.Impersonate("domain", "username", "Password");
doc = application.Documents.Open(@"LocalPath");

But each time its asking credentials. So would like to pass these credentials through C# code?How to do?


It seems that you files are password protected. you need to open it by providing password.

Document newDocument = wordApplication.Documents.Open(@"C:\Users\XYZ\Desktop\myNextFile.doc",
                                                              confirmConversions: false,
                                                              addToRecentFiles: false,
                                                              readOnly: false,
                                                              passwordDocument: "#$nonsense@!");
  • Yes,its password protect document.I used your code,but still its asking Credentials... – user52682 Jul 31 '16 at 13:25
  • Please be more precise. Where it is asking password ? It is throwing exception in Code ? – Muhammad zaighum Aug 1 '16 at 5:47
  • Its not throwing any error.Whenever debugging point reaches the Documents.Open method,a window security pop up coming on screen and asking the protected document credentials.If i give my credentials(UserName,Password),its takes and moving to next line. – user52682 Aug 1 '16 at 6:04
  • Please make sure the password you are providing here is correct. passwordDocument: "#$nonsense@!"); – Muhammad zaighum Aug 1 '16 at 6:11
  • I hard coded correct password in the Documents.Open Method. – user52682 Aug 1 '16 at 6:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.