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)


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.


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

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;
If we browse the site now, Home.aspx opens up as the default page.

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.

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.


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;
    PublishingWeb objPublishingWeb = PublishingWeb.GetPublishingWeb(web);
    //Set default page layout of site to internal page layout               
    if (objPublishingWeb != null)
       PageLayout _pageLayout = (from _pl in objPublishingWeb.GetAvailablePageLayouts()
                               where _pl.Name == “InnerPage.aspx”
                               select _pl).FirstOrDefault();
       objPublishingWeb.SetDefaultPageLayout(_pageLayout, true);

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?>
  <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=<img alt="Welcome" src="~SiteCollection/PublishingImages/HomeBanner.jpg" >></Property>
      <Property Name=PublishingPageLayout
                   Value=~SiteCollection/_catalogs/masterpage/HomePage.aspx, Home Page />
      <Property Name=ContentType Value=MyCustomContentType />    
  •  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”)

How to test Emails delivery on a developer server box, where outgoing emails are blocked

         –         In many business cases, there is every now and then a requirement to send emails to administrator or users of the system.

         At times, the outgoing emails are blocked on the dev boxes / servers, which hamper testing the email module in the    application.
         There is a workaround, to preview the outgoing email at the local dev box along with all the body, subject, recipients etc.

In the Web.config file for the application, place the following entries within the section.

<smtp deliveryMethod=“SpecifiedPickupDirectory”>
<specifiedPickupDirectory pickupDirectoryLocation=“c:\Mails” />
<proxy autoDetect=“true” />

The mail will be stored as a Mail message in the folder specified in the web config file, through which we can verify the contents and detail of the mail.

Create and Assign Custom Permission levels programatically in SharePoint

Sometimes, we come across a business requirement, where in:

We need to create SharePoint sites on the fly (maybe using a site definition), which have their own unique permission and groups.
We might need to create a custom permission level for the contributors for this site, say which does not have the delete rights but all other Contributor rights as is.
Then we need to assign this custom permission level to Contributor group and remove the default ‘Contribute’ permission level from the site.


/// Creating & Assigning custom permission
/// level to Contributor group of root site

/// SpWeb object
/// Group on which the custom permission
/// has to be applied

privatevoid CreateAssignCustomPermissionLevel(SPWeb spWeb, SPGroup myGroup)

spWeb.AllowUnsafeUpdates = true;
//Get the role definition collection for this SPWeb
SPRoleDefinitionCollection sprdcoll = spWeb.RoleDefinitions;
//Define the new custom RoleDefinition
SPRoleDefinition roleDefinition = new SPRoleDefinition();
roleDefinition.Name = “MyCustomRoleDefinition”;
//And then start giving all permisions that you want to give.
roleDefinition.BasePermissions =
| SPBasePermissions.EditListItems
//| SPBasePermissions.DeleteListItems //Delete permission removed from this definition.
| SPBasePermissions.ViewListItems
| SPBasePermissions.OpenItems
| SPBasePermissions.ViewVersions
| SPBasePermissions.DeleteVersions
| SPBasePermissions.CreateAlerts
| SPBasePermissions.ViewFormPages
| SPBasePermissions.BrowseDirectories
| SPBasePermissions.ViewPages
| SPBasePermissions.BrowseUserInfo
| SPBasePermissions.UseRemoteAPIs
| SPBasePermissions.UseClientIntegration
| SPBasePermissions.Open
| SPBasePermissions.EditMyUserInfo;

//Add role definition to spweb

//Assign custom role definition to the contributor group
SPRoleAssignment assignment = new SPRoleAssignment(myGroup);
//Add custom role definition to the SPRoleAssignment
//Add the custom RoleAssignment to the SPWeb.

//Once we have the custom permission level assigned to contributors group,
//we need to remove the default ‘Contribute’ permission level from this web
spWeb.AllowUnsafeUpdates = false;

This method can be placed in feature receiver, where this feature is activated when the site is created on the fly.