Gorgo.Live.ToString()

Mariusz, Gorzoch tech Blog

AudienceManager in context of w3wp

leave a comment »

SharePoint is powerful platform, but it has also his nasty dark corners. Today I discover one of them when I deploy my solution to production and for some reason it was working for me (full admin on farm), where it did not work for any other normal users. For some reason if someone with normal/read privileges tried to use my solution he get “Access denied” message.

at the end this was caused by this peace of code:

AudienceManager audManager = new AudienceManager();
Audience globalAudience = audManager.GetAudience("All site users");

and there is nothing wrong with that code. It works quite ok in dev environment with admin rights, but if run by normal users then this is throwing exception “Access denied” on internal “CanAccess” method.

more over the some issue appears for construction like this:

Audience aud = audManager.GetAudience(new Guid(aID))

happily there is simple work around to that by instead of using GetAduience method you will use Audiences enumerator and check item by item for the item you are looking for. To simply this I wrote simple extensions method to AudienceManager and you can find them bellow:

public static class AudienceManagerExtensions
{
    public static Audience GetAudienceByNameEx(this AudienceManager audManager, string AudienceName)
    {
        foreach (Microsoft.Office.Server.Audience.Audience objAudience in audManager.Audiences)
        {
            if (objAudience != null && objAudience.AudienceName == AudienceName)
            {
                return objAudience;
            }
        }
        throw new ArgumentOutOfRangeException("AudienceName", AudienceName, "Cannot be found");
    }     public static Audience GetAudienceByGuidEx(this AudienceManager audManager, Guid AudienceID)
    {
        foreach (Microsoft.Office.Server.Audience.Audience objAudience in audManager.Audiences)
        {
            if (objAudience != null && objAudience.AudienceID == AudienceID)
            {
                return objAudience;
            }
        }
        throw new ArgumentOutOfRangeException("AudienceID", AudienceID, "Cannot be found");
    }
}

imageHope that this will help someone to solve this problem quicker then me. I’ve spend few days looking for that answer. http://robswssblog.blogspot.com/2008/03/access-denied-on-audienceglobalcanacces.html

Advertisements

Written by Mariusz Gorzoch

18 April 2011 at 16:13

Posted in SharePoint

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

%d bloggers like this: