我正在使用PowerShell在SharePoint 2010中创建/更新标准视图。 我创建了一个包含View信息的XML文件,并使用powershell中的以下函数创建基于xml文件的视图。
该功能非常适合添加新视图,但我想要添加的是,如果视图已经存在,请使用xml文件中的信息进行更新。
所以基本上,创建视图,否则更新它们。
ForEach($xmlView in $xml.Root.Views.SelectNodes("View"))
{
$title = Get-XmlElement-Text $xmlView.SelectSingleNode("Title");
$spView = $lib.Views[$title];
if($spView -eq $null)
{
$Error.Clear(); #Need to clear Errors to handle non terminating errors
$defaultView = Get-XmlElement-Text $xmlView.SelectSingleNode("DefaultView");
if($defaultView -ne $null)
{
$defaultView = [boolean]::parse($defaultView);
}
else
{
$defaultView = $false
}
$rowLimit = Get-XmlElement-Text $xmlView.SelectSingleNode("RowLimit");
if($rowLimit -eq $null)
{
$rowLimit = 100;
}
$paged = Get-XmlElement-Text $xmlView.SelectSingleNode("Paged");
if($paged -ne $null)
{
$paged = [boolean]::parse($paged);
}
else
{
$paged = $true
}
$sortFilter = $xmlView.SelectSingleNode("SortFilter").InnerXML;
$viewFields = New-Object System.Collections.Specialized.StringCollection;
foreach($xmlField in $xmlView.ViewFields.SelectNodes("Field"))
{
$f = Get-XmlElement-Text $xmlField
if($f -ne $null -and $lib.Fields.ContainsField($f) -eq $true)
{
$viewFields.Add($f) > $null;
}
else
{
Write-Error "failed to find field: [$f] in library: [$lib]";
break;
}
}
if($Error.Count -eq 0)
{
$lib.Views.Add($title, $viewFields, $sortFilter, $rowLimit, $paged, $defaultView) > $null;
Write-Verbose "Added view: [$title] to libary $($lib.title)" -verbose;
}
else
{
Write-Warning "Failed to add view: [$title] to libary $($lib.title)";
}
}
else
{
Write-Host "Duplicate view found: [$spView]";
#If View already exists, update instead
}
}
答案 0 :(得分:1)
Dunno为什么这个被低估了,但如果你还没有得到它,这就是答案。
您已在$ spView字段中拥有该视图,因此请调用以下方法
$spView.ViewFields.Add("newfieldname1");
$spView.ViewFields.Add("newfieldname2");
//this can be a loop to add as many as you want
$spView.ViewFields.Delete("Removefieldname1");
//again can be a loop
//then finish by calling the update method
$spView.Update();