I have been looking for a way to create a new cache profile in SharePoint using Powershell. This profile can be used in the Page Output cache.
I noticed that cache profiles are stored in a list called “Cache Profiles” and implement the “Page Output Cache” content type.
So, creating a new profile shouldn’t be that difficult.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
$site = Get-SPSite $rootURL $web = Get-SPWeb $rootURL $list = $web.Lists["Cache Profiles"] $ctCacheProfile = $list.ContentTypes["Page Output Cache"] $profile = $list.Items.Add() $profile["ContentTypeId"] = $ctCacheProfile.Id $profile.Update() $profile["Title"] = "My Custom Cache Profile" $profile["Display Name"] = "My Custom Cache Profile" $profile["Description"] = "My Custom Cache Profile" $profile["Perform ACL Check"] = $true $profile["Enabled"] = $true $profile["Duration"] = "180" $profile["Check for Changes"] = $false $profile["Vary by Custom Parameter"] = "Browser" $profile["Vary by HTTP Header"] = "" $profile["Vary by Query String Parameters"] = "" $profile["Vary by User Rights"] = $true $profile["Cacheability"] = "ServerAndPrivate" $profile["Safe for Authenticated Use"] = $true $profile["Allow writers to view cached content"] = $false $profile.Update() |
To add the profile to your Page Output Cache and enable the cache, you can use the following code:
1 2 3 4 |
$cacheSettings = New-Object Microsoft.SharePoint.Publishing.SiteCacheSettingsWriter($rootURL) $cacheSettings.EnableCache = $true $cacheSettings.SetAuthenticatedPageCacheProfileId($site, $profile.ID) $cacheSettings.Update() |