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();
    }
}




No comments:

Post a Comment