Reader Level:
Articles

Enable Audience Targeting for SharePoint list using PowerShell

By Vijai Anand on Apr 11, 2011
In this article we will be seeing how to enable audience targeting for SharePoint list using PowerShell.
  • 0
  • 0
  • 7574

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()

Vijai Anand

Vijai Anand has been working in IT industry for over 5 years. He holds Bachelor's degree in Electronics and Communication Engineering. Vijai write articles and blogs related to SharePoint 2013, SharePoint 2010, Silv... Read more

COMMENT USING