Monday, March 18, 2013

Enable Versioning SharePoint 2010 Document Library reading CSV file using Powershell


My requirement is update all document libraries version history “No Version” to “Major version”. when i loop through all document libraries from all sites it is updating so many out of box document libraries. I dont want to touch any out of box or other libraries.  so i come up with one csv file which is having my document library names and site relative url as shown below.


here DocLibName, URL are headings. for home site you dont need to specify any value for URl like below.



copy to notepad file and Save as DocLibraries.csv file. dont change below heading names.



DocLibName
 URL
Shared Documents

Resource Library
subsite1
Meeting Minutes
sbusite2
Archives
subsite3
Final Documents
susbsite3


$URL = "http://spsev5:8003/"
#add-pssnapin Microsoft.SharePoint.Powershell

$site = New-Object Microsoft.SharePoint.SPSite($URL)
#$web = $site.openweb("")
#.csv file path
$InputFile = "D:\Development\Surya\DocLibraries.csv"
$file = ipcsv $InputFile

#logging
$timestamp = get-date -format "yyyyMMdd_hhmmtt"
$filenameStart = "DocLibraryVersioningLog"
$logfile = ("{0}{1}.csv" -f $filenamestart, $timestamp)
$header = "Head1,Head2"
$header | out-file -FilePath $logfile

#variables
      $csvDocLibName= $null
      $csvURL = $null
      $webUrl = $null
      $i=1
     
Write-Host "Started"
foreach ($line in $file)
{
#get excel sheet cell values 
   $csvDocLibName = $line.DocLibName
   $csvURL=   $line.URL
   $webUrl=  "$URL$csvURL" #concatenating site url + subsite url from csv file
     
      #Write-Host $webUrl
      #Write-Host "DocLibName:"  $DocLibName "URL:" $webURL
     
try  {

            Start-SPAssignment -Global
         $web =Get-SPWeb  $webUrl
            $msg = ("{0},{1}" -f "$i", "$webUrl")
            $msg | out-file -FilePath $logfile  -append
           
            $library = $web.Lists[$csvDocLibName]
           
            if($library  -ne $null)
            {
                  Write-Host "library name : " $library.Title
               $library.EnableVersioning = $true
            # $library.EnableMinorVersions = $true
             #$library.MajorVersionLimit = $majorVersionLimit
             #$library.MajorWithMinorVersionsLimit = $minorVersionLimit
             #$library.ForceCheckOut = $requireCheckOut
             $library.Update()
            }
           
            Write-Host "....library version updated : " $web.URL  $library.Title
            $msg = ("{0},{1}" -f "......library version updated:", $library.Title)
            $msg | out-file -FilePath $logfile  -append
           
            Stop-SPAssignment -Global
            #$web.Dispose()
           
      }
Catch
      {
            # $_ | Out-File d:\surya\errors.txt -Append
          $msg = ( "{0},{1}" -f "Error occurred while updating version:", $_)
            $msg | out-file -FilePath $logfile  -append
      }
     
      $i++
           
}
$web.Dispose()
$site.Dispose()






No comments:

Post a Comment

Followers