SPWebPartCollection and IsIncludedFilter are both obsolete in V3, so I will use other new methods and properties to set the audience targeting property to a SharePoint Site Group which is part of “All Site Users” audience by default.
SPWeb web = new SPSite("http://server/mysite").OpenWeb();
SPLimitedWebPartManager wm = web.GetLimitedWebPartManager("default.aspx", System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
foreach (WebPart wp in webParts)
{
SiteUserManager sum = wp as SiteUserManager;
if (sum != null && string.CompareOrdinal(sum.Title, wp.Title) == 0)
{
sum.AuthorizationFilter = ";;;;" + web.AssociatedOwnerGroup.Name ;
wm.SaveChanges(sum);
break;
}
}
AuthorizationFilter is an arbitrary string to determine whether a WebPart control is authorized to be added to a page or not. You either have to give it a valid GUID of your audience or like what I did above provide the name of the group prefixed by “;;;;”.