Experience Forms — Getting the Context Site and Item in Submit Actions
At time of writing, there is no out of the box way to resolve the correct Sitecore.Context.Item or Sitecore.Context.Site from within a Submit Action.
This is a problem if we want to do something with a form on a particular site, page, or language.
Sitecore.Context.Site will resolve to the wrong site in a multi-site instance — it will just take whatever the default catch-all is (usually “website”).
Sitecore.Context.Item just returns null.
I drew inspiration from this question on SSE about how to resolve the correct language.
You can then access the correct Sitecore.Context.Site as normal, and you can retrieve the current context item in your Submit Action code.
var contextItemIdStr = HttpContext.Current.Request.Params[SiteAwareInitializeAjaxOptions.ItemKey];
if (!string.IsNullOrEmpty(contextItemIdStr))
{
pageItem = Sitecore.Context.Site.Database.GetItem(contextItemIdStr);
}
Happy travels!