Currently there are no direct methods to enable Audience Targeting on a List or Library via script or code.

This is currently done through the List or Library Settings screen under Audience targetting settings option

When you enable audience targeting through these settings, a field is created in the corresponding List or Library called Target Audiences

Using the Get-PnpField PnP PowerShell command to get the SchemaXml property we can see how the field is constructed.

1
(Get-PnPField -Identity "Target Audiences" -List Documents).SchemaXml

This is the XML returned

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

<Field
ID="{61cbb965-1e04-4273-b658-eedaa662f48d}"
Type="TargetTo"
Name="Target_x0020_Audiences"
DisplayName="Target Audiences"
Required="FALSE"
SourceID="{803a362c-54fb-47c7-83c8-e7a83a9512f8}"
StaticName="Target_x0020_Audiences"
ColName="ntext2"
RowOrdinal="0" Version="2">
<Customization>
<ArrayOfProperty>
<Property>
<Name>AllowGlobalAudience</Name>
<Value xmlns:q1="http://www.w3.org/2001/XMLSchema" p4:type="q1:bo
olean" xmlns:p4="http://www.w3.org/2001/XMLSchema-instance">true</Value>
</Property>
<Property>
<Name>AllowDL</Name>
<Value xmlns:q2="http://www.w3.org/2001/XMLSchema" p4:type="q2:boolean" xmlns:p4="http://www.w
3.org/2001/XMLSchema-instance">true</Value>
</Property>
<Property>
<Name>AllowSPGroup</Name>
<Value xmlns:q3="http://www.w3.org/2001/XMLSchema" p4:type="q3:boolean" xmlns:p4="http://www.w3.org/2001/XMLSchema-ins
tance">true</Value>
</Property>
</ArrayOfProperty>
</Customization>
</Field>

After some experimenting, I found that the only two columns you need to set are the ID and Type

ID needs to have 61cbb965-1e04-4273-b658-eedaa662f48d set as it’s value

Type needs to have TargetTo set as it’s value

You still need to provide values for Name, DisplayName, StaticName, but they can be whatever you want.

Don’t like the name Target Audiences then you can change it to something else. The great thing is, it can still be turned off through the List or Library settings page.

To enable Audinence targeting using PnP PowerShell it’s as simple as running this script.

1
2
3
4
5
6

$credentials = (Get-Credential)

Connect-PnPOnline -Url https://contoso.sharepoint.com/sites/somesite -Credentials $credentials

Add-PnPFieldFromXml '<Field ID="{61cbb965-1e04-4273-b658-eedaa662f48d}" Type="TargetTo" Name="Target_x0020_Audiences" StaticName="Target_x0020_Audiences" DisplayName="Target Audiences" />' -List Documents

And to do the same thing in C# CSOM

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

// from SharePointPnPCoreOnline Nuget package
var manager = new OfficeDevPnP.Core.AuthenticationManager();

string siteUrl = "https://contoso.sharepoint.com/sites/somesite";
string appId = "your-app-id";
string appSecret = "your-app-secret";

using (var context = manager.GetAppOnlyAuthenticatedContext(siteUrl, appId, appSecret))
{

var list = context.Web.Lists.GetByTitle("Documents");

var fieldSchemaXml = @"
<Field
ID='{61cbb965-1e04-4273-b658-eedaa662f48d}'
Type='TargetTo'
Name='Target_x0020_Audiences'
StaticName='Target_x0020_Audiences'
DisplayName='Target Audiences'
/>
";

var field = list.Fields.AddFieldAsXml(fieldSchemaXml, false, AddFieldOptions.AddFieldInternalNameHint);
context.ExecuteQueryRetry();
}

To turn off Audience Targeting all you need to do is remove the field from the list.

Comment and share

I’ve often had times, where I’ve migrated a whole load of documents from a File Server or some other ECMS. These exported documents are normally in major published state and need to be in the same state in SharePoint (i.e. Published!).

Sweet and simple, but very effective.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
$sites = "https://contoso.sharepoint.com/sites/site1", "https://contoso.sharepoint.com/sites/site1"

foreach ($site in $sites)
{
Connect-PnPOnline -Url $site -UseWebLogin

$list = Get-PnPList -Identity "Documents"

$items = (Get-PnPListItem -List $list -Fields "Title","GUID","FileRef").FieldValues

foreach ($item in $items)
{
$file = $item.FileRef

Set-PnPFileCheckedIn -Url $file -CheckinType MajorCheckIn -Approve
}
}

Comment and share

  • page 1 of 1
Author's picture

Toby Statham

Independent Office 365 / SharePoint Specialist and an associate consultant at aiimi.com, an Information Management company.

Independent Office 365 / SharePoint Specialist

Brighton, UK