Wednesday, September 17, 2008

Programatically adding Audience

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

namespace LearnSharePoint
{
class Audience
{
static void Main(string[] args)
{
try
{
using (SPSite spSite = new SPSite(@"http://localhost"))
{
ServerContext siteContext = ServerContext.GetContext(spSite);
AudienceManager amManager = new AudienceManager(siteContext);

AudienceCollection acCollection = amManager.Audiences;
string strName = "HR Department";
string strDescription = "All members of the human resource department";

if (!acCollection.AudienceExist(strName))
{
acCollection.Create(strName, strDescription);
}

foreach (Audience tempAudience in acCollection)
{
Console.WriteLine(tempAudience.AudienceName);
Console.WriteLine(tempAudience.AudienceSite);
Console.WriteLine(tempAudience.MemberShipCount);
}
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}
}
}
}

0 comments: