0

I am programmatically adding an InfoPath Form web part to a web part page. In my code I am setting the form location and some other properties. When the feature is activated, the web part is added to the page and the properties are set but the form doesn't show. The error on the web part page is "The Web Part cannot find an InfoPath form in the specified location. Either the location does not have an InfoPath form associated with it or it is on a different site collection. Modify the Web Part Properties and select a list or library on the current site." When I edit the web part and click apply, the form shows. Here is my code. After the method is called in the feature activation, I am doing a site.Update(). Any help would be appreciated. Thank you.

private void AddWebPartToPage(string pageName, SPWeb site, string reqLibName, string url)
    {
        SPFile page = site.GetFile(url + "/" + listTitle + "/" + certReqPageName);
        SPLimitedWebPartManager manager = site.GetLimitedWebPartManager(url + "/" + listTitle + "/" + pageName, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
        //create instance of InfoPath form webpart
        Microsoft.Office.InfoPath.Server.Controls.WebUI.BrowserFormWebPart wp = new Microsoft.Office.InfoPath.Server.Controls.WebUI.BrowserFormWebPart();
        //set webpart properties
        wp.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;
        wp.AllowClose = false;
        wp.AllowHide = false;
        wp.AllowMinimize = false;
        //add webpart to page
        manager.AddWebPart(wp, "Full Page", 0);
        manager.SaveChanges(wp);
        //set form location for CertReq webpart
        wp.FormLocation = url + "/" + reqLibName;
        manager.SaveChanges(wp);
        page.Update();
    }
1
  • Along with FormLocation you need to set ContentTypeId too.
    – san
    Jan 26 '17 at 11:51
0

Try this

SPFile page = site.GetFile(url + "/" + listTitle + "/" + certReqPageName);
using (SPLimitedWebPartManager manager = page.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 = url + "/" + reqLibName;
    wp1.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;
    wp1.AllowClose = false;
    wp1.AllowHide = false;
    wp1.AllowMinimize = false;
    manager.AddWebPart(wp1, "Full Page", 0);
    manager.SaveChanges(wp1);      
    }
    finally
    {
        manager.Web.Dispose();
    }
}
1
  • I am still getting the same behavior. The properties are there but I have to edit the web part and click apply to get the InfoPath form to show up. Nov 7 '14 at 16:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.