The following code runs fine as an administrator but crashes with a 403 forbidden error when run as a non-admin on the am.GetAudience("My Audience") call. Can anyone spot why, I'm running with elevated privileges?
List<string> accounts = new List<string>();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite("http://mysite"))
{
AudienceManager am = new AudienceManager(ServerContext.GetContext(site));
foreach (UserInfo member in am.GetAudience("My Audience").GetMembership())
{
accounts.Add(member.NTName.ToLower());
}
}
});
Audience a = am["My Audience"]works without elevating privileges.... – user682 May 18 '10 at 8:56am.Audiences["My Audience"]? This is because GetAudience calls an internal method CanAccess() that checks for UserProfileApplicationAdminRights and throws an UnauthorizedAccessException if you are not allowed to manage audiences. If you use the Audiences collection index remember to place the code inside atry/catchand catch theAudienceArgumentExceptionto avoid the code to break if an audience does not exist. You see similar behaviour on User Profile Manager – Anders Rask♦ Oct 25 '11 at 8:31