情形: 我们有两个字段已添加到共享文档库的默认视图“所有文档”。 home.aspx页面上的共享Documents XsltListViewWebPart视图不反映更改,因为这存在OOTB。我需要将两个字段添加到XsltListViewWebPart视图中,因为已存在多个站点并且手动更改是不可能的。
我找到了一个资源,它删除了XsltListViewWebPart的所有ViewFields,然后从库的视图中添加了字段。 http://spblog.net/post/2011/04/26/Changing-default-view-for-ListViewWebPart-programmatically.aspx
我不得不修改它以在我的环境中工作一点,但这里是源
# Add SharePoint cmdlets reference
Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
$web = Get-SPWeb -Identity "http://myServer/sites/Test_Site04_18_03-03-49"
$file = $web.GetFile("SitePages/Home.aspx");
$wpMngr = $file.GetLimitedWebPartManager([System.Web.UI.WebControls.Webparts.PersonalizationScope]::Shared);
$sharedDocList = $web.Lists["Shared Documents"]
$sharedDocViewFields = $list.Views["All Documents"].ViewFields
$foundWebPart = $false
:webPartLoop foreach($webPart in $wpMngr.WebParts)
{
:viewLoop foreach($view in $sharedDocList.Views)
{
if($webPart.ViewGuid -eq $view.ID.ToString("B"))
{
$foundWebPart = $true
$view.ViewFields.DeleteAll()
#loop through all the fields in the shared doc view and add them
foreach($field in $sharedDocViewFields)
{
write-host "field: " $field
$view.ViewFields.Add($field)
}
$view.Update();
break viewLoop
}
}
#break out of the loop because we already found the webpart and modified the view
if ($foundWebPart)
{
break webPartLoop
}
}
我运行它并看到错误:
Exception calling "Add" with "1" argument(s): "Column 'ColumnName' does not exist. It may have been deleted by another user."
+ $view.ViewFields.Add <<<< ($field)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
我在做错了什么?我认为Doc Lib上的原始字段可能存在问题。非常奇怪,我可以把它写到屏幕上,但它告诉它当我尝试将它添加到新视图时不存在。
我甚至试图从仅将视图更改为库所使用的相同视图来处理此问题,但没有成功。我正在阅读的一篇文章提到了一些关于隐藏观点的内容......