Thursday, August 4, 2011

PowerShell scripts to configure lists in batch

Below are the PowerShell scripts I often use to configure lists in batch.

Hope you would like them :-)

I plan to keep updating the scripts.  If you think some functionality is very useful but not listed here, please let me know.

function AddColumnToDocLibDefaultView([string]$siteURL)
{
 $site = Get-SPSite($siteURL)
 Write-Host "siteURL is " $siteURL
 foreach($web in $site.AllWebs)
 {
  Write-Host "Inspecting " $web.Title
  $i = 0
  $listname = @()
  foreach ($list in $web.Lists)
  {
   if($list.BaseType -eq "DocumentLibrary" -and $list.OnQuickLaunch)
   {
      Write-Host "list title: " $list.Title ", BaseType: " $list.BaseType
      $listname += @($i)
      $listname[$i] = $list.Title
      $i = $i + 1
   }
  }

  #  $viewName = "All Documents"
  foreach ($listtitle in $listname)
  {
    $mylist = $web.Lists[$listtitle]
    $spView = $mylist.DefaultView

    $spField = $mylist.Fields["Version"]
    if($spView.ViewFields.Exists($spField.InternalName) -eq $false)
    {
   $spView.ViewFields.Add($spField)
    }

    $spField = $mylist.Fields["Checked Out To"]
    if($spView.ViewFields.Exists($spField.InternalName) -eq $false)
    {
   $spView.ViewFields.Add($spField)
    }

    $spField = $mylist.Fields["Title"]
    if($spView.ViewFields.Exists($spField.InternalName) -eq $false)
    {
   $spView.ViewFields.Add($spField)
    }

    $spView.Update()
  }
 }

 $site.Dispose()
}

AddColumnToDocLibDefaultView("http://sharepointServer/sites/site1")

-------------------------------------------
function AddRemoveContentTypesToLib([string]$siteURL)
{
 $site = Get-SPSite($siteURL)
 Write-Host "siteURL is " $siteURL
 foreach($web in $site.AllWebs)
 {
  Write-Host "Inspecting " $web.Title
  $i = 0
  $listname = @()
  foreach ($list in $web.Lists)
  {
   if($list.BaseType -eq "DocumentLibrary" -and $list.OnQuickLaunch)
   {
      Write-Host "list title: " $list.Title ", BaseType: " $list.BaseType
      $listname += @($i)
      $listname[$i] = $list.Title
      $i = $i + 1
   }
  }

  foreach ($listtitle in $listname)
  {
   $mylist = $web.Lists[$listtitle]
   $mylist.ContentTypesEnabled = $true
   $mylist.Update()

   #$ctToRemove = $mylist.ContentTypes["Document"]
   #$ctToRemove.Hidden = $true
   #$ctToRemove.Update()
   #$mylist.ContentTypes.Delete($ctToRemove.Id)

   #$ctToRemove = $mylist.ContentTypes["Excel"]
   #$ctToRemove.ReadOnly = $false
   #$mylist.ContentTypes.Delete($ctToRemove.Id)

   #$ctToRemove = $mylist.ContentTypes["PowerPoint"]
   #$ctToRemove.ReadOnly = $false
   #$mylist.ContentTypes.Delete($ctToRemove.Id)

   #$ctToAdd = $site.RootWeb.ContentTypes["MyDocument"]
   #$ctToAdd.ReadOnly = $false
   #$ct = $mylist.ContentTypes.Add($ctToAdd)

   $ctToAdd = $site.RootWeb.ContentTypes["Word"]
   $ctToAdd.ReadOnly = $false
   $ct = $mylist.ContentTypes.Add($ctToAdd)

   $ctToAdd = $site.RootWeb.ContentTypes["Excel"]
   $ctToAdd.ReadOnly = $false
   $ct = $mylist.ContentTypes.Add($ctToAdd)

   $ctToAdd = $site.RootWeb.ContentTypes["PowerPoint"]
   $ctToAdd.ReadOnly = $false
   $ct = $mylist.ContentTypes.Add($ctToAdd)

   $mylist.Update()
  }
 }
 $site.Dispose()
}

AddRemoveContentTypesToLib("http://sharepointServer/sites/site1")

-------------------------------------------
function ChangePageSizeOfDocLibDefaultView([string]$siteURL)
{
 $site = Get-SPSite($siteURL)
 Write-Host "siteURL is " $siteURL
 foreach($web in $site.AllWebs)
 {
  Write-Host "Inspecting " $web.Title
  $i = 0
  $listname = @()
  foreach ($list in $web.Lists)
  {
   if($list.BaseType -eq "DocumentLibrary" -and $list.OnQuickLaunch)
   {
      Write-Host "list title: " $list.Title ", BaseType: " $list.BaseType
      $listname += @($i)
      $listname[$i] = $list.Title
      $i = $i + 1
   }
  }

  #  $viewName = "All Documents"
  foreach ($listtitle in $listname)
  {
    $mylist = $web.Lists[$listtitle]
    $spView = $mylist.DefaultView
    $spView.RowLimit = 300

    $spView.Update()
  }
 }
 $site.Dispose()
}

ChangePageSizeOfDocLibDefaultView("http://sharepointServer/sites/site1")

-------------------------------------------
function EnableVersioning([string]$siteURL)
{
 $site = Get-SPSite($siteURL)
 Write-Host "siteURL is " $siteURL
 foreach($web in $site.AllWebs)
 {
  Write-Host "Inspecting " $web.Title
  foreach ($list in $web.Lists)
  {
     if($list.BaseType -eq "DocumentLibrary")
     {
     Write-Host "Versioning enabled: " $list.EnableVersioning
     Write-Host "MinorVersioning Enabled: "  $list.EnableMinorVersions
     Write-Host "EnableModeration: " $list.EnableModeration
     Write-Host "Major Versions: " $list.MajorVersionLimit
     Write-Host "Minor Versions: " $list.MajorWithMinorVersionsLimit
     $list.EnableVersioning = $true
     $list.EnableMinorVersions = $false
     $list.MajorVersionLimit = 6
   #  $list.MajorWithMinorVersionsLimit = 5
     $list.Update()
     Write-Host $list.Title " is updated with MajorVersionLimit 6"
     }
  }
 }
 $site.Dispose()
}

EnableVersioning("http://sharepointServer/sites/site1")

-------------------------------------------
function PredefineSendToLib([string]$siteURL)
{
 $site = Get-SPSite($siteURL)
 Write-Host "siteURL is " $siteURL
 foreach($web in $site.AllWebs)
 {
  Write-Host "Inspecting " $web.Title
  $i = 0
  $listname = @()
  foreach ($list in $web.Lists)
  {
   if($list.BaseType -eq "DocumentLibrary" -and $list.OnQuickLaunch)
   {
      Write-Host "list title: " $list.Title ", BaseType: " $list.BaseType
      $listname += @($i)
      $listname[$i] = $list.Title
      $i = $i + 1
   }
  }

  foreach ($listtitle in $listname)
  {
    $mylist = $web.Lists[$listtitle]
    $mylist.SendToLocationName = "Shared"
    $mylist.SendToLocationUrl = "http://sharepoint.company.local/shared"
    $mylist.Update()
  }
 }
 $site.Dispose()
}

PredefineSendToLib("http://sharepointServer/sites/site1")

-------------------------------------------
function RemoveColumnFromDocLib([string]$siteURL)
{
 $site = Get-SPSite($siteURL)
 Write-Host "siteURL is " $siteURL
 foreach($web in $site.AllWebs)
 {
  Write-Host "Inspecting " $web.Title
  $i = 0
  $listname = @()
  foreach ($list in $web.Lists)
  {
   if($list.BaseType -eq "DocumentLibrary" -and $list.OnQuickLaunch)
   {
      Write-Host "list title: " $list.Title ", BaseType: " $list.BaseType
      $listname += @($i)
      $listname[$i] = $list.Title
      $i = $i + 1
   }
  }

  #  $viewName = "All Documents"
  foreach ($listtitle in $listname)
  {
    $mylist = $web.Lists[$listtitle]

    if($mylist.Fields.ContainsFieldWithStaticName("SNo") -eq $true)
    {
   $spField = $mylist.Fields["SNo"]
   $spField.Delete()
    }

    $mylist.Update()
  }
 }

 $site.Dispose()
}

RemoveColumnFromDocLib("http://sharepointServer/sites/site1")

-------------------------------------------


No comments:

Post a Comment