InfoPath 2010 – SPContexts, code behind and sandboxed solutions

I was recently tasked with adding some code behind to an InfoPath 2010 form and having it submit to multiple SharePoint lists. The issue I ran into was trying to get spcontext to work in a sandboxed solution. I couldn’t. Try as hard as I could, the spcontext was not being passed. I was getting the error that the object was not set to the instance of the object. Bottom line, when using Infopath 2010 in sandboxed mode, with security level of domain, you do not have access to the SPCONTEXT object. So everytime I tried to call it, it wouldn’t be called and the value was being set to Null.

The first solution is to set your InfoPath form to full trust and have an administrator approve it. But, I didn’t want to do that. All I wanted was to get the URL of the site. SO I did something neat. I found the SaveLocation that InfoPath stores and pulled out the URL from that.

 string strPath = e.InputParameters[“SaveLocation”].ToString();
            string strLoc = strPath.Substring(0, strPath.LastIndexOf(“/”));

            XPathNavigator mainNav = this.CreateNavigator();
            //this line will set a value for the field

            mainNav.SelectSingleNode(“//my:SiteUrl”, NamespaceManager).SetValue(strLoc.Substring(0, strLoc.LastIndexOf(“/”)).ToString());
            
           Then I just use my form variable in my SPSite declaration and i’m good to go.

using (SPSite site = new SPSite(MainDataSource.CreateNavigator().SelectSingleNode(“//my:SiteUrl”, NamespaceManager).Value))

If anyone can tell me why SPcontext isn’t available in the security level of Domain Trust in an InfoPath form, i would love to hear it.

Comments are closed.