Ampersand issue when checking term name in SharePoint using PowerShell

时间:2018-11-16 21:38:16

标签: sharepoint sharepoint-online powershell-v4.0

I am creating terms into SharePoint using PowerShell online. I have no issues in terms of adding new term for the first time.

If I check next time if the term exists and the ampersand &amp converts to different hex in SharePoint as a result it fails in the comparison in PowerShell.

I found the TaxonomyItem.NormalizeName(String) Method which describes that I can normalize my text. But I am getting an error

Unable to find type [Microsoft.SharePoint.Client.Taxonomy].

What I have in my PowerShell script is:

$Termname = [Microsoft.SharePoint.Client.Taxonomy]::NormalizeName($Context, $Termname) 

2 个答案:

答案 0 :(得分:1)

执行此操作

$ Termname = [Microsoft.SharePoint.Client.Taxonomy] :: NormalizeName($ Context,$ Termname)

将为空,但我必须像这样分别执行上下文:

$ context.ExecuteQuery()。

这解决了我的问题。

答案 1 :(得分:0)

NormalizeName method属于TaxonomyItem class,下面是在PowerShell中使用它的示例:

$result = [Microsoft.SharePoint.Client.Taxonomy.TaxonomyItem]::NormalizeName($Context, $Termname)
$Context.ExecuteQuery() #query needs to be executed in order to retrieve NormalizeName value 
Write-Host $result.Value

有时(从性能的角度来看)避免对服务器的额外请求可能更为方便,然后可以使用以下功能:

Function Normalize-Name([string]$Name) {
    if (!$Name) {
       return $null
    }  
    $trimSpacesRegex = new-object regex("\\s+",([System.Text.RegularExpressions.RegexOptions]::IgnoreCase -bor [System.Text.RegularExpressions.RegexOptions]::Compiled))
    return $trimSpacesRegex.Replace($Name, " ").Replace('&', [char]0xff06).Replace('"', [char]0xff02)
}

$encTermname = Normalize-Name -Name $Termname

应返回与TaxonomyItem.NormalizeName函数相同的结果