Aarebrot.net

Frodes website about Sharepoint and other stuff...

  • Increase font size
  • Default font size
  • Decrease font size

Powershell: Check-in multiple files!

E-mail Print
User Rating: / 0
PoorBest 

Checking in multiple files in a library

I do a lot of CSS styling with my current project, and with that comes a lot of trial and error. I make image files, upload them, see how they look, delete them, upload new ones, take a look again, and so on. Depending on what stage I am in designing the page, I sometimes just stick the images in the Style Library and load them from there inside my CSS.

Luckily there's a "Upload multiple files" option in Sharepoint, but unfortunately there's isn't a quick way of checking in multiple files of a single directory. Well, there sort of is... But I just like Powershell better. Anyway, for the longest time I was clicking on each file individually and checking them in one by one. I figured there has got to be an easier way! Turns out the answer is Powershell.

Actually, there's apparently a way to do it in Site Settings as well, but I haven't tried it because Powershell is just so darned handy.

Anyway, after a whole whackload of research and some trial and error, I finally came up with a script that allows me to check-in and publish all files that are currently checked out. It's pretty nifty if I may say so myself.

This script isbased of another script written by Colin Byrne. If you think my script is the best thing since sliced bread,you really need to thank him, not me. I found a code snippet on his blog to publish pages in a site collection, and worked my way from there. You can view Colin's original script here.

Here is the full code:

  1. # Description
  2. #   Check-in and Publishes all checked-out files in a library
  3. #
  4. # Syntax
  5. #   ./pubFiles [[-l] <string>] [[-f] <string>]
  6. #
  7. # Parameters
  8. #   [[-l] <string>]
  9. #       Specify an alternate library to publish files in
  10. #
  11. #   [[-f] <string>]
  12. #       Filter to use when searching for files to publish
  13. #
  14. #       Example:
  15. #       ./pubFiles -f file.jpg      - returns exact match
  16. #       ./pubFiles -f ?ile.jpg      - single character wildcard
  17. #       ./pubFiles -f *e.jpg        - multiple character wildcard
  18. #
  19. # Settings
  20. #   Only change the -value parameter!
  21. #
  22. set-variable -option constant -name url     -value "http://localhost:31337"     # Site collection
  23. set-variable -option constant -name comment -value "System Approval"            # Publishing comment
  24. set-variable -option constant -name lib     -value "Style Library"              # Library to publish
  25. set-variable -option constant -name dfilter -value "*"                          # Default file filter
  26. # End of settings
  27.  
  28. # Function: Approve-File
  29. # Description: Approve a single file in a Publishing Web
  30. # Parameters: publishingPage File object
  31. # comment Comment to accompany the check-in/approve/publish
  32. #
  33. function Approve-File ([Microsoft.SharePoint.SPListItem]$pubFile, [string]$comment) {
  34.     "Processing " + $pubFile.Name
  35.     $listitemfile = $pubFile.File
  36.  
  37.     # Check item if checked out
  38.     if ($listitemfile.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
  39.     {
  40.         " Checking in file"
  41.         $listitemfile.CheckIn($comment,[Microsoft.SharePoint.SPCheckInType]::MajorCheckin )
  42.     }
  43.  
  44.     # If moderation is being used then handle the approval and publishing
  45.     if ($pubFile.ParentList.EnableModeration)
  46.     {
  47.         $modInformation = $pubFile.ModerationInformation
  48.         " Moderation Enabled"
  49.  
  50.         # Check for pending approval
  51.         if($modInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Pending)
  52.         {
  53.             " Approving"       
  54.             $listitemfile.Approve($comment)
  55.         }
  56.  
  57.         # Publish
  58.         if($modInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Draft)
  59.         {
  60.             " Publishing"
  61.             $listitemfile.Publish($comment)
  62.         }
  63.     }
  64. }
  65.  
  66. # Function: Approve-AllPagesInSPWeb
  67. # Description: Loop through all the pages in a Publishing Web and checkin and approve them
  68. # Parameters: web SPWeb object
  69. # comment Comment to accompany the checkin/approve/publish
  70. #
  71. function Approve-AllPagesInSPWeb([Microsoft.SharePoint.SPWeb]$web, [string]$comment, [string]$destination, [string]$filter)
  72. {
  73.     # Check this is a publishing web
  74.     if ([Microsoft.SharePoint.Publishing.PublishingWeb]::IsPublishingWeb($web) -eq $true)
  75.     {
  76.         # just a quick loop to space out dashes
  77.         for($c = 0; $c -lt $destination.length; $c++)
  78.         {
  79.             $dshspcr += "-"
  80.         }
  81.  
  82.         # provide some feedback
  83.         ""
  84.         "Checking " + $destination + "..."
  85.         "---------" + $dshspcr + "---"
  86.         ""
  87.  
  88.         # do some stuff
  89.         $list = $web.Lists[$destination]            # Load library we want to check
  90.         [Object[]]$files = $list.get_items() | where { $_.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout} | where { $_.Name -like $filter }
  91.         $formatted = $files | ft -Autosize Url, Name, ID, Level, HasPublishedVersion                   
  92.         if($files.count -gt 0)
  93.         {
  94.             "Found the following files:"
  95.             $formatted
  96.             "Checking in files..."
  97.             for($i=0; $i -lt $files.count; $i++)
  98.             {
  99.                 Approve-File $files[$i] $comment
  100.             }
  101.         }
  102.         else
  103.         {
  104.             " - Found no files"
  105.         }      
  106.         $web.Dispose()
  107.     }
  108.  
  109. }
  110. # Handle input parameters
  111. for($c = 0; $c -lt $args.length; $c++)
  112. {  
  113.     if($args[$c] -eq "-f")
  114.     {
  115.         $c++
  116.         $filter = $args[$c]
  117.     }
  118.     elseif($args[$c] -eq "-l")
  119.     {
  120.         $c++
  121.         $library$args[$c]
  122.     }
  123.     else
  124.     {
  125.         "Invalid parameter(s)"
  126.         $fail = "yes"
  127.     }
  128. }
  129.  
  130. if($library.length -lt 1)
  131. {
  132.     $library = $lib
  133. }
  134. if($filter.length -lt 0)
  135. {
  136.     $filter = $dfilter
  137. }
  138.  
  139. if($fail -ne "yes")
  140. {
  141.     # Create site object
  142.     $site = new-object Microsoft.SharePoint.SPSite($url)
  143.     $site.rootweb | foreach {Approve-AllPagesInSPweb $_ $comment $library $filter}
  144.    
  145.     #Dispose of objects
  146.     $site.Dispose()
  147.     ""
  148.     "Check-in and publish completed!"
  149.     ""
  150. }
  151.  
  152. # Changelog
  153. #
  154. #   v1.1 - June 19th, 2008
  155. #       * Added ability to publish files based on a filename filter
  156. #       * Added ability to publish files in an alternate library
  157. #       * fixed bug preventing more than 2 function parameters to ApprovePagesInSPweb
  158. #
  159. #   v1.0 - June 18th, 2008
  160. #       * First public release

If you want to download a copy of the script, use the provided link below.

Attachments:
 pubFiles.ps1[Powershell: Publish multiple files in a library]4 KbFeb-11-2010 6:17pm
Last Updated on Monday, 10 November 2008 09:15  

Add your comment

Your name:
Subject:
Comment:
  The word for verification. Lowercase letters only with no spaces.
Word verification: