Adding a SharePoint View to every library

How to add a SharePoint View to every library

Sometimes one has to create a custom view and deploy it to every library. This example walks through a set of explicitly named site collections within a given Managed Path, and adds a custom view. To enable running it multiple times, the script first deletes every instance of that named view, if any exist. Otherwise we would have a new duplicate view in each library each time we ran this script. Note both filtering and sort order is specified by a CAML query. This example shows a very simple one (sort by filename) but it’s possible to construct complex queries. The two approaches I like to use is use a tool called CAMLBuilder, or create a test view, and then navigate the object model and extract the CAML query. Lastly I add the fields using the static (internal) name in the preferred sequence.

#adds "Field" to right  in Default View; deletes field first, in case it already exists, so this can be rerun safely
Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
# Script changes the default View. 
$envrun="Prod"			# selects environment to run in
if ($envrun -eq "Dev")
{
}
elseif ($envrun -eq "Prod")
{
$siteUrl = "http ://sharepoint/ManagedPath/"
$LoopString = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,09"  #these are the explicitly named Site Collections
$LoopStringArr = $LoopString.Split(“,”)
}
else
{
Write-Host "ENVIRONMENT SETTING NOT VALID: script terminating..."
$siteUrl =  $null;
return;
}
Write-Host "script starting" 
$myheader = "STARTING: $(get-date)"
foreach ($letter in $LoopStringArr)
{
$SiteName=$siteurl+$letter
$rootSite = New-Object Microsoft.SharePoint.SPSite($SiteName)
$site=$rootSite  #skipping traversing all sites for now
write-host $site.Url
if ($true) #this is useful to uncomment as a filter
#  if ($site.Url -like "$siteurl/personal/plau*")  
{
Write-Host -foregroundcolor darkblue "$($site.id) - $($site.Url) - $($site.contentdatabase.id) - $($site.contentdatabase.name)"   
$rootWeb = $site.RootWeb
$web=$rootWeb;
$JPLists=$web.Lists;
$JPListsCount=$JPLists.Count  #it much more efficient to resolve Lists object and count outside the loop
for ($i=0;$i -lt $JPListsCount;$i++)
{ 
$JPLib=$JPLists[$i];
$A_Lib_Count++;
$SkipLib=$true; #true
if ( ($JPlib.BaseType -ne "DocumentLibrary") -or ($JPlib.hidden) )
{
# forget the rest and return to top
Write-Host -foregroundcolor green "fast test skipping Library: $($JPlib)";   
}
elseif ($JPLib.Title -Match "SitesAssets|Photo|Image|CustomizedsReports|Templates|Pages|Picture|cache|style|Slide")
{
# forget the rest and return to top
Write-Host -foregroundcolor red "fast test skipping Library because it mentions $Matches: $($JPlib)";   
}
elseif ($JPLib.BaseTemplate -ne "DocumentLibrary")   #alternatively, only skip if -eq XMLForm
{
# forget the rest and return to top
Write-Host -foregroundcolor red "fast skipping Library because it is not of base DocumentLibrary, it is BaseType:$($JPlib.basetemplate): $($JPlib.title)";   
}
elseif (($JPLib.ThumbnailsEnabled) -or ($JPLib.DefaultView -eq "AllSlides"))
{
# forget any library with thumbnails, these are not normal doclibs, and return to top
Write-Host -foregroundcolor red "fast test skipping Library because it has Thumbnails/Slides $($JPlib)";   
}
elseif (!$JPLib.ContentTypesEnabled)
{
Write-Host -foregroundcolor red "skipping because it does not have CTs enabled: $($JPlib)";   
}
else
{  $SkipLib=$false;	}
if (!$SkipLib)
{
write-Host -foregroundcolor green "Processing Library: $($JPlib)";   
#hah, don't even delete and add if found, just cycle
$x=$null;
try
{
$x=$JPLib.Views.get_Item("NEW View")
}
catch {$x=$null}
if ($x -ne $null)
{
continue;
}
try
{
$x=$JPLib.Views.get_Item("NEW View")
if ($x.id -ne $null) #prevents duplicate entries
{
$JPLib.Views.Delete($x.ID.ToString())
}
}
catch
{}
if ($JPLib.Views["NEW View"] -eq $null) #prevents duplicate entries
{
$viewQuery = '<OrderBy><FieldRef Name="FileLeafRef" /></OrderBy>'  #This is any CAML query you construct
#let's add the fields, by internal name, in the preferred sequence
$viewFields = New-Object System.Collections.Specialized.StringCollection
$viewFields.Add("DocIcon") > $null
$viewFields.Add("LinkFilename") > $null
$viewFields.Add("Title") > $null
$viewFields.Add("Modified") > $null
$viewFields.Add("Editor") > $null
$viewFields.Add("ProductCode") > $null
$viewFields.Add("ProductDescription") > $null
$viewFields.Add("DocumentType") > $null
$viewFields.Add("ReportSubType") > $null
#RowLimit property
$viewRowLimit = 30
#Paged property
$viewPaged = $true
#DefaultView property
$viewDefaultView = $false
#$ViewQuery=$null;
$viewTitle="NEW View"
$newview = $JPLib.Views.Add($viewTitle, $viewFields, $viewQuery, $viewRowLimit, $viewPaged, $viewDefaultView)
} #endif view doesn't exist
}
}
try
{
$rootweb.Dispose()
$web.Dispose()
}
catch{}
} #if $true/Siteurl is not null, if environment setup is valid
try
{
$rootSite.Dispose()
$site.Dispose()
}
catch{}
} #foreach letter
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *