Powershell: Recusively Check In and Publish files in a SharePoint Document Library

I have combined the scripts from Paul King (here) and Brijendra Gautam (here) to create my own version of the script.

Function CheckInAndPublishFolderItemsRecusively( [Microsoft.SharePoint.SPFolder] $folder )
{
    # Create query object
    $query = New-Object Microsoft.SharePoint.SPQuery
    $query.Folder = $folder

    # Get SPWeb object
    $web = $folder.ParentWeb

    # Get SPList
    $list = $web.Lists[$folder.ParentListId]

    # Get a collection of items in the specified $folder
    $itemCollection = $list.GetItems($query)

    # Iterate through each item in the $folder
    foreach ($item in $itemCollection)
    {
        # If the item is a folder
        if ($item.Folder -ne $null)
        {
            # Call the Get-Items function recursively for the found sub-solder
            CheckInAndPublishFolderItemsRecusively $item.Folder
        }
        else
        {
if ($item.File.Level -ne "Published")
{
           if ($item.File.CheckOutType -ne "None")
           {
               if ($item.File.Versions.Count -eq 0)
               {
                   # Check in the file
                    $item.File.CheckIn("Checked in By Administrator", [Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
$item.File.Publish('Published using Powershell')
}
                }
}
        }
    }

    $web.dispose()

    $web = $null
}

The script is invoked as follows:
$web = Get-SPWeb $siteUrl
$folder = $web.GetFolder("Style Library")
CheckInAndPublishFolderItemsRecusively $folder

Comments

Popular posts from this blog

SharePoint 2013: Error updating managed account credentials

Error deploying Nintex workflow: An item with the same key has already been added