-
Notifications
You must be signed in to change notification settings - Fork 11
/
MapCrawledPropertyToManagedProperty.ps1
161 lines (133 loc) · 5.03 KB
/
MapCrawledPropertyToManagedProperty.ps1
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<#
Author: Mikael Svenson - techmikael.com - @mikaelsvenson
Date: November 2020
Summary: Script to map or clear crawled properties to reusable managed properties
Usage: MapCrawledPropertyToManagedProperty.ps1 -siteUrl https://tenant.sharepoint.com/sites/site -managedProperty RefinableString00 -crawledProperty ows_<columnName>
Clear/reset a mapping with: MapCrawledPropertyToManagedProperty.ps1 -siteUrl https://tenant.sharepoint.com/sites/site -managedProperty RefinableString00
Use -appendToExistingMapping:$false if you want to overwrite an existing mapping instead of adding to it.
Connect to https://tenant-admin.sharepoint.com for tenant wide mapping
#>
param (
[Parameter(Mandatory = $true)]
[string]$siteUrl,
[Parameter(Mandatory = $true)]
[string]$managedProperty,
[string]$crawledProperty,
[bool]$appendToExistingMapping = $true,
[bool]$interactiveLogin = $false,
[bool]$printConfig = $false
)
function Load-Module ($m) {
# If module is imported say that and do nothing
if (Get-Module | Where-Object { $_.Name -eq $m }) {
write-host "Module $m is already imported."
}
else {
# If module is not imported, but available on disk then import
if (Get-Module -ListAvailable | Where-Object { $_.Name -eq $m }) {
Import-Module $m -Verbose
}
else {
# If module is not imported, not available on disk, but is in online gallery then install and import
if (Find-Module -Name $m | Where-Object { $_.Name -eq $m }) {
Install-Module -Name $m -Force -Verbose -Scope CurrentUser
Import-Module $m -Verbose
}
else {
# If module is not imported, not available and not in online gallery then abort
write-host "Module $m not imported, not available and not in online gallery, exiting."
EXIT 1
}
}
}
}
Load-Module "PnP.PowerShell"
if( $interactiveLogin ) {
Connect-PnPOnline -Url $siteUrl -UseWebLogin
} else {
Connect-PnPOnline -Url $siteUrl
}
$validNames = @("Int", "Date", "Decimal", "Double", "RefinableInt", "RefinableDate", "RefinableDateSingle", "RefinableDateInvariant", "RefinableDecimal", "RefinableDecimal", "RefinableString");
if (($crawledProperty.Length -ne 0) -and -not ($crawledProperty -match '^ows_')) {
Write-Warning "Crawled property has to start with ows_";
return
}
if ($crawledProperty -match '^(ows_taxId|ows_r|ows_q)') {
Write-Warning "Script only support regular crawled properties ows_<field name>";
return
}
$mp = $managedProperty | Select-String -Pattern '^(?<name>(Refinable|Double|Decimal|Date|Int)[a-z]*)(?<num>\d+)$'
if ($mp.Matches.Length -eq 0) {
Write-Warning "Script only support reusable managed properties";
return
}
$mpNumber = $mp.Matches[0].Groups['num'].Value
$basePid = 0;
if ($managedProperty -match "^RefinableString1" -and $mpNumber.length -eq 3 ) {
$basePid = 1000000900
$mpNumber = $mpNumber - 100;
}
elseif ($managedProperty -match "^RefinableDouble") {
$basePid = 1000000800
}
elseif ($managedProperty -match "^RefinableDecimal") {
$basePid = 1000000700
}
elseif ($managedProperty -match "^RefinableDateInvariant") {
$basePid = 1000000660
}
elseif ($managedProperty -match "^RefinableDateSingle") {
$basePid = 1000000660
}
elseif ($managedProperty -match "^RefinableDate") {
$basePid = 1000000600
}
elseif ($managedProperty -match "^RefinableInt") {
$basePid = 1000000500
}
elseif ($managedProperty -match "^Double") {
$basePid = 1000000400
}
elseif ($managedProperty -match "^Decimal") {
$basePid = 1000000300
}
elseif ($managedProperty -match "^Date") {
$basePid = 1000000200
}
elseif ($managedProperty -match "^Int") {
$basePid = 1000000100
}
elseif ($managedProperty -match "^RefinableString") {
$basePid = 1000000000
}
$mpPid = $basePid + [int]$mpNumber
$scriptDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
if ($scriptDir.Length -eq 0) {
$scriptDir = "."
}
if ($crawledProperty.Length -eq 0) {
Write-Host "Resetting crawled property mapping for $managedProperty"
$config = Get-Content -Path "$scriptDir\SearchMappingReset.xml" -Raw
}
else {
if($appendToExistingMapping) {
Write-Host "Appending crawled property $crawledProperty to managed property $managedProperty"
} else {
Write-Host "Replacing crawled property $crawledProperty to managed property $managedProperty"
}
$config = Get-Content -Path "$scriptDir\SearchMappingTemplate.xml" -Raw
}
$config = $config -replace "##PID##", $mpPid
$config = $config -replace "##CPNAME##", $crawledProperty
$config = $config -replace "##APPEND##", $appendToExistingMapping.ToString().ToLower()
if($printConfig) {
$config
return
}
if ($siteUrl -like "*-admin*") {
Set-PnPSearchConfiguration -Scope Subscription -Configuration $config
}
else {
Set-PnPSearchConfiguration -Scope Site -Configuration $config
}
Write-Host "Remember to re-index items after you have done a schema change"