Audience targeting in SharePoint is a great way of hiding irrelevant information from users and likewise, only show relevant information. You could create a list with a lot of items and target those items to specific groups of users. Users who are targeted will see the items while other users will not see them… audience targeting. This has nothing to do with permissions. It’s just a filter. Users who are not targeted can still access those items if they want.
To be able to use audience targeting on a list, you have to enable it first. It’s disabled by default. To enable this, you can go to the list settings of your list and open the Audience Targeting settings. There you will find a checkbox to enable this.
Once it’s enabled, you will see an extra column in your list “Target Audiences” of the type “Audience Targeting”.
To disable it, go back into the audience targeting settings and uncheck the checkbox. The extra column will be removed.
That’s the UI way… now, how about PowerShell? Can you enable/disable audience targeting? Offcourse!
We only need to add a field with a specific ID and attributes to the list and it’s done.
The script below enables audience targeting for a list.
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
<# .SYNOPSIS Enables audience targeting on a SharePoint list or library. .DESCRIPTION Enables audience targeting on a SharePoint list or library. This will add an extra column to all associated content types. This makes it important to do this AFTER you added the content types to the list or library. .NOTES File Name: Enable-AudienceTargetingOnList.ps1 Author : Bart Kuppens Version : 1.0 .PARAMETER Web Specifies the URL for the web where the library is located. .PARAMETER ListName Specifies the name of the list where you want to enable audience targeting. .EXAMPLE PS > .\Enable-AudienceTargeting.ps1 -Web http://teamsites.westeros.local -ListName Documents Description ----------- Enables audience targeting on the "Documents" library on http://teamsites.westeros.local. #> [CmdletBinding()] param( [parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false,HelpMessage="Specifies the URL for the web where the library is located.")] [string]$Web, [parameter(Position=1,Mandatory=$true,ValueFromPipeline=$false,HelpMessage="Specifies the name of the list where you want to enable audience targeting.")] [string]$ListName ) # Load the SharePoint PowerShell snapin if needed if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null) { Write-Host "Loading the SharePoint PowerShell snapin..." Add-PSSnapin Microsoft.SharePoint.PowerShell } $SPWeb = Get-SPWeb $Web -EA SilentlyContinue if ($SPWeb -eq $null) { Write-Error "$Web is not a valid SharePoint Web" } else { Try { $fieldID = "61cbb965-1e04-4273-b658-eedaa662f48d" [Guid]$AudFieldID = New-Object System.Guid($fieldID) $list = $SPWeb.Lists[$ListName] if ($list -ne $null) { # Check if audience targeting is already enabled on this list. $audField = $list.Fields[$AudFieldID] if ($audField -eq $null) { # Not yet enabled, enable it $xmlField = New-Object System.Xml.XmlDocument $field = $xmlField.CreateElement("Field") $field.SetAttribute("ID",$fieldID) $field.SetAttribute("Type","TargetTo") $field.SetAttribute("Name","TargetTo") $field.SetAttribute("DisplayName","Target Audiences") $field.SetAttribute("Required","FALSE") $list.Fields.AddFieldAsXml($field.OuterXml) >> $null $list.Update() Write-Host -ForegroundColor Green "Audience targeting is succesfully enabled on '$ListName'" } else { Write-Host -ForegroundColor Yellow "Audience targeting is already enabled on '$ListName'" } } else { Write-Host "The list with the name $ListName was not found on $($SPWeb.Url)" } } catch { Write-Error $_.Exception } finally { $SPWeb.Dispose() } } |
The script below disables audience targeting for a list.
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
<# .SYNOPSIS Disables audience targeting on a SharePoint list or library. .DESCRIPTION Disables audience targeting on a SharePoint list or library. This will delete the audience targeting column from the list. All information contained in that column, will be lost! .NOTES File Name: Disable-AudienceTargetingOnList.ps1 Author : Bart Kuppens Version : 1.0 .PARAMETER Web Specifies the URL for the web where the library is located. .PARAMETER ListName Specifies the name of the list where you want to disable audience targeting. .EXAMPLE PS > .\Disable-AudienceTargeting.ps1 -Web http://teamsites.westeros.local -ListName Documents Description ----------- Disables audience targeting on the "Documents" library on http://teamsites.westeros.local #> [CmdletBinding()] param( [parameter(Position=0,Mandatory=$true,ValueFromPipeline=$false,HelpMessage="Specifies the URL for the web where the library is located.")] [string]$Web, [parameter(Position=1,Mandatory=$true,ValueFromPipeline=$false,HelpMessage="Specifies the name of the list where you want to enable audience targeting.")] [string]$ListName ) # Load the SharePoint PowerShell snapin if needed if ((Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null) { Write-Host "Loading the SharePoint PowerShell snapin..." Add-PSSnapin Microsoft.SharePoint.PowerShell } $SPWeb = Get-SPWeb $Web -EA SilentlyContinue if ($SPWeb -eq $null) { Write-Error "$Web is not a valid SharePoint Web" } else { Try { $fieldID = "61cbb965-1e04-4273-b658-eedaa662f48d" [Guid]$AudFieldID = New-Object System.Guid($fieldID) $list = $SPWeb.Lists[$ListName] if ($list -ne $null) { # Check if audience targeting is enabled on this list. $audField = $list.Fields[$AudFieldID] if ($audField -ne $null) { # It's enabled, disable it. $list.Fields.Delete($audField.InternalName); $list.Update() Write-Host -ForegroundColor Green "Audience targeting is succesfully disabled on '$ListName'" } else { Write-Host -ForegroundColor Yellow "Audience targeting is not enabled on '$ListName'" } } else { Write-Host "The list with the name $ListName was not found on $($SPWeb.Url)" } } catch { Write-Error $_.Exception } finally { $SPWeb.Dispose() } } |