Technical tips, suggestions and learnings, mostly on SharePoint.
Home » What We're Thinking » Ideas
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!
Step 1:
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=1.0.0.0,Culture=neutral,PublicKeyToken=ec04d212dae41cff" %>
Step 2:
Ensure that this assembly is listed as a safe-control in your web.config and is signed with a strong-key:
Web.Config:
<SafeControls> <SafeControl Assembly="MyAssembly, Version=1.0.0.0, Culture=Neutral, PublicKeyToken=ec04d212dae41cff" Namespace="MyAssembly.MyCustomWebPartPage" TypeName="*" /></SafeControls>
Step 3:
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
if (cacheEnabled)
SPContext.Current.UseDefaultCachePolicy = false;
base.OnInit(e);
SetContextProperties();
}
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();
return setting;
internal static void SetContextProperties()
SPControlMode display;
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;
else
//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"));
HttpContext.Current.Response.Cache.SetExpires(DateTime.Now.AddSeconds(cacheTimeOutSeconds));
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.Server);
HttpContext.Current.Response.Cache.SetValidUntilExpires(true);
There you have it! A speedy loading web page completely coming out of cache!