Search code examples
sharepoint-2010infopath

Deploy BrowserFormWebPart declaratively without BinarySerializedWebPart Element


Does anyone know if there is a way to deploy a BrowserFormWebPart (custom InfoPath form for a list content type) using standard AllUsersWebPart element and a CDATA section for the properties? So far I have tried without success. Any help is appreciated.


Solution

  • After 2 days of research - Following code works

      private void UpdateInfoPathForms(SPSite oSite)
        {
            UpdateInfoPath(oSite, "Lists/Audit Calendar/Item/newifs.aspx");
            UpdateInfoPath(oSite, "Lists/Audit Calendar/Item/displayifs.aspx");
            UpdateInfoPath(oSite, "Lists/Audit Calendar/Item/editifs.aspx");
        }
    
        private void UpdateInfoPath(SPSite oSite, string formFileLocation)
        {
            var file = oSite.RootWeb.GetFile(formFileLocation);
    
            using (SPLimitedWebPartManager manager = file.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
            {
                try
                {
                    var wp1 = new Microsoft.Office.InfoPath.Server.Controls.WebUI.BrowserFormWebPart();
    
                    wp1.SubmitBehavior = Microsoft.Office.InfoPath.Server.Controls.WebUI.SubmitBehavior.FormDefault;
                    wp1.FormLocation = "~list/Item/template.xsn";
                    wp1.ContentTypeId = oSite.RootWeb.Lists["Audit Calendar"].ContentTypes["Item"].Id.ToString();
    
                    IListWebPart listWebpart = wp1 as IListWebPart;
    
                    listWebpart.ListId = oSite.RootWeb.Lists["Audit Calendar"].ID;
    
                    if (formFileLocation.Contains("newifs.aspx"))
                    {
                        listWebpart.PageType = PAGETYPE.PAGE_NEWFORM;
                    }
                    else if (formFileLocation.Contains("displayifs.aspx"))
                    {
                        wp1.ListFormMode = Microsoft.Office.InfoPath.Server.Controls.WebUI.ListFormMode.ReadOnly;
                        listWebpart.PageType = PAGETYPE.PAGE_DISPLAYFORM;
    
                    }
                    else if (formFileLocation.Contains("editifs.aspx"))
                    {
                        listWebpart.PageType = PAGETYPE.PAGE_EDITFORM;
                    }
                    listWebpart.ViewFlags = SPViewFlags.None;
    
                    manager.AddWebPart(wp1, "Main", 0);
                    manager.SaveChanges(wp1);
                }
                finally
                {
                    manager.Web.Dispose();
                }
            }