SHARE
TWEET

Untitled

a guest Apr 15th, 2012 168 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. SharePoint 2010, Powershell - Loop through all Document Libraries, create view and set it as default
  2. $site= New-Object Microsoft.SharePoint.SPSite $siteURL
  3. $web=$site.OpenWeb()
  4. $list=$web.Lists["$list"]
  5. $view=$list.Views["Detailed"]
  6. $view.DefaultView = $true
  7. $view.Update()
  8. $list.Update()
  9.        
  10. Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
  11. $siteURL = "http://sp14fdev01/"
  12. $site = Get-SPSite($siteURL)
  13.  
  14. foreach($web in $site.AllWebs) {
  15. foreach($list in $web.Lists) {
  16.         if($list.BaseType -eq "DocumentLibrary") {
  17.  
  18.  $site = New-Object Microsoft.SharePoint.SPSite($SiteURL) ;
  19.  $web = $site.OpenWeb($SiteURL);
  20.  $list = $web.Lists.item($listname);
  21.  
  22.  $viewfields = New-Object System.Collections.Specialized.StringCollection
  23.  $viewfields.Add("DocIcon")
  24.  $viewfields.Add("LinkFilename")
  25.  $viewfields.Add("_UIVersionString")
  26.  $viewfields.Add("FileSizeDisplay")
  27.  $viewfields.Add("Created")
  28.  $viewfields.Add("Modified")
  29.  $viewfields.Add("Editor")
  30.  [void]$list.Views.Add("Detailed", $viewfields, "", 100, $true, $true)
  31.  
  32.  $list.Update();
  33.  }}
  34.  $web.Dispose();
  35.  $site.Dispose();
  36.  }
  37.        
  38. Add-PSSnapin Microsoft.SharePoint.PowerShell -erroraction SilentlyContinue
  39.  
  40. $siteURL = "http://sp14fdev01/"
  41. $site = Get-SPSite($siteURL)
  42.  
  43. foreach($web in $site.AllWebs) {
  44.   foreach($list in $web.Lists) {
  45.     if($list.BaseType -eq "DocumentLibrary") {
  46. // the variables `$web` and `$list` already reference the objects you need
  47.       //$site = New-Object Microsoft.SharePoint.SPSite($SiteURL) ;
  48.       //$web = $site.OpenWeb($SiteURL);
  49.  
  50. // new instance of the list is necessary to avoid the error "Collection was modified"
  51.       $newList = $web.Lists.item($list.ID);
  52.  
  53.       $viewfields = New-Object System.Collections.Specialized.StringCollection
  54.       $viewfields.Add("DocIcon")
  55.       $viewfields.Add("LinkFilename")
  56.       $viewfields.Add("_UIVersionString")
  57.       $viewfields.Add("FileSizeDisplay")
  58.       $viewfields.Add("Created")
  59.       $viewfields.Add("Modified")
  60.       $viewfields.Add("Editor")
  61.       [void]$newList.Views.Add("Detailed", $viewfields, "", 100, $true, $true)
  62.  
  63.       $newList.Update();
  64.  
  65. // setting the default view
  66.       $view=$newList.Views["Detailed"]
  67.       $view.DefaultView = $true
  68.       $view.Update()
  69.     }
  70.   }
  71.  
  72.   $web.Dispose();
  73. }
  74.  
  75. $site.Dispose();
RAW Paste Data
Top