Blog Archive

Friday, August 10, 2012

Clearing the Cache using Code

I come across a situation where I have to perform the manual Sign Out of the SharePoint 2010 Site and the easiest way to do so is just clearing the cache using code.

Following Code will remove all the cache files from your computer and will clear all the browser cache and indirectly forcefully sign out from the SharePoint site. (As we are clearing the FedAuth Cookie and its information in the Cache.)


     public void ClearApplicationCache()
    {
        List<string> keys = new List<string>();

        // retrieve application Cache enumerator
        IDictionaryEnumerator enumerator = Cache.GetEnumerator();

        // copy all keys that currently exist in Cache
        while (enumerator.MoveNext())
        {
            keys.Add(enumerator.Key.ToString());
        }

        // delete every key from cache
        for (int i = 0; i < keys.Count; i++)
        {
            Cache.Remove(keys[i]);
        }
    }

No comments:

Post a Comment