Technical tips, suggestions and learnings on SharePoint.
I recently was given the task of creating an entire Internet facing site using WSS (Windows SharePoint Services) 3.0. I created web part pages everywhere so that the content manager could login and customize the content on each page. The problem that I was having was that this site had very high web traffic so I needed to ensure that the pages loaded up very quickly. I also needed to ensure that the pages in design mode and unpublished pages were not cached. Unlike MOSS, WSS didn't have the ability to turn on output caching on directly for different versions of the page. With a little fiddling around and some relection of the assemblies, I was able to get caching working on my web part pages. Enjoy!
Modify the Page attribute of
a) default.aspx in the root of your web site and b) all of the web part page layouts (Local_drive:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\60\TEMPLATE\1033\STS\DOCTEMP\SMARTPGS\) to inherit from a custom assembly:
<%@ Page language="C#" MasterPageFile="~masterurl/custom.master" Inherits="MyAssembly.MyCustomWebPartPage,MyAssembly,Version=22.214.171.124,Culture=neutral,PublicKeyToken=ec04d212dae41cff" %>
Ensure that this assembly is listed as a safe-control in your web.config and is signed with a strong-key:
<SafeControls> <SafeControl Assembly="MyAssembly, Version=126.96.36.199, Culture=Neutral, PublicKeyToken=ec04d212dae41cff" Namespace="MyAssembly.MyCustomWebPartPage" TypeName="*" /></SafeControls>
Inherit from (Microsoft.SharePoint.WebPartPages.WebPartPage) and use the following code in the class:
public class MyCustomWebPartPage : Microsoft.SharePoint.WebPartPages.WebPartPage
protected override void OnInit(EventArgs e)
bool cacheEnabled = bool.Parse(GetAppSetting("WebPageCacheEnabled", "False"));
//this needs to be set in order to prevent WSS from setting cache headers
SPContext.Current.UseDefaultCachePolicy = false;
private static string GetAppSetting(string key, string defaultValue)
string setting = defaultValue;
if (System.Configuration.ConfigurationManager.AppSettings[key] != null)
setting = System.Configuration.ConfigurationManager.AppSettings[key].ToString();
internal static void SetContextProperties()
if (string.IsNullOrEmpty(HttpContext.Current.Items["ContextPropertiesSet"] as string))
HttpContext.Current.Items["ContextPropertiesSet"] = "true";
display = SPControlMode.Display;
if (HttpContext.Current.Request.Form["MSOLayout_InDesignMode"] != "1")
string str = HttpContext.Current.Request.QueryString["DisplayMode"];
if (!string.IsNullOrEmpty(str) && (string.Compare(str, "DESIGN", StringComparison.OrdinalIgnoreCase) == 0))
display = SPControlMode.Edit;
display = SPControlMode.Edit;
//Only cache if the file is published (i.e. not in draft or checked out mode) and the page is not in edit mode (determined above)
if (((SPContext.Current.FileLevel == SPFileLevel.Published)) && (display == SPControlMode.Display))
//Default cache timeout to 5 min if not set
double cacheTimeOutSeconds = double.Parse(GetAppSetting("WebPageCacheTimeoutSeconds", "300"));
There you have it! A speedy loading web page completely coming out of cache!
where to put the code in step three, shall I add it to each custom webpart I am building? or where?
The code in the above example goes into a signed assembly called MyAssembly. The code in step three is just a class in there which gets called as described in step one from your web part pages
hi ..i used the above code....but i am getting the following error
"The directive 'outputcache' is not allowed in this page. "
plz provide the sln for this
Are you sure you are using the webpartpage page layout or the master page?