Greetings,
I am trying to use System.Net.WebRequest from my web application to copy files to a Sharepoint library. I have the AppPool set up to use a domain account that has owner permissions in the Sharepoint list. Everything works fine if I log on to the Web server with this account, open the Sharepoint library in the browser, and supply the windows credentials. If I stay logged in to the server, I can run my web app and it copies. If I log off of the server, I get this message...
Access Denied. Before opening files in this location, you must first add the web
site to your trusted sites list, browse to the web site, and select the option
to login automatically.
Here is the function I am calling where remoteFile value is @"\\sp.mysite.com\davwwwroot\sites\subsite\library\"
public void UploadDocument(string localFile, string remoteFile) { // Read in the local file FileStream fstream = new FileStream(localFile, FileMode.Open, FileAccess.Read); byte[] buffer = new byte[fstream.Length]; fstream.Read(buffer, 0, Convert.ToInt32(fstream.Length)); fstream.Close(); // Create the web request object WebRequest request = WebRequest.Create(remoteFile); CredentialCache mycache = new CredentialCache(); mycache.Add(new Uri(remoteFile), "Basic", new NetworkCredential(@"domain\someaccount", "********")); request.Credentials = mycache; request.Method = "PUT"; request.ContentLength = buffer.Length; // Write the local file to the remote system BinaryWriter writer = new BinaryWriter(request.GetRequestStream()); writer.Write(buffer, 0, buffer.Length); writer.Close(); // Get a web response back request.GetResponse().Close(); }
What can I do to make this work without having to be logged into the server?
Your advice is much appreciated!
Matt