Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用powershell更新具有多个值的SharePoint企业关键字?_Sharepoint_Powershell - Fatal编程技术网

如何使用powershell更新具有多个值的SharePoint企业关键字?

如何使用powershell更新具有多个值的SharePoint企业关键字?,sharepoint,powershell,Sharepoint,Powershell,我正在尝试编写一个powershell脚本,该脚本查看用户定义的变量,并更新列表中与该值匹配的每个项目的企业关键字 例如,假设您在SP中有一个包含托管元数据关键字的页面:new、fresh、clean 我想要一个脚本,询问用户他们想要交换什么关键字。因此,用户将一个变量指定为:fresh,另一个变量指定为:fresh,并使用关键字fresh更新任何项目 以下是我以前使用过但现在不起作用的内容,因为存在多个值: Add-PSSnapin Microsoft.SharePoint.PowerShel

我正在尝试编写一个powershell脚本,该脚本查看用户定义的变量,并更新列表中与该值匹配的每个项目的企业关键字

例如,假设您在SP中有一个包含托管元数据关键字的页面:new、fresh、clean 我想要一个脚本,询问用户他们想要交换什么关键字。因此,用户将一个变量指定为:fresh,另一个变量指定为:fresh,并使用关键字fresh更新任何项目

以下是我以前使用过但现在不起作用的内容,因为存在多个值:

Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
$webURL = <MY SP URL>
$listName = <MY LIST NAME>
$web = Get-SPWeb $webURL
$list = $web.Lists[$listName]
$listitems = $list.items 
$session = Get-SPTaxonomySession -Site $web.Site
$termStore = $session.TermStores["Managed Metadata Service"]
$group = $termStore.Groups["Resources"]
$termset = $group.TermSets["Wiki Categories"]
$terms = $termSet.GetTerms(100)
$wiki1 = read-host "Enter the wiki category you want to update"
$wiki2 = read-host "Enter the replacement wiki category"

$term = $terms | ?{$_.name -eq $wiki2}


Foreach($item in $listitems) 
{$wiki = $item["Wiki Categories"]
    if($wiki.label -eq $term) 
    { 
        $spitem = [Microsoft.SharePoint.SPListItem]$item;
    $taxfield = [Microsoft.SharePoint.Taxonomy.TaxonomyField]$spitem.Fields["Wiki Categories"]
        $taxfield.SetFieldValue($spitem, $term)

    $spitem.Update() 
    $spitem.File.Publish("True")
    } 
}
这一行:

$term = $terms | ?{$_.name -eq $wiki2}
$taxfield.SetFieldValue($spitem, $term)

问题是你通过了一个($学期)到了你应该通过的地方

您可以这样转换它们:

$taxfield = $spitem.Fields["Wiki Categories"]

$tfvc = new-object -typename Microsoft.SharePoint.Taxonomy.TaxonomyFieldValueCollection -argumentlist $taxfield;

foreach($t in $ term)
{
   $tfv = new-object -typename Microsoft.SharePoint.Taxonomy.TaxonomyFieldValue -argumentlist $taxfield
   $tfv.TermGuid = $t.Id
   $tfv.Label = $t.Name
   $tfvc.Add($tfv)
}
...
$taxfield.SetFieldValue($spitem, $tfvc)

这是否发生在其自身的foreach中,在$item for each之前还是在中?在“…”之前的部分将$term中的多个值转换为不同的集合类型($tfvc),因此可以在foreach之前对所有项执行一次。然后将第二个参数替换为
$taxfield.SetFieldValue
。www.qipoint.com上有一个工具可以实现这一点