SharePoint 2010, Powershell - Combine all document libraries, create a view and set it as default

I have a small problem with a script I am working on for SharePoint 2010. I am only new to Powershell so I could not spot a dubious obvious and egregious problem.

The desire of the script is to loop through every website in every site collection, create a view for document libraries only, and set that view to default.

Issue 1) Currently the document libraries appear to be listed, but then the view is generated multiple times in the first library found. There is something wrong with the foreach loops, but I don't know what.

Problem 2) I need to integrate this section so that I can set the default view, but I'm not too sure where to hold it so it goes through with changes in each library.

$site= New-Object Microsoft.SharePoint.SPSite $siteURL
$web=$site.OpenWeb()
$list=$web.Lists["$list"]
$view=$list.Views["Detailed"]
$view.DefaultView = $true
$view.Update()
$list.Update()

      

Any help on these two would be very helpful :-). Thanks Ashley

Full Script:

Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
$siteURL = "http://sp14fdev01/"
$site = Get-SPSite($siteURL)

foreach($web in $site.AllWebs) {
foreach($list in $web.Lists) {
        if($list.BaseType -eq "DocumentLibrary") {

 $site = New-Object Microsoft.SharePoint.SPSite($SiteURL) ;
 $web = $site.OpenWeb($SiteURL);
 $list = $web.Lists.item($listname);

 $viewfields = New-Object System.Collections.Specialized.StringCollection 
 $viewfields.Add("DocIcon") 
 $viewfields.Add("LinkFilename") 
 $viewfields.Add("_UIVersionString") 
 $viewfields.Add("FileSizeDisplay") 
 $viewfields.Add("Created") 
 $viewfields.Add("Modified") 
 $viewfields.Add("Editor") 
 [void]$list.Views.Add("Detailed", $viewfields, "", 100, $true, $true)

 $list.Update();
 }}
 $web.Dispose();
 $site.Dispose();
 }

      

+3


source to share


1 answer


The changes are explained in the comments.



Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue

$siteURL = "http://sp14fdev01/"
$site = Get-SPSite($siteURL)

foreach($web in $site.AllWebs) {
  foreach($list in $web.Lists) {
    if($list.BaseType -eq "DocumentLibrary") {
// the variables `$web` and `$list` already reference the objects you need
      //$site = New-Object Microsoft.SharePoint.SPSite($SiteURL) ;
      //$web = $site.OpenWeb($SiteURL);

// new instance of the list is necessary to avoid the error "Collection was modified"
      $newList = $web.Lists.item($list.ID);

      $viewfields = New-Object System.Collections.Specialized.StringCollection 
      $viewfields.Add("DocIcon") 
      $viewfields.Add("LinkFilename") 
      $viewfields.Add("_UIVersionString") 
      $viewfields.Add("FileSizeDisplay") 
      $viewfields.Add("Created") 
      $viewfields.Add("Modified") 
      $viewfields.Add("Editor") 
      [void]$newList.Views.Add("Detailed", $viewfields, "", 100, $true, $true)

      $newList.Update();

// setting the default view
      $view=$newList.Views["Detailed"]
      $view.DefaultView = $true
      $view.Update()
    }
  }

  $web.Dispose();
}

$site.Dispose();

      

+3


source







All Articles