Changing the page for a SharePoint Library View Changing the page for a SharePoint Library View
Joel Plaut

Joel Plaut

September 02, 2015

All Post
Changing-the
Share:

Views are easily created by end users, and can be created through automation. I recently had to change the underlying aspx page for an existing view. First I tried using the SPView method SetViewXml(), however that does not work, as many other programmers have discovered to their chagrin.

The approach that works is to clone the view, then rename the title. In this case, I am trying to create AllItems.aspx, so I clone the view to that name then rename it:

$NewView = $SourceView.clone("AllItems",100,$true,$true)
$NewView.title = "All Documents";
$NewView.update();
$Views.delete($SourceView.id);

The script below goes one better. It finds the extra “All Documents” views, and deletes all the ones that are duplicate, preserving the original (latest) View. It iterates through the collection downward deleting as it goes.

Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
 
$mylogfile="ongoinglogfile.txt"
 
$envGUID = $(Get-SPFarm).id
if      ($envGUID -eq  "c25ca1f1-6d8f-4c59-86e3-d64821a3bcbb" ) {$env = "Dev"}
elseif  ($envGUID -eq "8e51b3e2-1ed7-4549-ad51-c5c43b065d2c" )  {$env = "Prod"}
else
{
    $env = "Unknown"
    Write-Host -ForegroundColor darkred "UKNOWN FARM in $($MyInvocation.MyCommand.Definition)"
    Add-Content $mylogfile "UKNOWN FARM in $($MyInvocation.MyCommand.Definition)"
    exit
}
elseif ($env -eq "Dev")
{
    #$MatchStr="ht tp://SharePoint dev/Sites/*"  
    $MatchStr="ht tp://SharePoint dev/Sites/2015" 
    $sitesArrNames = "ht tp://SharePoint dev/Sites/2015"
    $sitesArr = $sitesArrNames.split(",");
 
}
elseif ($env -eq "Prod")
{
    $MatchStr="ht tp://SharePoint/Sites/2015" 
    $sitesArrNames = "ht tp://SharePoint/Sites/2015"
    $sitesArr = $sitesArrNames.split(",");
}
else
{
    $waName = $MatchStr = $waName = $null;
}
 
$libsArrStr="Lib1,lib2,Documents,Shared Documents"
$LibsArr=$libsArrStr.split(",")
 
 
write-host  "STARTING: $(get-date) Script: $($MyInvocation.MyCommand.Definition)"
Add-Content $mylogfile "STARTING: $(get-date) Script: $($MyInvocation.MyCommand.Definition)"
 
 
foreach ($SiteName in $sitesArr)
{
    $site = get-spsite $SiteName
    if ($site.url -like $MatchStr)
    {
    $webs=$Site.AllWebs
    $webcount = $Site.AllWebs.Count
 
    for ($i=0; $i -lt $webcount; $i++)
    {
        $TargetWeb=$web=$webs[$i]  #$TargetWeb is used by my standard routines
             
        Write-Host "==>working in $($web.url)"
        Add-Content $mylogfile "==>working in $($web.url)"
 
        $lists=$web.lists;
 
      write-host -f green "The Library $($listName) exists in the site $($web.url), about to tune the view"  #not going to try to config it either
      Add-Content $mylogfile "The Library $($listName) exists in the site $($web.url), about to tune the view" 
 
      for ($k=0; $k -lt $LibsArr.count; $k++)
      {
        $libstr = $LibsArr[$k];
 
        $JPLib = $web.Lists.TryGetList($libStr)  #Extra JPFolder is for the override on routines with lib
        if($JPLib -ne $null)  #optional, can filter to ensure ($JPLib.ContentTypesEnabled)
                            {
                write-host "Analyzing $($JPLib.title) in $($web.url)"
                 Add-Content $mylogfile "Analyzing $($JPLib.title) in $($web.url)"
 
                 $Views = $JPLib.views;
 
                 $foundOne=$false;
                 for ($vi=$views.count-1; $vi -ge 0; $vi--)
                 {
                   $View = $views[$vi];
                   if ($View.title -eq "All Documents")
                   {
                   if ($foundOne -eq $true)  #there was one found earlier
                   {
                     $views.Delete($view.id);
                     write-host "Deleted a view in $($JPLib.title) in $($web.url)"
                     Add-Content $mylogfile "Deleted a view in $($JPLib.title) in $($web.url)"
                    }
                    else
                      {
                      $foundOne=$true;
                      $SourceView = $view;
                      }
                      }
                      }
 
 
                      if ($FoundOne)
                      {
                      if ($SourceView.schemaxml.contains("Documents.aspx"))
                      {
                        $NewView = $SourceView.clone("AllItems",100,$true,$true)
                        $NewView.title = "All Documents";
                        $NewView.update();
                        $Views.delete($SourceView.id);
                        write-host "Cloned and deleted a view in $($JPLib.title) in $($web.url)"
                        Add-Content $mylogfile "Cloned and deleted a view in $($JPLib.title) in $($web.url)"
                        }
 
                        }
      }
      }
 
            $web.Dispose()
            } #SPWeb processing
         
        } #if $true/Siteurl is not null, if environment setup is valid
        $site.Dispose()
} #foreach site

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.