Tag Archives: SPWeb

how to apply custom master page to application pages

I came across a position, where i have an application page developed for user requirement and the user desires the look and feel of this page to me similar to other pages in the context.

I tried so many alternatives but didn’t get prospered; I found the right to use the default.master or custom master page for application pages.

First thing is that if you need to use a masterpage from the site context, you want to have the similar content placeholders as are expected by the master page.

Master pages can be loaded dynamically. This can be done by assigning a master page file to the MasterPageFile property in the Page object. This property may only be assigned in the Page PreInit event, this is the first event executed page execution lifecycle.

Copy and paste this below code in your custom application page and deploy the assembly to GAC and see, it should work now.

protected override void OnPreInit(EventArgs e)
SPWeb TestWeb = SPControl.GetContextSite(Context).OpenWeb();
string strUrl = TestWeb.ServerRelativeUrl + “/_catalogs/masterpage/Test.master”;
this.MasterPageFile = strUrl;

Where “Test.master” has to be stored in master page gallery of the site.

Probably you may get a memory leak for SPWeb object , so you can use this simple code instead of the above

protected override void OnPreInit(EventArgs e)
this.MasterPageFile = SPContext.Current.Web.MasterUrl;

Delete All Old Versions from SharePoint 2010 Document Libraries

How many versions of a typical document do you need to keep?  5?  10?  100?
SharePoint content databases often get cluttered with redundant versions of the same documents.  In some cases, I’ve seen presentation libraries with less than 10 GB of active content, but hundreds of GBs of content due to gradual changes.
If you’re ready to clear out old versions, SharePoint 2010’s Management Shell makes it easy.  The script below will iterate through all lists and update them to keep only the last 2 copies.  It will also loop through and delete unneeded versions.

Get-SPWebApplication |  Get-SPSite -Limit All |  Get-SPWeb -Limit All |  ForEach-Object  { ForEach($list in $_.Lists) { If($list.EnableVersioning -eq $true) { $list.MajorVersionLimit = 2; $list.Update(); ForEach($item in$list.Items) { $item.URL; $item.SystemUpdate() } } } }

You can see that it…
  1. Loops through all web applications
  2. Loops through their site collections
  3. Loops through their webs
  4. Loops through their lists
  5. If versioning’s enabled, it sets the major version limit to 2
  6. In order to remove old versions, it needs to loop through each item and perform a system update

The script can be easily amended to deal with only specific site collections / libraries.  To keep a different number of versions, modify the MajorVersionLimit variable above.