Saturday, October 8, 2011

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!

8 comments:

  1. I have tried the above code but its not working. When i deploy the page,web parts included in the page are giving error.When i remove the properties and deploy the page its working fine.

    ReplyDelete
    Replies
    1. @Anonymous, It should surely work as I have kept a working example here. Can you send the Element.xml content for review?

      Delete
  2. Hii...Thanks for quick response. Below is my Element.xml file.











    My custom page includes "Search Refinment Panel" web part and one custom web part extended with CoreResultsWebPart. When i add these properties and deploy the page these web parts gives error and if i delete these properties and then deploy the page its working fine.

    ReplyDelete
    Replies
    1. @Anonymous, Can you pase the xml here?

      Delete
    2. Hello below is my xml code.











      Thanks.

      Delete
    3. @Anonymous - I am extremely sorry, but the xml is not getting though this comment. I think you need to set the web part setting correctly. As you mentioned in the above comment that once you remove the web part the page deploys fine but with it the page does not deploy properly. I am sure you will find the answer soon.

      Delete
  3. Hello, thanks you for your blog. It is helpful.

    In this tutorial, there is something I don't understand. What is the content of sample.aspx?? Is this file empty?



    ReplyDelete
    Replies
    1. Yes, Sample can be empty or you can also use the DefaultPageLayout.aspx html on the sample.aspx.

      Thanks,
      Maulik Dhorajia

      Delete