Showing posts with label Download. Show all posts
Showing posts with label Download. Show all posts

Friday, January 24, 2014

Downloading a Document Template from SharePoint

Since I've wasted a couple of hours on this I'll share my findings.  I'm creating a web service that will do a mail merge for me using Aspose Words. The document template is stored in a SharePoint document library as a content type and the merged document I want to store back into the document library. 

I tackled the upload first.  Since my web service and SharePoint will be installed on separate servers, I call the SharePoint Copy.asmx web service to upload the document. That works fine.  My next task was to use the document template from SharePoint which I need to retrieve as a memory stream. 

The preferred method is to use the GetItem method of the Copy.asmx service. No problem I thought as I already has a reference to it. 

But try as I might I could not manage to download the template.  When you install a content type as a template the path to the document is something like this

DocLib/Forms/Some Letter/SomeLetter.dotx

I suspect it was something to do with this location that it would not work.  If I tried the same code on a document in the library itself it worked fine.  I gave up in frustration and started Googling for alternatives.

I came up with this incredibly simple approach which I share below.  It simply uses the DownloadData method of the WebClient.  Since I know the absolute path of the template, it works a treat.


public MemoryStream DownloadSharePointDocument(string sourceUrl)
{
   string sharePointSiteUrl = ConfigurationManager.AppSettings["sharepointsiteurl"];

   if (!sharePointSiteUrl.EndsWith("/"))
   {
 
       sharePointSiteUrl = sharePointSiteUrl + "/";
   }
   sourceUrl = sharePointSiteUrl + sourceUrl;
 
   WebClient wc = new WebClient();
   wc.UseDefaultCredentials = true;

   byte[] response = wc.DownloadData(sourceUrl);
   MemoryStream ms = HelperMethods.MemoryStreamFromBytes(response);
   return ms;

}

Thursday, May 21, 2009

Lynx Text Browser - Downloading Document Error

I was developing an Accessible web site the other day which included downloading of documents (either Word, Excel or PDF). It just contains an anchor tag for the document which will open the document in Acrobat Reader allowing the user to "Save As". It worked as designed in Internet Explorer and FireFox but gave an error when I tried it in the Lynx Text Browser. The error message was:

The exception Integer division by zero.
(0xc0000094) occurred in the application at location 0x00451c24.

There is not a lot of technical help around Lynx and Google didn't offer any meaningful results. But I found that I could download a text file in Lynx without problem so I figured it must be something to do with the inability to recognise the PDF file type.

After some more research I found an obscure attribute for the Anchor tag in HTML. This is the "type" attribute in which you can add the content type. So to the Anchor tag for the document I added

type="application/pdf"

and that did the trick. It appears that Lynx requires this extra hint of the file type to avoid the error shown above. I also added the type attribute for the other file types for good measure and it all works fine now.