Home > Uncategorized > How to programmatically set the audience targeting on a web part (WSS 3.0)

How to programmatically set the audience targeting on a web part (WSS 3.0)

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 “;;;;”.


Categories: Uncategorized Tags:
  1. Dirk
    April 3rd, 2012 at 01:23 | #1

    This appears to be exactly what I need. I do have a question though.
    I’m using Vs2008 but it does not seem to know the SiteUserManager Class. What library do I need to reference for that?

  2. Gargi
    September 4th, 2012 at 06:46 | #2

    Hi Reza,
    I want to display the target audience value of web part as output and for that need to write PowerShell script. Can you please guide me for same?

    Thanks,
    Gargi

You must be logged in to post a comment.