Enabling the Audience Targeting through Object Model - MOSS SharePoint 2007

 The "Audience targeting settings" is enabled in the “Listenabletargeting.aspx” application page. In the code behind of this page the method "btnsave_click" is called to set the Audience targeting. This method is available in the Microsoft.SharePoint.Portal.Audience.AdminUI.ListEnableTargetingPage class in Microsoft.Sharepoint.Portal.Dll

In MOSS whenever the "audience targeting" is enabled in the List a new field of type "Target Audience" is created in the List where you can set the audiences for the list item. The GUID of this target audience field is “ 61cbb965-1e04-4273-b658-eedaa662f48d”.

So to enable the audience targeting through object model first need to create a field of type “Target Audience” in the List. Following is the code snippet to enable the audience targeting through SharePoint API

SPSite site = new SPSite(<Site URL>);

            SPWeb web = site.OpenWeb();

            SPList list = web.Lists["Tasks"];

            SPField field ;

            field = null;

            try

            {

     field = list.Fields[new Guid("61cbb965-1e04-4273-b658-eedaa662f48d")];

            }

            catch(ArgumentException ex)

            {

 

            }

            if (field == null)

            {

                string createFieldAsXml = GetCreateFieldAsXml(); // See below for the GetCreateFieldAsXML() method

                list.Fields.AddFieldAsXml(createFieldAsXml);

                list.Update();

            }

            Audience siteAudience;

            ServerContext context = ServerContext.GetContext(site);

  AudienceManager audManager = new AudienceManager(context);

               

            foreach (SPListItem item in list.Items)

            {

                siteAudience = audManager.GetAudience("<audience name created in the SSP>");

          Guid id = siteAudience.AudienceID;

                item["Target Audiences"] = id.ToString()+";;;;";

                item.Update();

               

            }

Following is the method to get the Audience target field as XML :

 internal string GetCreateFieldAsXml()

        {

            XmlElement element = new XmlDocument().CreateElement("Field");

            element.SetAttribute("ID", "61cbb965-1e04-4273-b658-eedaa662f48d");

            element.SetAttribute("Type", "TargetTo");

            element.SetAttribute("Name", "TargetTo");

            element.SetAttribute("DisplayName", "Target Audiences");

            element.SetAttribute("Required", "FALSE");

            return element.OuterXml;

        }

Thus you can set the audience targeting through object model using SharePoint API.

Following code snippet helps to retrieve the audience targeting value and its self explanatory J

SPSite site = new SPSite(<Site URL>);

            SPWeb web = site.OpenWeb();

            SPList list = web.Lists["Tasks"];

            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);

            }

Hope this helps for someone who is in need to set/get the audience targeting through object model.