Creating a View in each Document Library in a Web Application Creating a View in each Document Library in a Web Application
Wishv Prajapati

Wishv Prajapati

December 19, 2012

All Post
Creating
Share:

Crate a View in each Library in a Web App

My friend Rob Holmes raised an interesting challenge; deploying a new view to all libraries. Here’s the script to apply it to all libraries with Content Types enabled that are Document Libraries, across all libraries in all webs in all site collections of a web application.

Sorting/filtering entries in a view requires the use of CAML, for which I provide a few examples commented in the script. To write CAML, one needs to know the available fields. If you have a $Lib, you can dump the static or internal names for every field in the library with this command:

1
$Lib.Fields | select internalname

Here’s some CAML examples.

'<OrderBy><FieldRef Name="mvReceived_x0020_Time" Ascending="FALSE" /></OrderBy><Where><IsNotNull><FieldRef Name="mvReceived_x0020_Time" /></IsNotNull></Where>'
'<OrderBy><FieldRef Name="mvSentOn" Ascending="FALSE" /></OrderBy><Where><IsNotNull><FieldRef Name="mvReceived_x0020_Time" /></IsNotNull></Where>'
'<OrderBy><FieldRef Name="mvSentOn" Ascending="FALSE" /></OrderBy><Where><IsNotNull><FieldRef Name="mvSentOn" /></IsNotNull></Where>'

There’s a few good CAML builder tools. You can also just navigate an existing View and grab the XML from the View in SharePoint.

Here’s the script:

  Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
$wa=get-spwebapplication http ://SharePoint
 
Write-Host "script starting $(get-date)"
 
$wa=get-spwebapplication http ://SharePoint
 
foreach ($Site in $wa.sites)
{
    if ($site.url -like $MatchStr)
    {
        $webs=$Site.AllWebs
        $webcount = $Site.AllWebs.Count
 
        for ($i=0; $i -lt $webcount; $i++)
        {
            $web=$webs[$i]
 
            Write-Host "working in $($web.url)"
 
            $lists=$web.lists;
 
              for ($k=0; $k -lt $lists.count; $k++)
              {
                    $JPLib = $lists[$k];
                         #don't bother adding a view to a hidden library
                    if ($JPLib.Hidden)
                    {
                        continue;
                    }
                         #only for libraries, not for GenericList and other types
                    if ($JPLib.BaseType -ne "DocumentLibrary")
                    {
                        continue;
                    }
                         #choose your own lib filter; this acts on every content type enabled library
                    if ($JPLib.ContentTypesEnabled)
                    {
                    write-host -f green "The Library $($JPLib.title) exists in the site $($web.url), about to tune the view"
                    try
                    {
                    $x=$JPLib.Views.get_Item("Email")
                    if ($x.id -ne $null) #prevents duplicate entries
                    {
                        $JPLib.Views.Delete($x.ID.ToString())
                    }
                    }
                    catch
                    {}
                    if ($JPLib.Views["Email"] -eq $null) #prevents duplicate entries
                       {
 #$viewQuery = ''
                     #$viewQuery = ''
                     $viewQuery = ''
 
                     $viewFields = New-Object System.Collections.Specialized.StringCollection
                     $viewFields.Add("DocIcon") &gt; $null
                     $viewFields.Add("LinkFilename") &gt;  $null
                     $viewFields.Add("Title") &gt;  $null
                     $viewFields.Add("mvTo") &gt;  $null
                     $viewFields.Add("mvFrom") &gt;  $null
                     $viewFields.Add("mvSubject") &gt;  $null
                     $viewFields.Add("mvSentOn") &gt;  $null
                     $viewFields.Add("mvAttach_x0020_Count") &gt;  $null
                #    $viewFields.Add("DocType") &gt;  $null # this is accounting specific
 
                     #RowLimit property
                     $viewRowLimit = 50
                     #Paged property
                     $viewPaged = $true
                     #DefaultView property
                     $viewDefaultView = $false
                     #$ViewQuery=$null;
                     $viewTitle="Email"
                     $newview = $JPLib.Views.Add($viewTitle, $viewFields, $viewQuery, $viewRowLimit, $viewPaged, $viewDefaultView)
                       }
    }
    }
   }
#} #foreach site
} #if $true/Siteurl is not null, if environment setup is valid
}<del datetime="2024-03-06T19:12:48+00:00">  </del>

One thought on “Creating a View in each Document Library in a Web Application

Leave a Reply

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

Want to talk?

Drop us a line. We are here to answer your questions 24*7.