SharePoint Tech Blog

03/05/2010

Create SharePoint Audiences using Powershell

Filed under: SharePoint — Tags: , — kctnpblog @ 12:49 am

This is a modified version of the codeplex script for creating SharePoint audiences. I find this a bit easier to use since it uses an xml file as its input.
This code requires the SharePoint Powershell Profile to be loaded.

[Void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server.Audience");
#
$url="http://localhost";
#
$spsite=Get-SPSite -url $url;
$spcontext=[Microsoft.Office.Server.ServerContext]::GetContext($spsite);
$searchcontext=[Microsoft.Office.Server.Search.Administration.SearchContext]::GetContext($spsite)
$spsite.Dispose();
#
$audmanager=New-Object Microsoft.Office.Server.Audience.AudienceManager($spcontext);
$audcollection=$audmanager.Audiences;
#
[xml]$adata=Get-Content .\audiences.xml
#
$adata.Audiences | ForEach-Object {
$audname=$_.Audience.Name;
$auddesc=$_.Audience.Description;
$newaud=$audcollection.Create($audname,$auddesc);
$newaud.AudienceRules=New-Object System.Collections.ArrayList;
$_.Audience.Rule | ForEach-Object {
$property=$_.Property;
$operator=$_.Operator;
$value=$_.Value;
$newrule=New-Object Microsoft.Office.Server.Audience.AudienceRuleComponent($property,$operator,$value);
[Void]$newaud.AudienceRules.Add($newrule);
}
$newaud.Commit();
#compile audience
$args=$searchcontext.Name, "1", "1", $audname;
$runjob=[Microsoft.Office.Server.Audience.AudienceJob]::RunAudienceJob($args);
}

The audiences.xml file:

Advertisements

5 Comments »

  1. […] example is based on the Create SharePoint Audiences using Powershell article. Below is the modified script (CreateAudiences.ps1) that does not require a PowerShell […]

    Pingback by Create SharePoint 2010 Audiences Using PowerShell — 01/10/2010 @ 3:59 am

  2. I posted an update that fixes some issues and include a deletion script that can be used as part of the uninstall process. Here is the link:
    http://blog.kappasolutions.ca/blog/post/2010/09/30/Create-SharePoint-2010-Audiences-Using-PowerShell.aspx

    Comment by Kirill K. — 01/10/2010 @ 8:53 pm

  3. HI,
    i used this script to create audieances. but its not compiling.
    if i create audience thru CA its automatically compiles.
    requirement-> i need to create a audieance but dont want to add any rules. manually when i create a audience. i cancel the next step wich comes while doing it thru CA. this is for anonymous users no need to add rules.how can achive this
    regards
    Guru

    Comment by guru — 18/02/2011 @ 12:06 pm

    • I’m not sure why an audience without rules is required. By defalult all users aren’t part of an audience and therefore should see everything that is not audience targetted. Could you please clarify what is your functional requirement and what are you trying to achieve?

      Comment by kctnpblog — 17/03/2011 @ 10:46 pm

  4. Hello,
    I have a problem while adding rules to audiences.
    I have xml file with all the rules, but not able to add rules to custom user profile properties using the above script.
    But rules are adding to properties other than custom user profile properties.
    Could please help me?

    Comment by Rajashekar — 28/06/2011 @ 1:02 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: