SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a webpart that extracts a form from an infopath template then it attempts to add this to a form library in SharePoint. I get an unauthorized error 401 when it attempts to add the file. It works fine if I run the webpart on the local server, but has that error when I run it on another computer. The server is on 2009 r2 and sharepoint 2010. I have created different codes where I use:

  1. system account using token,
  2. SPSecurity.RunWithElevatedPrivileges over entire code block
  3. use an admin account token, web.AllUsers["domain\adminaccount"].UserToken and
  4. user default account.

All resulted in total failure.

I have also tried this solution without success:

The oddball part is, I have two single server test machines. Testmachine1 works fine whether you use it remotely or locally. While Testmachine2 fails when using remotely.

Then in my production environment with a load balance and 2 WFE servers. The code seems to work on certain machines on the network but not others and I can't find the connection between them. It's not an account issue as my account which I am admin of works when using my computer but when I log into the computer throwing an error, it doesn't work and throws the 401 errors.

Anyone with any idea????????? Thanks.

The code block which works fine in some but not other:


            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
                using (SPWeb web = site.AllWebs[listRoot])

                    SPDocumentLibrary list = (SPDocumentLibrary)web.Lists[listName];

                    web.AllowUnsafeUpdates = true;  
                    using (MemoryStream ms = new MemoryStream(xmlData))
                        //add metadata
                        Hashtable ht = new Hashtable();
                        ht.Add("Title", formName);
                        newfile = list.RootFolder.Files.Add(formName + ".xml", ms, ht, true);


                    web.AllowUnsafeUpdates = false;  


share|improve this question

I see that you are adding an XML file. Maybe on the machines where you cannot upload, the administrator doesn't allow XML files to be uploaded. Try uploading XML files directly to these document libraries from the browser and see if it works. If it doesn't change the settings to allow XML files to be uploaded.

share|improve this answer
I tried that and it does allow me to upload xml file using that computer. I am thinking it's something server related as I have to stand alone SP2010 test machines. One works fine and the other won't allow me to update using another computer. I just don't know what it could be? – wirble Dec 6 '12 at 14:21
This issue was caused by one of those cross domain issue. The servers were two load balance servers. One was hitting the other and it was not properly configured. If I remembered correctly. 1. You either impersonate a different user in code or 2. Change the computer host file on the server and add servername – wirble Sep 11 '13 at 19:15

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.