SharePoint 2013 Using Publishing Page as custom forms for content type

I wanted to customize the display, edit, and new form for a custom list.  I took the approach of using a publishing web with custom web part.  The 2 methods that I use to accomplish that are as follow

public static void AddWebParts(SPWeb web, string pageUrl, List webParts)
        {
            using (SPLimitedWebPartManager wpManager = web.GetLimitedWebPartManager(pageUrl, System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared))
            {                
                foreach (WebPart webPart in webParts)
                {
                    wpManager.AddWebPart(webPart, webPart.ZoneID, 0);
                    wpManager.SaveChanges(webPart);
                }                               
            }
        }

private static void addPage(SPWeb web, PublishingWeb pubWeb, string pageUrl, string pageTitle)
        {
            PublishingPage pubPage = pubWeb.GetPublishingPage(pageUrl);
            if (pubPage == null)
            {
                pubPage = pubWeb.AddPublishingPage(pageUrl, layout);
                pubPage.Title = pageTitle;      
                pubPage.Update();          

               AddWebParts(web, pageUrl, webParts);

                pubPage.CheckIn("Checked in comment.");
                pubPage.ListItem.File.Publish("Published comment.");
            }
        }

Then I set the content type to use my custom publishing page

SPContentType ct = list.ContentTypes["Item"];
                ct.EditFormUrl = "/Pages/form.aspx";
                ct.DisplayFormUrl = "/Pages/form.aspx";
                ct.NewFormUrl = "/Pages/form.aspx";
                ct.Update();

All looks like it's working.  However, when I click on the item to view it, I'm getting the following error:

System.ArgumentException: Column 'PublishingPageLayout' does not exist. It may have been deleted by another user. 

It has something to do with the parameters (i.e. list, web, id) that were passing to the page. To get it working, I had to set RequiresRouting property to false.  The updated code is as below

private static void addPage(SPWeb web, PublishingWeb pubWeb, string pageUrl, string pageTitle)
        {
            PublishingPage pubPage = pubWeb.GetPublishingPage(pageUrl);
            if (pubPage == null)
            {
                pubPage = pubWeb.AddPublishingPage(pageUrl, layout);
                pubPage.Title = pageTitle;
                pubPage.RequiresRouting = false;        
                pubPage.Update();          

               AddWebParts(web, pageUrl, webParts);

                pubPage.CheckIn("Checked in comment.");
                pubPage.ListItem.File.Publish("Published comment.");
            }
        }


Comments

Popular posts from this blog

SharePoint 2013 App Details Page Error

SharePoint 2013 - Working with Display Template for Content Search Web Part

Add spell check dictionary in SharePoint 2010, SharePoint 2013, and Office 365