Tuesday 28 September 2010

Set object caching user accounts with PowerShell

In all installations of SharePoint, it’s important to properly configure object caching so that SharePoint can efficiently retrieve cached versions of items where possible. In my experience, this is one of THE most frequently forgotten configuration steps – yet Technet is clear that SharePoint cannot perform optimally without this being done. Object caching configuration mainly involves the configuration of two specific user accounts which SharePoint will use to simulate a reader and high-privileged user. If these accounts are not configured, you’ll see entries like this in the Windows event log:

Object Cache: The super user account utilized by the cache is not configured. This can increase the number of cache misses, which causes the page requests to consume unneccesary system resources.

The full details of why this configuration is necessary are in the Technet article Configuring object cache user accounts. The article outlines the process for performing the configuration, but bizarrely (IMHO) lists a process which is half PowerShell, half manual steps in Central Admin. My guess is that, just like software, shipping documentation has compromises too, and this article didn’t yet make the cut to receive the full treatment. I’ve written some PowerShell which does the full configuration – make sure you edit it to use your account names and web application URLs. You'll need to paste this into a tool which can strip out HTML, before saving as a .ps1 file. If anyone has any trouble working with it, let me know and I'll post the actual file somewhere.

Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
 
# EDIT THIS TO ENSURE ACCOUNTS ARE CORRECT FOR ENVIRONMENT..
 
$objectCachePortalSuperReader = "sp2010\portalsuperreader" 
$objectCachePortalSuperUser = "sp2010\portalsuperuser" 
 
$allWebApps = ("http://one.myCompany.com", "http://two.myCompany.com") 
 
Function ConfigureObjectCachingOnWebApp([Microsoft.SharePoint.Administration.SPWebApplication]$webApplication)
{
      Write-Host ("Processing " + $webApplication.Url);
 
      try
      {
            GrantPolicy $webApplication $objectCachePortalSuperReader FullRead
            GrantPolicy $webApplication $objectCachePortalSuperUser FullControl
 
            SetPropertyOnWebApp $webApplication "portalsuperuseraccount" $objectCachePortalSuperUser
            SetPropertyOnWebApp $webApplication "portalsuperreaderaccount" $objectCachePortalSuperReader
 
            $webApplication.Update()
 
            Write-Host -BackgroundColor DarkGreen -ForegroundColor White ("Configured " + $webApplication.Url);
      }
      catch
      {
            Write-Host -BackgroundColor DarkRed -ForegroundColor White ("Failed to configure " + $webApplication.Url + ". Error details : " + $_)        
      }
}
 
Function GrantPolicy([Microsoft.SharePoint.Administration.SPWebApplication]$webApplicationForPolicy, [string]$userOrGroup, 
      [Microsoft.SharePoint.Administration.SPPolicyRoleType]$policyLevel)
{
      $policy = $webApplicationForPolicy.Policies.Add($userOrGroup, $userOrGroup) 
      $policy.PolicyRoleBindings.Add($webApplicationForPolicy.PolicyRoles.GetSpecialRole($policyLevel)) 
}
 
Function SetPropertyOnWebApp([Microsoft.SharePoint.Administration.SPWebApplication]$webApplicationForProperty, [string]$property, 
      [string]$propertyValue)
{
      $webApplicationForProperty.Properties[$property] = $propertyValue 
}
 
# inline script starts here..
 
$confirmed = Read-Host "This script must be edited before running for correct user accounts for each environment - have you done this? Enter Y to continue or N to exit."
if ($confirmed -eq 'y')
{
      foreach($webAppName in $allWebApps)
      {
            $webApp = Get-SPWebApplication -Identity $webAppName -ErrorAction SilentlyContinue
 
            if($webApp)
            {
                  ConfigureObjectCachingOnWebApp($webApp)     

            }
            else
            {
                  Write-Host -ForegroundColor DarkRed ("Failed to find web app '" + $webAppName + "' in this environment. ")
            }
      }
}
 
Write-Host -BackgroundColor Blue -ForegroundColor White "Script completed"

Monday 13 September 2010

Speaking on SP2010, jQuery and AJAX at the 1st UK SharePoint Saturday

In a couple of weeks I’ll be speaking at the first ever UK edition of the popular SharePoint Saturday event. It should be a great day with lots of SharePoint folks from around the country and beyond – the speaker list looks great and we even have Joel Oleson (ex-MS IT) and Mark Miller (www.EndUserSharePoint.com) flying in from the States. Since the original allocation “sold-out” (in loose terms, the event is free!), capacity has just been extended to 400, so at the time of writing you still have chance to register.

My topic this time is about a transition that I’ve been trying to address for myself recently, and suspect quite a lot of SharePoint developers are thinking about - here’s the blurb:

SharePoint, jQuery and AJAX - a beginner's survival guide

It takes a different approach to build SharePoint apps with a slick AJAX experience - getting rid of those nasty postbacks involves declining some help from .Net and learning new techniques. Whilst the answers often lie with the Client Object Model and jQuery, both are hefty topics so the barrier can be intimidating. This talk aims to boil things down to "3 core techniques to survive by", with step-by-step demos to show the development process. Tips and tricks such as enabling jQuery intellisense, debugging JavaScript and using JSON will be covered. To pull the concepts together, the session will round off with a demo migration of an existing mini-application from postbacks to AJAX. Building experiences that users love is simpler than you think!

I’ll publish the slides here after the talk, but as ever much of the goodness will be in the demos – right now I’m not sure if the event has the facility to record the talks unfortunately. If you’re within reach and are thinking of coming, no doubt the whole day will have great content.

Hope to see you there!