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:
- system account using token,
- SPSecurity.RunWithElevatedPrivileges over entire code block
- use an admin account token, web.AllUsers["domain\adminaccount"].UserToken and
- user default account.
All resulted in total failure.
I have also tried this solution without success: http://support.microsoft.com/kb/926642
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:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
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;
}
}
});