tags:

views:

429

answers:

4

Using the SharePoint SDK, I'm attempting to retrieve a list and display the contents in a composite control. The list is audience aware and I'd like to maintain that in my control. How can I go about getting this list, filtered by audience, using the SharePoint SDK? Here's some of the code I'm working with:

SPWeb currentWeb = SPContext.Current.Site.RootWeb;
SPList shortcuts = currentWeb.Lists["Shortcuts"];
A: 

Here's a code snippet that maybe could be of use, to determine each items audience:

SPList shortcuts = currentWeb.Lists["Shortcuts"];
SPListItemCollection items = list.Items;

Audience siteAudience;

ServerContext context = ServerContext.GetContext(site);
AudienceManager audManager = new AudienceManager(context);
foreach (SPListItem item in items)
{
  string ID = item["Target Audiences"].ToString();
  string NewID = ID.Remove(36);
  Guid guid = new Guid(NewID);
  siteAudience = audManager.GetAudience(guid);
}
Magnus Johansson
+3  A: 

Here's some of the code I'm using now, and it's not quite working for me. According to how the audiences are set up, I should be getting results:

protected override void CreateChildControls()
{
dropdown = new DropDownList();
dropdown.Items.Add(new ListItem("Select...", ""));

SPWeb currentWeb = SPContext.Current.Site.RootWeb;
SPList shortcuts = currentWeb.Lists["Shortcuts"];

ServerContext context = ServerContext.GetContext(currentWeb.Site);
AudienceManager audManager = new AudienceManager(context);
AudienceCollection audiences = audManager.Audiences;
AudienceLoader audienceLoader = AudienceLoader.GetAudienceLoader();

foreach (SPListItem listItem in shortcuts.Items)
{
    string audienceFieldValue = (string)listItem["Target Audiences"];

    if (AudienceManager.IsCurrentUserInAudienceOf(audienceLoader, audienceFieldValue, false))
    {
        dropdown.Items.Add(new ListItem(listItem.Title, listItem.Url));
    }
}

Controls.Add(dropdown);
base.CreateChildControls();
}

On:

if (AudienceManager.IsCurrentUserInAudienceOf(audienceLoader, audienceFieldValue, false))

It's never returning true, even when it should be.

Chris Stewart
Turns out this code is correct and does work. I had adjusted my profile to be a part of the specified audience, but the change hadn't taken effect since the audiences hadn't been recompiled. Once that happened, everything worked as expected.
Chris Stewart
A: 

Hi Johansson,

Thanks for the alternative code! But I get an exception caught at line

string NewID = ID.Remove(36);

What is the idea of 36?? Please explain.

Thanks, Akshay

Akshay
A: 

Here's a more succinct code snippet. Main changes are removal of unused objects, and a more efficient version of the foreach loop.

protected override void CreateChildControls()
{
  dropdown = new DropDownList();
  dropdown.Items.Add(new ListItem("Select...", ""));

  SPWeb currentWeb = SPContext.Current.Site.RootWeb;
  SPListItemCollection scItems = currentWeb.Lists["Shortcuts"].Items;

  AudienceLoader audienceLoader = AudienceLoader.GetAudienceLoader();

  // Iterate over a copy of the collection to prevent multiple queries to the list
  foreach (SPListItem listItem in scItems)
  {
    string audienceFieldValue = (string)listItem["Target Audiences"];

    if (AudienceManager.IsCurrentUserInAudienceOf(audienceLoader, audienceFieldValue, false))
    {
      dropdown.Items.Add(new ListItem(listItem.Title, listItem.Url));
    }
  }

  Controls.Add(dropdown);
  base.CreateChildControls();
}
CBono