Using System.Net.WebRequest to copy files to Sharepoint RRS feed

  • Question

  • User-2010311731 posted


    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));
        // 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);
        // Get a web response back

    What can I do to make this work without having to be logged into the server?

    Your advice is much appreciated!


    Friday, May 9, 2014 2:28 PM

All replies

  • User71929859 posted


    Unless your site is added to the trusted site lists, it always asks for the credentials. Once you logout from the server, it can't get the windows credentials hence the error throws out.

    What can I do to make this work without having to be logged into the server?

    Add it to the trusted sites list.

    In Internet Explorer, you can do it like this.

    Go to the Internet Options -> Select Security tab.

    Select Local intranet Icon.

    Click on Sites button.

    Check Automatically detect intranet network checkbox.

    Click Advanced button.

    Give your site address in the Add this website to the zone: textbox. You can even use wild cards here if you need all the sites in a particular domain to be trusted

    ex: - *.test.co.nz

    Click Add.

    Sunday, May 11, 2014 7:56 PM
  • User-2010311731 posted

    Thank you for the response.  I have this site added to the trusted sites list.  The problem is storing the credentials.  Our server engineers are looking into either group policy or possibly virus scanners that are removing the entries from Credentials Manager whenever I log off the server.  I also notice that every few hours, the Web Client service gets stopped.

    I am working on a client-side solution so the files can get copied using the user credentials instead of forcing them through the server.  Any advice on the best way to accomplish this client-side?


    Tuesday, May 13, 2014 12:00 PM