I am working on a problem right now and it involves downloading multiple PDF files to our DMZ from our LAN SharePoint server.
What we are trying to accomplish is a "NewsFeed" type application. What it does is take list data from uploaded Content Types made in SharePoint, then post them to our external website hosted on the DMZ, with a link to the appropriate pdf file. We used the REST API that sharepoint has to accomplish the text layout fairly easily, but the problem we are now running into is copying the .pdf of the uploaded document from the SharePoint site, onto the DMZ, so we can store them in a cache, and the user can access them through an href.
Here is code that works, to access the SharePoint REST API.
// Create the connection to sharepoint. Credentials are managed within web.config
SharePointListsREST.SharePointDataContext dc = new SharePointListsREST.SharePointDataContext(new Uri(ConfigurationManager.AppSettings["spUrl"]));
CredentialCache cc = new CredentialCache();
cc.Add(new Uri(ConfigurationManager.AppSettings["spUrl"]), "NTLM", new NetworkCredential(ConfigurationManager.AppSettings["spUser"], ConfigurationManager.AppSettings["spPassword"], ConfigurationManager.AppSettings["spDomain"]));
dc.Credentials = cc;
This works perfectly, it is able to authenticate, grab the data and display it. For the PDF download functionality we have this.
private void GetPDF(string URL, string path)
{
if (System.IO.File.Exists(path))
System.IO.File.Delete(path);
WebClient wc = new WebClient();
CredentialCache cc = new CredentialCache();
cc.Add(new Uri(ConfigurationManager.AppSettings["spUrl"]), "NTML", new NetworkCredential(ConfigurationManager.AppSettings["spUser"], ConfigurationManager.AppSettings["spPassword"], ConfigurationManager.AppSettings["spDomain"]));
wc.Credentials = cc;
wc.DownloadFile(URL, path);
}
which does not work at all and throws a 401 Not Authorized error.However, if you go to try to access the pdf file by going to http://XX.XX.XX.XX/.../test.pdf
you will be prompted for a username/password. Entering my windows based credentials will give me access to the pdf.
I have tried using wc.UseDefaultCredentials = true;
but it does not work either. Does anyone have any ideas on how I can get this to work with the webclient?
Here is one of the error logs from IIS.
2012-07-11 00:56:12 XX.XX.XX.XX GET /Employee+Images/_t/sample.jpg - 80 - 192.168.200.12 - 401 2 5 0