我正在运行以下命令,以获取Sharepoint安装的所有子站点中所有列表的所有视图。
$views = @()
foreach ($web in Get-PnPSubWebs) {
foreach ($list in Get-PnPList -Web $web.id) {
foreach ($view in Get-PnPView -list $list.id -web $web.id) {
$views += [pscustomobject]@{Id = $view.Id; StyleId = $view.StyleId}
}
}
}
哪个工作正常,我能按预期获得所有意见。但是我没有成功为视图设置新样式。
我尝试使用$view.ApplyStyle()
,如此处https://social.msdn.microsoft.com/forums/sharepoint/en-US/58068fb4-33ad-46cf-b866-bd86e1cbcafb/update-sharepoint-list-view-style-via-powershell和此处http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spview.applystyle.aspx所述。
但是,我得到以下错误:
Method invocation failed because [Microsoft.SharePoint.Client.View] does not contain a method named 'ApplyStyle'.
接下来,我按照以下说明尝试了Set-PnPView -Web $web.id -List $list.id -Identity $view.Id -Values @{StyleId=17}
:
https://docs.microsoft.com/en-us/powershell/module/sharepoint-pnp/set-pnpview?view=sharepoint-ps。那只给我这个信息:
WARNING: Setting property 'StyleId' to '17' failed with exception 'Property set method not found.'. Value will be ignored.
最后,$view.StyleId = 17
也不起作用。然后我得到了:'StyleId' is a ReadOnly property.
如预期的那样。
我们希望列表全部为阴影样式(id:17)。我们有数百个列表,并且我希望有一种更好的方法,然后花一整天手动更改所有列表。我还没有找到更改默认列表视图样式的方法,但是尽管没有办法查看或更改默认样式,我们所有的列表似乎都设置为“默认”视图。
感谢所有建议。
更新: 这是我最终用于主站点所有子站点中所有列表中所有视图的完整脚本:
Connect-PnPOnline –Url http://sharepointsite –CurrentCredentials
foreach ($web in Get-PnPSubWebs) {
foreach ($list in Get-PnPList -Web $web.id) {
foreach ($view in Get-PnPView -list $list.id -web $web.id) {
[xml]$Doc = New-Object System.Xml.XmlDocument
$Doc.LoadXml($view.ListViewXml);
$element = $Doc.SelectSingleNode("//View//ViewStyle");
if ($element -eq $null)
{
$element = $Doc.CreateElement("ViewStyle");
$element.SetAttribute("ID", 17);
$Doc.DocumentElement.AppendChild($element);
}
else
{
$element.SetAttribute("ID", 17);
}
Set-PnPView -Web $web.id -List $list.id -Identity $view.Id -Values @{ListViewXml=$Doc.FirstChild.InnerXml}
}
}
}
然后也要获取主要网络的所有列表,我运行了此
Connect-PnPOnline –Url http://sharepointsite –CurrentCredentials
$web = Get-PnPWeb
foreach ($list in Get-PnPList -Web $web.id) {
foreach ($view in Get-PnPView -list $list.id -web $web.id) {
[xml]$Doc = New-Object System.Xml.XmlDocument
$Doc.LoadXml($view.ListViewXml);
$element = $Doc.SelectSingleNode("//View//ViewStyle");
if ($element -eq $null)
{
$element = $Doc.CreateElement("ViewStyle");
$element.SetAttribute("ID", 17);
$Doc.DocumentElement.AppendChild($element);
}
else
{
$element.SetAttribute("ID", 17);
}
Set-PnPView -Web $web.id -List $list.id -Identity $view.Id -Values @{ListViewXml=$Doc.FirstChild.InnerXml}
}
}
答案 0 :(得分:0)
这是我的示例测试脚本供您参考(我已经在线测试过,我认为它应该适用于SharePoint 2019,因为它是CSOM api)。
Add-Type -Path (Resolve-Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll")
Add-Type -Path (Resolve-Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll")
$UserName = "Wendy@tenant.onmicrosoft.com"
$siteURL = "https://tenant.sharepoint.com/sites/lee"
$Password = "password"
[SecureString]$SecurePass = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $SecurePass)
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteURL)
$ctx.credentials = $Credentials
try{
$list = $ctx.web.Lists.GetByTitle("MyList2")
$views = $list.views
$ctx.load($views)
$ctx.executeQuery()
foreach ($v in $views)
{
if ($v.Title -eq "All Items")
{
$ctx.Load($v);
$ctx.ExecuteQuery();
[xml]$Doc = New-Object System.Xml.XmlDocument
$Doc.LoadXml($v.ListViewXml);
$element = $Doc.SelectSingleNode("//View//ViewStyle");
if ($element -eq $null)
{
$element = $Doc.CreateElement("ViewStyle");
$element.SetAttribute("ID", 17);
$Doc.DocumentElement.AppendChild($element);
}
else
{
$element.SetAttribute("ID", 17);
}
$v.ListViewXml = $Doc.FirstChild.InnerXml;
$v.Update();
$ctx.ExecuteQuery();
}
}
}
catch{
write-host "$($_.Exception.Message)" -foregroundcolor red
}