Hi,
I have an InfoPath 2010 form which will be accessed anonymously. Â When submitted, it will save to a document library. Â In order for an anonymous user to be able to submit the form (to the library), I must use code behind to run with elevated privileges. Â This is all fine and it works except it submits it as System Account. Â
This is a problem because I need to create a workflow that will send an email to a group mailbox (can’t do this through an alert), and workflows do not trigger when an item is added from System Account. Â I’ve looked everywhere and tried everything to get the code to submit as the System Administrator account instead using the UserToken but I have not been successful. Â I thought about creating an event receiver, but I don’t want the hassle of managing one for each form that may eventually need this same functionality.
Here is my code behind:
This works but submits as System Account:
public void btnSubmit_Clicked(object sender, ClickedEventArgs e)
{
   // Write your code here.
  SPSecurity.RunWithElevatedPrivileges(delegate()
  {
    this.Submit();
  }
}
After doing a bunch of reading, I tried this with no luck. Â It submits as System Account as well:
public void btnSubmit_Clicked(object sender, ClickedEventArgs e)
{
// Write your code here.
SPWeb webInUserContext = SPContext.Current.Web;
SPSite SiteInUserContext = SPContext.Current.Site;
Guid webGuid = webInUserContext.ID;
Guid siteGuid = SiteInUserContext.ID;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
//Get Web and Site impersonated as SHAREPOINT\System
using (SPSite site = new SPSite(siteGuid))
{
using (SPWeb web = site.OpenWeb(webGuid))
{
//right now, logged in as SHAREPOINT\System Account
//impersonate site admin account and continue
SPUserToken userToken = GetSystemToken(site);
using (SPSite impersonatedSite = new SPSite(siteGuid, userToken))
{
using (SPWeb impersonatedWeb = impersonatedSite.OpenWeb(webGuid))
{
impersonatedWeb.AllowUnsafeUpdates = true;
//NOT SURE WHAT TO DO HERE.
//this.Submit(); works but only submits as System Account
impersonatedWeb.AllowUnsafeUpdates = false;
impersonatedWeb.Dispose();
}
}
web.Dispose();
}
}
});
}
public static SPUserToken GetSystemToken(SPSite site)
{
site.CatchAccessDeniedException = false;
SPUserToken sysToken = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite siteWithElevatedPrivileges = new SPSite(site.ID))
{
sysToken = siteWithElevatedPrivileges.RootWeb.SiteAdministrators[0].UserToken;
}
});
return sysToken;
}
Any advice would be greatly appreciated. Â I’ve attached a file too for ease of reading.
Jeff
Sorry I meant to say that I want to submit forms as “Site Administrator” not “System Administrator”