Tuesday, February 23, 2010

Set audience targeting programmatically

Hi All,

Today I am going to explain you about audience targeting. However I am going to share in terms of programming aspects. Because we all know that we can create our audience in central administration and then simple apply them on list items or document library items or on web parts on pages.

However we will see it this time how to do the same using code.

I assume that you have already created audience named “Sales” and “Finance”. I am not going in deep discussion explaining you each and every class and methods used in it. You may query me your doubts and I will be keen to reply you back.

using (SPWeb objsite = (SPWeb)properties.Feature.Parent)
{
using (SPLimitedWebPartManager wpm
= objsite.GetLimitedWebPartManager("default.aspx", PersonalizationScope.Shared))
{
AudienceManager am = new AudienceManager(ServerContext.Current);

wpm.WebParts[0].AuthorizationFilter
= string.Format("{0};;;;", am.GetAudience("Sales").AudienceID);

wpm.SaveChanges(wpm.WebParts[0]);
}
}


We first take our web context and then take out the default.aspx web parts. We use webpartmanager to deal with them. Then take out Audience manager class with the context, and then interesting part comes. Authorizationfilter takes argument in such way that we have to pass ;;;; in it after getting the audience target ID. Get the webpart that you want and pass its Title let’s say in wpm.WebParts[0].AuthorizationFilter line. I’ve used 0 just to get the first webpart and show you the demo.

Here we want to target sales people, so we have passed Sales and get the ID of it and finally call up the savechanges method of webpartmanager object.

As simple as that.

Hummm…What about Finance people. Well there you go, you can set multiple audience in one go.

Just change the line to.

wpm.WebParts[0].AuthorizationFilter
= string.Format("{0},{1};;;;", am.GetAudience("Sales").AudienceID, am.GetAudience("Finance").AudienceID);

Your job is done.

5 comments:

Anonymous said...

Can i get visitor count also. using audience targeting

SharePoint Kings said...

What do you mean by visitor?

Prakash Ja said...

Hello Sharepoint Kings Team,

First of all, thanks for sharing this excellent article on setting audience targeting programmatically. However, my question is how to set audience as SPGroup.

You know that we can set target audience a sharepoint group so how do I set sharepoint group as target audience for a webpart ?

I look forward to your reply.

Thanks,

Khushi said...

Hi there,

I have a field of type of Audience on the document library. I want to get the list of documents if the user falls in the Target Audience.

Could you pls guide/
thanks
Khushi.

SharePoint Kings said...

Khushi,
not sure your exact requirement but may be create an view where user in that column.

else do it programmatic way in event handler give permission to only specific users and remove other user permission.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com