Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

How to: Create a Rules-Based Audience

Last modified: January 27, 2010

Applies to: SharePoint Server 2010

Microsoft SharePoint Server 2010 supports content targeting based on audience rules, SharePoint groups, and Microsoft Exchange distribution list (DL) memberships. In addition, SharePoint Server 2010 provides the ability to target content at the list item level, rather than just at the list level.

This code example shows how to create a rules-based audience to which you can target content.

Note Note

The audience is only created here; it is not compiled, and rules have not been added. You can add rules by using the user interface or the object model, and then compile the audience by using the user interface. Compiling audiences is not supported in the object model.

Replace servername and other strings with actual values before running the code example. Also add the following references in your Microsoft Visual Studio project:

  • Microsoft.Office.Server

  • Microsoft.Office.Server.UserProfiles

  • Microsoft.SharePoint

  • System.Web

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.Office.Server.Audience;
using Microsoft.SharePoint;
using Microsoft.Office.Server;
using System.Web;

namespace AudienceConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (SPSite site = new SPSite("http://servername"))
                {
                    SPServiceContext context = SPServiceContext.GetContext(site);
                    AudienceManager audManager = new AudienceManager(context);

                    AudienceCollection ac = audManager.Audiences;
                    Audience a = null;
                    string sAudName = "Customer Connection";
                    string sDescription = "Members of the Customer Connection v-team";

                    try
                    {
                        a = ac.Create(sAudName, sDescription);
                    }
                    catch (AudienceDuplicateNameException e)
                    {
                        //Your exception handling code here
                    }
                    catch (AudienceException e1)
                    {
                        //Your exception handling code here
                    }
                }

            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
                Console.Read();
            }

        }
    }


}
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.