Wednesday, September 09, 2015

InfoPath 2013 - Full Trust Code

If you are working in InfoPath form with source code, you must elevate your form to "Full Trust" as its required to access the SharePoint server and its files.

On design , Select the file->Form Option->under Security and Trust category, select the "Full trust" security level and save and publish.

Here is the simple entry form with three column,that will insert the item in SharePoint list through C# object model.

public void CTRL5_5_Clicked(object sender, ClickedEventArgs e)
            using (SPSite FormSite = new SPSite("http://win-oq6s840ais1/"))
                using (SPWeb FormWeb = FormSite.OpenWeb())

                    SPList list = FormWeb.Lists["Register"];
                    SPListItem item = list.Items.Add();
                    item["Title"] = GetDomValue("/my:myFields/my:ddlTitle");
                    item["FirstName"] = GetDomValue("/my:myFields/my:txtFirstName");

                    item["LastName"] = GetDomValue("/my:myFields/my:txtLastName");

                    FormWeb.AllowUnsafeUpdates = true;

                    // Set AllowUnsafeUpdates back to 'false' to prevent further updates to the database.
                    FormWeb.AllowUnsafeUpdates = false;


        private string GetDomValue(string XpathToGet)
            return this.CreateNavigator().SelectSingleNode(XpathToGet, this.NamespaceManager).Value;