Thursday, October 13, 2011

SharePoint 2010 - SharePoint Page unable to save the "Publishing Content" changes in Version History.

Problem
I was facing a strange issue with custom "Page". Each and every page layout i was using having a "Page Content" control to enter some text. Here is visual as well as HTML of the control I am talking about.

HTML
<PublishingWebControls:RichHtmlField FieldName="PublishingPageContent" runat="server"/>
Visual
  • I added a new page to my SharePoint Site.
  • Assigned a custom "Page Layout".
  • My Page Layout contains the "Page Content" control. I made some changes to the text "Test".
  • Check In the page.
  • I want to edit the page again, changed the text to "Test - This is a change".
  • Check In the page.
  • Now in the "Page" tab on the top click "Page History".
  • You can find out the versions on the left side. 0.1 and 0.2. Please have a look at the image below.
  • There is no history shown. If you try the same thing with a "Out Of the Box" Page Layouts like Article Page, Enterprise wiki page it will work.


Catch
The publishing content type "Page" was missing column "Page Content". Because if you see in the properties above FieldName="PublishingPageContent"(This is a static name of "Page Content" column) was there in the HTML, but was not as columns in "Page", so history was not getting saved.


Manual Solution
  • Site Actions
  • Site Settings
  • Site Content Types
  • Publishing Content Types
  • Click "Page"
  • Add from existing site columns
  • Select and Add a "Page Content"
  • Save
Programmatic Solution
Create a Visual Studio Project. Create a "Site level Feature". Add a feature receiver. Use below given code. Call the function from Feature_Activation. It will add the "Page Content" if missing.
/// <summary>
/// Add Page Content To Content Type Page for a Publishing Site.
/// </summary>
/// <param name="_properties"></param>
private void AddPageContentToContentTypPage(SPFeatureReceiverProperties _properties)
{
    using (SPWeb _web = ((SPSite)_properties.Feature.Parent).RootWeb)
    {

        //Retrieve the content type
        SPContentType _contentType = _web.ContentTypes["Page"];

        if (_contentType != null)
        {
            if (!_contentType.Fields.ContainsFieldWithStaticName("PublishingPageContent"))
            {
                SPField _field = _web.AvailableFields["Page Content"];
                SPFieldLink _fieldText = new SPFieldLink(_field);

                if (_contentType.FieldLinks[_fieldText.Id] == null)
                {
                    _contentType.FieldLinks.Add(_fieldText);
                }
                _contentType.Update(true);
            }
        }
    }
}

Final Result
Once you are done with this. Please follow the steps mentioned above and you can see the result below.



Hope this helps.

No comments:

Post a Comment