Pages

Search This Blog

Wednesday, February 22, 2012

Delete default page (Default.aspx) created on publishing site during site provisioning

In case of certain scenarios, like when we create a publishing site, or when we create a site definition and activate the publishing feature, a default.aspx page is created.
If we are using a custom page as welcome page for the site, we can delete the default.aspx page during provisioning of the site.

We can add the code for this in Feature Receiver.


SPList objPagesList = null;              
Guid ListId;

//Get the Pages List for current Locale
ListId = PublishingWeb.GetPagesListId(web);
objPagesList = web.Lists[ListId];

//Get the URL of the default.aspx page to be deleted
string urlPagetoDelete = objPagesList.RootFolder.ServerRelativeUrl + "/default.aspx";
if (web.GetFile(urlPagetoDelete).Exists)
{
    SPFile objFile = objPagesList.RootFolder.Files["default.aspx"];
    if (objFile != null)
    {
         if (objFile.CheckOutStatus == SPFile.SPCheckOutStatus.None)
         {
              objFile.CheckOut();
         }
         objFile.Delete();
    }
}




Assign default page of Publishing site to custom page during site provisioning

In my previous post, I have highlighted how to create pages in Pages library using CAML when a site is being provisioned.
We had created Home.aspx custom page.
 

Requirement:
We need to set Home.aspx as the default welcome page of the site, when the site is being provisioned.

Solution:
We can achieve this by putting a piece of code in the feature receiver.


//Get the Publishing Web object
PublishingWeb objPublishingWeb = PublishingWeb.GetPublishingWeb(web);
//Get the Home.aspx file from Pages library
SPFile objHomePageFile = web.GetFile(objPublishingWeb.PagesListName + "/Home.aspx");            

//Set default page of publishing web
objPublishingWeb.DefaultPage = objHomePageFile;
objPublishingWeb.Update();
web.Update();
  
If we browse the site now, Home.aspx opens up as the default page.

Tuesday, February 21, 2012

Programatically set default Page layout of site to Custom page layout

In my previous post, we have learned how to create custom page layouts using feature.

Requirement:
We need to set the custom page layout created as the default page layout of the site.
Say if a user creates a new page, it should create a page which is using the custom layout.
We need to achieve this programatically.

Solution:

We can achieve this by writing the code within the receiver of the feature created.

public override void FeatureActivated(SPFeatureReceiverProperties properties)
{   
    SPSite site = properties.Feature.Parent as SPSite;
    SPWeb web = null;
    if (site == null)
    {
         web = properties.Feature.Parent as SPWeb;
         if (web == null) return;
         site = web.Site;
    }
    else web = site.RootWeb;

       //Get the publishing web object
    PublishingWeb objPublishingWeb = PublishingWeb.GetPublishingWeb(web);
    //Set default page layout of site to internal page layout  (InnerPage.aspx)             
    if (objPublishingWeb != null)
    {
       PageLayout _pageLayout = (from _pl in objPublishingWeb.GetAvailablePageLayouts()
                               where _pl.Name == "InnerPage.aspx"
                               select _pl).FirstOrDefault();
       objPublishingWeb.SetDefaultPageLayout(_pageLayout, true);
       objPublishingWeb.Update();
    }
}


Create Pages using Custom Page Layout & Deploy them in Pages library

In my last post I described how to create custom page layouts.
Now using those page layouts, we need to create Pages and deploy them in pages library of the publishing site.
Follow the below mentioned steps in order to achieve the above.
  • Create a new module within the solution (say MyPages)
  • Delete the sample.txt file.
  • Add a text file within the module and rename it to Home.aspx

  • Add the required directives in Home.aspx page and add web part zones within the page. You can download Home.aspx page here.
  • Open the Elements.xml file and make changes accordingly as shown below
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="MyPages" Url="Pages">
    <File Path="MyPages\Home.aspx" Url="Home.aspx" Type="GhostableInLibrary" NavBarHome="True">
      <Property Name="Title" Value="Home" />
      <Property Name="PublishingPageImage" Value="&lt;img alt=&quot;Welcome&quot; src=&quot;~SiteCollection/PublishingImages/HomeBanner.jpg&quot; &gt;"></Property>
      <Property Name="PublishingPageLayout"
                   Value="~SiteCollection/_catalogs/masterpage/HomePage.aspx, Home Page" />
      <Property Name="ContentType" Value="MyCustomContentType" />    
    </File>
</Module>
</Elements>
  •  Deploy the solution and you should be able to see Home.aspx page created in the pages library using the custom page layout created.
  • We have specified that this page should be deployed to Pages library in our Elements.xml file of this module. (URL="Pages")

Create custom page layouts using Custom content type using CAML

Requirement:
At times, we come across a requirement to have custom page layouts and use those page layouts to create pages in the site. This needs to be done using CAML.

Scenario:
Lets take an example where you are creating a site definition for a customer, and the site should have a home page and some internal pages. Admin can create more internal pages with same layout as of the existing internal pages. In this scenario, we can create 2 custom page layouts within the site definition say Home Page & Inner Page, and then create new pages based on these page layouts.

Solution:
Here is how we achieve this:
Open Visual Studio 2010 and create a new project (Empty SharePoint project)

Step 1:
Create Custom content type derived from Article page content type.
  •    Right click the project and click Add -> New Item
  • Select Content Type from the list.
  • Give a suitable name for the content type.
  • Click Add.
  • Then it will ask, which base content type should your custom content type be inherited from. Select Article page from the list (In our case, it is article page. This can be inherited from any other type based on requirements)
  • Click Finish.
 
  • A feature with a content type is created in your solution. Open the Elements.xml file and you can update the properties of the content type like Name, Group, Description.
  • Notice the Content type ID generated by Visual Studio. This is a unique ID and will be used when we create page layouts using this content type.
 <?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <!-- Parent ContentType: Article Page (0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D) -->
  <ContentType ID="0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D00f2f3fc2629204643b44911a79fa95814"
               Name="MyCustomContentType"
               Group="Custom Content Types"
               Description="My Custom Content Type"
               Inherits="TRUE"
               Version="0">
    <FieldRefs>
    </FieldRefs>
  </ContentType>
</Elements>
  • Now the content type is created and on deploying this, we can see out custom content type in the content type gallery on the site.
 
 
Step 2:
Create custom page layouts based on the custom content type:
  • Add a new module to the solution, and name it PageLayouts. 
  • Delete the Sample.txt file created automatically within the module.
  • Add 2 aspx pages (page layouts) with the name HomePage & InnerPage. The way to do this is by Clicking Add -> New Item -> Text File and then put the name as Home.aspx
 
  • Similarly add InnerPage.aspx.
  • Register the required namespaces and assemblies and then add web part zones on this page accordingly.
  • You can download these pages here.
  • Update the elements.xml file accordingly as shown below:
<!--[if gte mso 9]> <![endif][if gte mso 9]> Normal 0 false false false EN-US X-NONE X-NONE <![endif][if gte mso 9]> <![endif][if gte mso 10]> <!--><?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Module Name="PageLayouts" Url="_catalogs/masterpage">
    <File Path="PageLayouts\HomePage.aspx" Url="HomePage.aspx" Type="GhostableInLibrary" >
      <Property Name="Title" Value="Home Page" />
      <Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;" />
      <Property Name="PublishingPreviewImage" Value="~SiteCollection/PublishingImages/HomePage.png" />
      <Property Name="PublishingAssociatedContentType" Value=";#$Resources:cmscore,contenttype_articlepage_name;;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D00f2f3fc2629204643b44911a79fa95814;#" />
    </File>
    <File Path="PageLayouts\InnerPage.aspx" Url="InnerPage.aspx" Type="GhostableInLibrary" >
      <Property Name="Title" Value="Inner Page" />
      <Property Name="ContentType" Value="$Resources:cmscore,contenttype_pagelayout_name;" />
      <Property Name="PublishingPreviewImage" Value="~SiteCollection/PublishingImages/InnerPage.png" />
      <Property Name="PublishingAssociatedContentType" Value=";#$Resources:cmscore,contenttype_articlepage_name;;#0x010100C568DB52D9D0A14D9B2FDCC96666E9F2007948130EC3DB064584E219954237AF3900242457EFB8B24247815D688C526CD44D00f2f3fc2629204643b44911a79fa95814;#" />
    </File>
</Module>
</Elements>
  •  Deploy the solution. Activate the feature. Go to Master page and page layouts gallery. You can see HomePage and InnerPage Page layouts deployed there.

  • These custom page layouts can now be used to create new pages.