SharePoint, XBOX, .NET, Technology - What I am reading

    [Home] [Recent] [Site Map]

   

How Rules in Audiences work

For a big customer we had to create about 80 different audiences, which should be deployed on multiple server (test, acceptance, production, etc.). Each audience have rules to describe if a user is part of the audience or not. Creating audiences is very easy, just provide a name and description. Rules however need some more attention.

Time to write a script for that! So, first of al, I searched the SDK for some more information about the way rules are used within the audiences. Basically you can add rules programatically like this:

using (SPSite site = new SPSite("http://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 and Joe Connection"];
          }
          catch (AudienceArgumentException ex)
           {
               //your exception handling code here
           }

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

          try
          {
              //if the rule is not emply, start with a group operator "AND" to append
             if (ruleListNotEmpty)
             {
                  aRules.Add(new AudienceRuleComponent(null, "AND", null));
             }

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

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

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

             AudienceRuleComponent r5 = new AudienceRuleComponent(null, "OR", null);
             aRules.Add(r5);

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

             AudienceRuleComponent r7 = new AudienceRuleComponent("FirstName", "Contains", "Joe");
             aRules.Add(r7);

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

             AudienceRuleComponent r9 = new AudienceRuleComponent("WorkEmail", "Contains", "someexample.com");
             aRules.Add(r9);

             AudienceRuleComponent r10 = new AudienceRuleComponent(null, ")", null);
             aRules.Add(r10);
             a.AudienceRules = aRules;
             a.Commit();
           }
           catch (AudienceException e)
           {
               //Your exception handling code here
           }
     }
}

Quite a flexible way to deal with rules. However, there are three parts that you have to deal with:

This way you can add change and construct audiences easily.


>>Source Link
>>Blog: Mart Muller_s Sharepoint Weblog
>>Publish Date: 10/8/2007 10:18:54 PM
>>Keywords: null arules.add

Related Posts
>>How to set user permissions programmatically? #
    SPSite site = null;SPWeb web = null;site = new SPSite("http://server:100/sites/DevSite/");web = site.OpenWeb();SPRoleAssignment roleAssignment = new SPRoleAssignment("domain\\user","alias@domain.com
>>Get the current variation label root URL #
    Here"s a way to create an URL relative to the current variation label:ReadOnlyCollection<VariationLabel> lst = Variations.Current.UserAccessibleLabels; Uri curUrl = new Uri(SPContext.Current.Web
>>XML Schema Facets #
    I have received a number of requests over a period of time asking how to get all the facets defined on simple types or complex types with simple content in an XML Schema. I recently wrote a code sampl
>>Manual Reghosting TSQL #
    I found a utility on gotdotnet.com that allows for detecting and reghosting WSS 2 ASPX pages that have been customized in FrontPage 2003. A simple TSQL trace gave insight into the simple method used
>>Bubbling Windows Forms Events With Anonymous Methods #
    The following code illustrates a great use for anonymous methods.public class MyContainerControl : Control { private class MySpecializedControl : Control { protected virtual void OnMyE
>>How to Query a Service Status #
    Sometimes its essential to query service status. If you are doing so on Vista then this is a little tricky. Standard users who are part of administrator"s group would not have all the required access
>>Recursive FindControl #
    Need to find a control anywhere on the page or in a template? Inspired by Steve Smith's implementation, here is the code to find it recursively. This example assumes the code is located in a cus
>>Guardar un site como plantilla, más conocido como "Save Site as Template"... #
    Volviéndome loco buscando la opción de "Save Site as Template" en MOSS 2007 (estaba utilizando una plantilla tipo SPS) descubrí que, no sé muy bien por qué, no había forma de encontrar esta opción...
>>get-datatable.ps1 #
    get-datatable.ps1 is an improvement on the get-dataset.ps1 from an earlier post. Instead of returning a dataset it returns a DataTable; and a UpdateSql method is added to the object returned, so you d
>>Retrieving all properties for a Sharepoint list #
    When we call the GetListItems or GetListItemChanges methods in Sharepoint"s Lists Web Service, we pass an XmlNode parameter specifying which fields we want to return, in this format: <ViewFields>

Related Posts:
>>AKS Update
>>MOSS (internet) licensing changed!
>>WSS 2.0 and SPS 2003 Service Pack 3 (SP3) released
>>MS Information Worker Conferences
>>New Version of Ontolica Search available (3.2)
>>TamTam.SharePoint.AudienceCreator 1.0 released on codeplex
>>New community site focusing on Enterprise Search
>>SharePoint 2007 limitations
>>CruiseControl Webparts v1.2
>>SharePoint Solution (WSP) creation tool
>>MOSS 2007: Best Practices when upgrading
>>Biometric Authentication For SharePoint


Month Archives:

Top Tags:
social software social networking .NET mashable Sharepoint ASP.NET Web 2.0 Web2.0 Startups Community News Search Marketplace General Software Development AJAX Windows Vista Microsoft Visual Studio myspace People Powered! YouTube Silverlight Vista Featured News MOSS C# Events Google WPF MOSS 2007 Office 2007 Web



@2007 All rights Reserved