Friday, March 8, 2013

Apply page layout to publishing pages


1-      Update content types to publishing pages
======================================

  private void UpdatePublishingPageContentTypes(SPWeb Web)        {            Web.AllowUnsafeUpdates = true;            // Get a content type.            SPContentTypeCollection contentTypeColl = Web.AvailableContentTypes;            string pageContentTypeName = "Page";            SPContentType pageContentType = Web.ContentTypes[pageContentTypeName];          
            foreach (SPContentType contentType in contentTypeColl)            {                if (contentType.Id.ToString().ToLower().Contains(pageContentType.Id.ToString().ToLower()))                {                    try                    {                        SPList list = Web.Lists["Pages"]; // Throws exception if does not exist.
                        // Make sure the list accepts content types.                        list.ContentTypesEnabled = true;
                        if (list.ContentTypes[contentType.Name] == null)                        {                            list.ContentTypes.Add(contentType);                        }                    }                    catch (ArgumentException ex)                    {                        // No list is found.                    }                }            }            Web.AllowUnsafeUpdates = false;        }


2-      Add content type attribute to elements .xml under file line declaration :
<File Url="Home.aspx"  Path="Home.aspx" Type="GhostableInLibrary">
        <Property Name="Title" Value="Home" />
        <Property Name="PublishingPageLayout" Value="~SiteCollection/_catalogs/masterpage/HomeLayout.aspx, Home Default Layout" />
        <Property Name="ContentType" Value="myPagesCT" />
</File>

No comments:

Post a Comment