Enable Audience Targeting for SharePoint list using PowerShell

In this article we will be seeing how to enable audience targeting for SharePoint list using PowerShell.

Go to List => List Settings => General Settings => Audience targeting settings.


















You can add the audience targeting field by enabling the "Enable audience targeting".






Using C#

  1. using System;

  2. using System.Collections.Generic;

  3. using System.Linq;

  4. using System.Text;

  5. using Microsoft.SharePoint;

  6. using System.Xml;

  7. namespace AudienceTargeting

  8. {

  9. class Program

  10.    

  11. {

  12. static void Main(string[] args)       

  13. {

  14. using (SPSite site = new SPSite("http://serverName:1111/"))           

  15. {

  16. using (SPWeb web = site.RootWeb)               

  17. {                 

  18. SPList list=web.Lists["cl"];

  19. XmlElement fldElement = new XmlDocument().CreateElement("Field");                   

  20. fldElement.SetAttribute("ID", "61cbb965-1e04-4273-b658-eedaa662f48d");                   

  21. fldElement.SetAttribute("Type", "TargetTo");                   

  22. fldElement.SetAttribute("Name", "TargetTo");                   

  23. fldElement.SetAttribute("DisplayName", "Target Audiences");                   

  24. fldElement.SetAttribute("Required", "FALSE");                    

  25. list.Fields.AddFieldAsXml(fldElement.OuterXml);                   

  26. list.Update();                

  27. }           

  28. }       

  29. }   

  30. }

  31. }

Using PowerShell

  1. $site=Get-SPSite "http://serverName:1111/"

  2. $web=$site.RootWeb

  3. $list=$web.Lists["cl"]

  4. $xmldata = new-object "System.Xml.XmlDocument"

  5. $fldElement =$xmldata.CreateElement("Field")

  6. $fldElement.SetAttribute("ID", "61cbb965-1e04-4273-b658-eedaa662f48d")

  7. $fldElement.SetAttribute("Type", "TargetTo")

  8. $fldElement.SetAttribute("Name", "TargetTo")

  9. $fldElement.SetAttribute("DisplayName", "Target Audiences")

  10. $fldElement.SetAttribute("Required", "FALSE")

  11. $list.Fields.AddFieldAsXml($fldElement.OuterXml)

  12. $list.Update()

Recent Posts

See All