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.

Saturday, October 8, 2011

SharePoint 2010 - Set the default Page Layout programmatically.

Goal - To set "CustomDefaultPageLayout.aspx" as the default page layout, so the next time when some one creates a "New Page" our custom page layout is applied to the page and not system's.


Follow the below given steps to achieve the goal.

  • First of all make sure that the site we have to apply the page layout is a "Publishing Site."
  • Create a Visual Studio Empty Project.
  • Add a Feature Receiver
  • Add new reference of "Microsoft.SharePoint.Publishing".
  • Apply this on the top of the class.
    using Microsoft.SharePoint.Publishing;
    using System.Linq;
  • On feature activation use the below given code.
    private void SetDefaultPageLayout(SPWeb _web)
    {
        PublishingWeb _pubWeb = PublishingWeb.GetPublishingWeb(_web);
        if (_pubWeb != null)
        {
            PageLayout _pageLayout = (from _pl in _pubWeb.GetAvailablePageLayouts()
                                        where _pl.Name == "CustomDefaultPageLayout.aspx"
                                        select _pl).FirstOrDefault();
            _pubWeb.SetDefaultPageLayout(_pageLayout, true);
            _pubWeb.Update();
        }
    }
  • Call the function "SetDefaultPageLayout" from the Feature Activation event.

SharePoint 2010 - Apply a Page Layout to an ASPX page using ELEMENT.xml

Goal
To deploy an ASPX page using Module in Visual Studio project. The web part should be deployed with a PageLayout applied to it. Else a page is going to be created without a page layout attached. The name of the page is "Sample.aspx" and PageLayout is "DefaultPageLayout.aspx". "GSM" is the name of our module.

  • Open Visual Studio 2010.
  • File > New > Sharepoint > Empty Sharepoint Project > Name it "Sample"
  • Right click project > Add a Module > Name "Something".
  • Remove Sample.txt. Add a file(Sample.aspx) to the module.
  • Make the below given changes to the "Element.xml".
  • Right click project and deploy.
  • Visit your website.
  • Site Actions > Edit Site
  • Click "Add a Web Part". On the top ribbon.

You should now be able to see the page with page layout applied to it.

Change in Element.xml


<?xml version="1.0" encoding="utf-8"?>
    <Elements xmlns="http://schemas.microsoft.com/sharepoint/">
      <Module Name="GSM" Url="Pages">
        <File Path="GSM\Sample.aspx" Url="Sample.aspx" Type="GhostableInLibrary">
            <Property Name="Title" Value="Sample Title"/>
            <Property Name="PublishingPageLayout" Value="~SiteCollection/_catalogs/masterpage/DefaultPageLayout.aspx, Default Page Layout"/>
        </File>
      </Module>
    </Elements>


Hope this is helpfull for you!

Sharepoint - Troubleshooting a page with "?contents=1"

Guys, I found out something which is really cool. I was working on a custom web part. I added that web part on a page "Test.aspx". One of the web part was having an error "Object Reference...". There was no SharePoint Designer installed in my machine and now i was confused, how can i remove that web part so that at least the page show up? I found out that from a friend that if we just use "?contents=1" in the querystring we can get the list of web parts and can remove the web part directly.


This "?contents=1" also show the web parts which were added to page in past. Go through the below given images to have an idea what i am trying to explain.


  • Here you can see a page with 4 web parts added to it.
    1. Quick Clicks - This is my custom webpart. This webpart contains an error which blocks the whole page to load.
    2. Image Viewer - OOTB web part.
    3. XML Viewer - OOTB web part.
    4. Content Query - OOTB web part.
  • When I clicked "Save & Close" from the top ribbon. The page bombs out giving the error mentioned below.
  • Now as i mentioned above i used "?contents=1" and it showed me the list of all the web parts for this page.
  • In the above image "Custom Web Part" was added to the page but was deleted, still it shows up in the history.
Please let me know if there are any queries.