I recently had a strange requirement or should I say an issue where the client had created a user control in SharePoint and was trying to use Fragment Caching on the user control. But somehow the fragment caching wasn’t working. And the client was asking whether SharePoint actually Supports Fragment Caching or not?
On further investigation I found that he was adding the user control through SPD. After a couple of tests I found the following reason on why Fragment Caching wouldn’t work for SPD added User Controls.
To understand the reason, I would like to touch base on how output caching/fragment caching works. Whenever you ask the control to be cached the output of the control is stored on the server. So that any request that comes for the page containing the control, the output is rendered straight from the cache and the code involved with the control is not executed again. The cache in this case is the server cache (Output saved on server). This would continue till the time the cache is invalidated.
Now let’s take a look how SPD works. Any customization which you do using SPD is stored in the database. So every request for this page, would bring the customized content from the database.
Now let’s combine the two – SPD and caching. When you add the cached user controls from SPD, the user controls are added to the database. So even though you add the entries in SPD, the changes are not added to the file but to the database. So when the page is requested, the contents and particularly, the cached user controls are brought from the database. And this happens every time irrespective of the fact that output caching has been enabled for the controls.
I researched and found that for controls that are added through SPD, since content comes from DB, no output is stored in the Server Cache and hence fragment caching doesn’t work when you use SPD. So the problem is not with SharePoint, not even with the SPD, but with the way SPD works which cannot be combined with output caching.
The workaround for this issue is to use one of the following:
1. If you want to show the user control on all pages:- This can be done by adding the user controls to the master page. So take the following steps to achieve this:
a. Check the master page being used by the site – CustomDefault.master.
b. Open the CustomDefault.master - master page in notepad or visual studio but not SPD. Add the two register tags and the instances of the user controls.
<%@ Register TagPrefix="wssuc" TagName="WellSet" src="~/_controltemplates/Cached.ascx" mce_src="~/_controltemplates/Cached.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="UnCached" src="~/_controltemplates/UnCached.ascx" mce_src="~/_controltemplates/UnCached.ascx" %>
Add the instances somewhere after the <head> tag.
<wssuc:Cached id="IdWellSet" runat="server"></wssuc:Cached>
<wssuc:UnCached id="IdWellSet1" runat="server"></wssuc:UnCached>
c. And that’s it and you are all set to use the user controls.
d. Now open the site and saw the output of the user controls and Output Caching works perfectly for the Cached User Control.
2. If you want to show the user controls on one of the pages or a selected few: The best and easy way to use this is to create a custom web part and add that web part to the gallery of the site. From the gallery you can go ahead and add web part to the selected page/s.