Click here to Skip to main content
14,359,553 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi All,

Is it any way we can set the Target Audience setting programmatically using CSOM ?

I know we can do through server object like in the below url:
http://weblogs.mysharepoint.de/blogs/owirkus/archive/2013/02/20/sharepoint-set-target-audience-to-navigation-nodes-by-code.aspx

I am not able to get the properties for setting the target audience in CSOM.

Please let me know.


Regards,
Saravanakumar D
Posted
Updated 11-Jul-17 21:35pm
v2
Comments
phil.o 1-Oct-15 5:35am
   
Please define 'not able to get the properties'. What have you tried? Where are you stuck?

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

Here is the code :

private static void SetWebpartTargetAudience(ClientContext sp2013Context)
{
var pageUrl = "/sites/demokm/Pages/Test.aspx";

try
{
//get page from page url
Microsoft.SharePoint.Client.File page = sp2013Context.Web.GetFileByServerRelativeUrl(pageUrl);
sp2013Context.Load(page, p => p.Properties, p => p.CheckOutType);
sp2013Context.ExecuteQuery();

//Check Out page, if page is checked-In
if (page.CheckOutType == CheckOutType.None)
{
page.CheckOut();
}

LimitedWebPartManager wpm = page.GetLimitedWebPartManager(PersonalizationScope.Shared);

sp2013Context.Load(wpm.WebParts, wps => wps.Include(wp1 => wp1.WebPart.Title, wp1 => wp1.Id));
sp2013Context.ExecuteQuery();

WebPartDefinition wpd = wpm.WebParts[0];

sp2013Context.Load(wpd, w => w.WebPart.Properties);
sp2013Context.ExecuteQuery();
//single target audience i.e sharepoint group
wpd.WebPart.Properties["AuthorizationFilter"] = ";;;;" + "SharePoint Group Name";
//multiple target audience i.e sharepoint groups
//wpd.WebPart.Properties["AuthorizationFilter"] = ";;;;" + "SharePoint Group Name1,SharePoint Group Name";
wpd.SaveWebPartChanges();
sp2013Context.Load(wpd);
page.CheckIn("checkin comments", CheckinType.MajorCheckIn);
sp2013Context.ExecuteQuery();
}
catch (Exception)
{
throw;
}
}

//Call this method
SetWebpartTargetAudience(SharePointContext);
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100