I try to access a Word file inside a SharePoint document library. When the document is about to be fetched from the server the Aspose.Words.Document constructor throws an exception with the message “The remote server returned an error: (401) Unauthorized.”
These two lines can reproduce the error with Aspose.Words version 15.9:
var fileName = "https://spserver.com/projects/project1/doclib/my_word_file.docx";
var document = new Document(fileName);
Accessing the document on https://spserver.com/projects/project1/doclib/my_word_file.docx via Internet Explorer 11 or Word 2013 works fine.
I guess the SharePoint server is only accessible inside the corporate network if the network traffic is routed through the network proxy. Is there a way to programmatically set a proxy server for Aspose.Words?
Thanks for your inquiry. You can download data into byte array using some standard .NET methods and then load stream into Document object. For example:
WebClient webClient = new WebClient();
webClient.Credentials = new NetworkCredential("User1", "password");
byte[] bytes = webClient.DownloadData("URI");
MemoryStream stream = new MemoryStream(bytes);
Document doc = new Document(stream);
thanks to your solution I could develop a full round trip like this:
WebClient webClient = new WebClient();
webClient.Credentials = CredentialCache.DefaultNetworkCredentials;
var docUri = "https://spserver.com/projects/project1/doclib/my_word_file.docx";
byte[] bytes = webClient.DownloadData(docUri);
MemoryStream stream = new MemoryStream(bytes);
Document doc = new Document(stream);
// TODO: modify document
MemoryStream outputStream = new MemoryStream();
doc.Save(outputStream, SaveFormat.Docx);
webClient.UploadData(docUri, "PUT", outputStream.ToArray());
Am I correct that using this method I am working on a disconnected copy of the SharePoint file? Is this the recommended way to access SharePoint files? What would e.g. happen if another user checks out the file for edit while I am working on the local copy or modifies the file before I upload it?
I guess that I will miss most of the SharePoint collaboration features with this approach? Are there any recommendations or best practices?
Thanks for your inquiry. We are in coordination with our product team to get answer pertaining to your queries. Soon you will be updated with the required information.
The best way for you is to use public SharePoint API for document processing. Otherwise it will be just offline copy of a document. For example SPFile class can be used for processing files on SharePoint server. The workflow would be to check out a file from SP server (using this API), then process it locally using Aspose.Words for .NET, and finally check in the file back to SharePoint server (again using this API). Hope, this helps.