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.

Eshare1.gif

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

Eshare2.gif

Using C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Xml;

namespace AudienceTargeting
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPSite site = new SPSite("http://serverName:1111/"))
            {
                using (SPWeb web = site.RootWeb)
                {                 
                    SPList list=web.Lists["cl"];
                    XmlElement fldElement = new XmlDocument().CreateElement("Field");
                    fldElement.SetAttribute("ID", "61cbb965-1e04-4273-b658-eedaa662f48d");
                    fldElement.SetAttribute("Type", "TargetTo");
                    fldElement.SetAttribute("Name", "TargetTo");
                    fldElement.SetAttribute("DisplayName", "Target Audiences");
                    fldElement.SetAttribute("Required", "FALSE");
                    list.Fields.AddFieldAsXml(fldElement.OuterXml);
                    list.Update();
                }
            }
        }
    }
}

Using PowerShell

$site=Get-SPSite "http://serverName:1111/"
$web=$site.RootWeb
$list=$web.Lists["cl"]
$xmldata = new-object "System.Xml.XmlDocument"
$fldElement =$xmldata.CreateElement("Field")
$fldElement.SetAttribute("ID", "61cbb965-1e04-4273-b658-eedaa662f48d")
$fldElement.SetAttribute("Type", "TargetTo")
$fldElement.SetAttribute("Name", "TargetTo")
$fldElement.SetAttribute("DisplayName", "Target Audiences")
$fldElement.SetAttribute("Required", "FALSE")
$list.Fields.AddFieldAsXml($fldElement.OuterXml)
$list.Update()

Up Next
    Ebook Download
    View all
    Learn
    View all