0

I am using SharePoint Foundation 2010 and would like to enable output caching for certain pages, or maybe the whole site. On SharePoint Server you have a mechanism to enable page output caching across the site collection (it is actually ASP.NET output cache under the hood). On SPF you don't get that feature - fair enough.

So how can I enable output caching? In ASP.NET I would just add a page directive - something like <%@ OutputCache Duration="30" %>. SharePoint throws an error if this is in the page. Sounds like it needs to be done in code, perhaps override the page class? Any suggestions welcome.

mike
  • 1
  • 1
  • 2

2 Answers2

0

You have to:

0 - Add the parameter

<%@ OutputCache Duration="300" VaryByParam="*" Shared="True" VaryByCustom="x"  VaryByControl="hdnButtonToForceUpdateCacheIfYouWantOnClick" %>

1 - Extend SPHttpApplication, IVaryByCustomHandler.

public class MyCache: SPHttpApplication, IVaryByCustomHandler {
public override void Init()
        {
            base.Init();
            this.RegisterGetVaryByCustomStringHandler((Microsoft.SharePoint.ApplicationRuntime.IVaryByCustomHandler)this);
        }

        public string GetVaryByCustomString(HttpApplication app, HttpContext context, string custom)
        {
            StringBuilder sb = new StringBuilder();

 string[] strings = custom.Split(';');
            bool appended = false;
            foreach (string str in strings)
            {
                switch (str)
                {
                     case "x":
                         var xv= context.Request.Cookies.Get("x");
                        if (xv!= null)
                        {
                            sb.Append(xv.Value);
                        }

                        break;
                }
             }
             return sb.toString();
           }
   }

2 - After modify your global.asax,

<%@ Assembly Name="Microsoft.SharePoint"%>
<%@ Assembly Name="Energisa.Distribuidora.Webparts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e2d241931dc35e9d"%> 
<%@ Import Namespace="(Namespace).Webparts" %>
<%@ Application Language="C#" Inherits="(Namespace).MyCache" %>

See more in: https://msdn.microsoft.com/en-us/library/office/ms550239(v=office.14).aspx

Roger Gusmao
  • 3,788
  • 1
  • 20
  • 17
  • One more thing, sometimes you have to clean the pre compiled ascx, that are stored at: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files – Roger Gusmao Jul 25 '18 at 11:45
0

You need to go on top site collection, there you have option under site administration->site collection output cashing-> something like cash profiles. There you can set your profile for output cashing and other stuff.

Diomos
  • 420
  • 5
  • 15