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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s