Show audience-targeted content in a SharePoint 2010 web part from a SharePoint list
I spent quite a bit of time hunting down this content and found that it’s MUCH easier than many of the blog posts will lead you to believe.
SharePoint does not automatically trim out anything based on audience-targeting. The web part has to do that work.
- You enabled Audience Targeting on a list (which allows you to audience-target each item in the list to a specific SharePoint group).
- You are developing a custom web part in C# / Visual Studio and want to show audience-targeted results.
Add the following 2 references to your project and add the using statements:
And this (in my case) goes into the User Control of a Visual Web Part in the Page_Load method.
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
using (SPWeb web = site.OpenWeb(SPContext.Current.Web.Url))
SPPictureLibrary library = (SPPictureLibrary)web.GetList(listUrl);
if (library != null)
AudienceLoader audienceLoader = AudienceLoader.GetAudienceLoader();
foreach (SPListItem listItem in library.Items)
// get roles the list item is targeted to
string audienceFieldValue = (string)listItem["Audience"];
// quickly check if the user belongs to any of those roles
if (AudienceManager.IsCurrentUserInAudienceOf(audienceLoader, audienceFieldValue, false))
// is a member
// not a member
I altered the code slightly to look at the Audience Targeting field of my picture library.
Here is the article:
Comments are closed