Saturday, January 12, 2013

Create and compile audiences in Sharepoint via PowerShell

When you need to create audiences based on some rules (e.g. based on value of user profile property) it is convenient to use PowerShell. However only creation is not enough for starting using them: you need to compile audiences in order to fill membership information. It is possible to combine these tasks in single script and in this post I will show how to do that.

Let’s create audiences based on the following rule: user profile property Department contains some value, when values will be retrieved from some source (e.g. from text file). Of course it is not restricted to use text file for storing values for audiences rules. You may use any source, just implement the appropriate part of the script:

   1: function write-log($txt)
   2: {
   3:     write-host $txt
   4:     $txt | out-file log.txt -append 
   5: }
   6:  
   7: function getUserProfileAppId()
   8: {
   9:     $type = [System.Type]::GetType(
  10: "Microsoft.Office.Server.Administration.UserProfileApplication,
  11: Microsoft.Office.Server.UserProfiles, Version=14.0.0.0, Culture=neutral,
  12: PublicKeyToken=71e9bce111e9429c", $true)
  13:     foreach ($service in [Microsoft.SharePoint.Administration.SPAdministrationWebApplication]::Local.Farm.Services)
  14:     {
  15:         foreach ($app in $service.Applications)
  16:         {
  17:             if ($app.GetType().FullName -eq $type.FullName)
  18:             {
  19:                 return $app.Id
  20:             }
  21:         }
  22:     }
  23:     return $null
  24: }
  25:  
  26: function compileAudience($name, $ctx)
  27: {
  28:     $id = getUserProfileAppId
  29:     if (!$id)
  30:     {
  31:         write-log ("Can't get id of user profile service application")
  32:         return
  33:     }
  34:     $args = [System.String[]]($id, "1", "1", $name)
  35:     $runJob = [Microsoft.Office.Server.Audience.AudienceJob]::RunAudienceJob($args)
  36: }
  37:  
  38: function createAudience($name, $val, $am, $ctx)
  39: {
  40:     if ($am.Audiences.AudienceExist($name))
  41:     {
  42:         write-log ("Audience '" + $name + "' already exist")
  43:         return
  44:     }
  45:     write-log ("Create audience '" + $name + "'")
  46:     $a = $am.Audiences.Create($name,
  47: "Automatically created audience based on values from text file.",
  48: [Microsoft.Office.Server.Audience.AudienceGroupOperation]::AUDIENCE_NOGROUP_OPERATION)
  49:     $rule = new-object Microsoft.Office.Server.Audience.AudienceRuleComponent(
  50: "Department", "Contains", $val)
  51:     $a.AudienceRules = new-object System.Collections.ArrayList(1)
  52:     $a.AudienceRules.Add($rule)
  53:     $a.Commit()
  54:     write-log ("Compile audience '" + $name + "'")
  55:     compileAudience $a.AudienceName $ctx
  56: }
  57:  
  58: $site = get-SPSite "..."
  59: $ctx = [Microsoft.SharePoint.SPServiceContext]::GetContext($site)
  60: $am = new-object Microsoft.Office.Server.Audience.AudienceManager($ctx)
  61:  
  62: #get pairs of (name, value) for audiences creation
  63: $pairs = ...
  64:  
  65: $pairs | Sort-Object Name | ForEach-Object { createAudience $_.name $_.val $am $ctx }
  66:  
  67: $site.Dispose()

At first we create site, context and audience manager (lines 58-60), which will be used further. Then we retrieve pairs of (name, value) from some source (line 63) and then call createAudience method for each pair (line 65). Creation itself is not very complicated (lines 38-56), but compilation is more tricky (lines 26-36). In order to compile audience we need to pass id of user profile service application. We do that in method getUserProfileAppId (lines 7-24) – we enumerate all service application and return those which have UserProfileApplication type.

Just run this script in order to create and compile your audiences in PowerShell. Hope it will be helpful.

No comments:

Post a Comment