Share via


How to: Add Simple Rules to an Audience Using AND and OR Operators

The user interface provides you with only two options for defining rules when you create audiences: include users that satisfy all rules, or include users that satisfy any rules. Also, when using the user interface, you are restricted to a maximum of six rules. Although this might be enough for most situations, some times you will need to have more than six rules.

In such cases, you can use the Audience object model. The Audience object model allows you to create more than six rules for an audience. The following code example adds simple rules for an audience called "John Connection". This example uses the AND operator to combine multiple rules. If you want just one of the rules to be true for the members, you can use the OR operator in place of AND.

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.SharePoint

  • System.Web

Example

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;
using System.Collections;

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

                    AudienceCollection ac = AudMgr.Audiences;
                    Audience a = null;
                    bool ruleListNotEmpty = false;

                    try
                    {
                        a = AudMgr.Audiences["John Connection"];
                    }
                    catch (AudienceArgumentException ex)
                    {
                        //your exception handling code here
                    }

                    ArrayList aRules = a.AudienceRules;
                    if (aRules == null)
                    {
                        aRules = new ArrayList();
                    }
                    else
                    {
                        ruleListNotEmpty = true;
                    }


                    try
                    {
                        if (ruleListNotEmpty)
                        {
                            aRules.Add(new AudienceRuleComponent(null, "AND", null));
                        }


                        AudienceRuleComponent r1 = new AudienceRuleComponent("FirstName", "Contains", "John");
                        aRules.Add(r1);

                        AudienceRuleComponent r2 = new AudienceRuleComponent(null, "AND", null);
                        aRules.Add(r2);

                        AudienceRuleComponent r3 = new AudienceRuleComponent("WorkEmail", "Contains", "example.com");
                        aRules.Add(r3);
                        a.AudienceRules = aRules;
                        a.Commit();
                    }
                    catch (AudienceException e)
                    {
                        //Your exception handling code here
                    }
                }

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

        }
    }


}

See Also

Other Resources

Targeting Content Using Audiences