场景:我们有两个字段被添加到共享文档库默认视图“所有文档”中。home.aspx 页面上的共享文档 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 上的原始字段可能存在问题。很奇怪我可以把它写到屏幕上,但是当我尝试将它添加到新视图时它告诉它不存在。我什至尝试从将视图更改为图书馆使用的相同视图的角度来解决这个问题,但没有成功。我正在阅读的一篇文章提到了一些关于隐藏视图的内容......