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

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: