I'm in the beginning of creating a custom web part that allows users to add themselves to Audiences and I'm first trying to get a feel for the API, by trying to simply list all Audiences I have on my site:
class Program
{
static void Main(string[] args)
{
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (var site = new SPSite("http://localhost/"))
{
var context = SPServiceContext.GetContext(site);
var audManager = new AudienceManager(context);
AudienceCollection audCollection = audManager.Audiences;
foreach (Audience audience in audCollection)
{
Console.WriteLine(audience.AudienceName);
}
}
});
}
catch(AudienceExecutionException ex)
{
Console.WriteLine(ex.ToString() + "\n\n");
Console.Read();
}
catch (Exception exception)
{
Console.WriteLine(exception.ToString());
Console.Read();
}
}
}
I can connect to the site but keep receiving the following error:
AudienceExecutionException was unhandled by user code
Retrieving overall status encountered an error.
Error is: Object reference not set to an instance of an object.