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:
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.