At some point, your architect/manager/customer may ask you to perform a SharePoint Migration from one version to another version. At that time, you'll be asked to analyze a lot of factors, like total data to be migrated, workflows, solution files etc. Along with that, one of the most important things is files uploaded in the document library.
Let’s say, for example, you use the Metalogix Context Matrix tool. In that, the checked out files in document library won't be migrated. To identify the files which are checked out in the entire site collection is a very tedious task manually. The below piece of PowerShell code makes life easier by getting the checked out files in the site collection in one shot. Here is the PowerShell code which will get the document library items from the site collection which are checked out.
- #Add the PowerShell SnapIn Add - PSSnapin microsoft.sharepoint.powershell
- # Enter the Site Collection URL $spWeb = Get - SPWeb "http://insiscvmsrv70:8888/sites/KM/"
- #Function to get the CheckedOut items in document library
-
- function GetCheckedItems($spWeb) {
- Write - Host "Scanning Site: $($spWeb.Url)"
- foreach($list in ($spWeb.Lists | ? {
- $_ - is[Microsoft.SharePoint.SPDocumentLibrary]
- })) {
- Write - Host "Scanning List: $($list.RootFolder.ServerRelativeUrl)"
- foreach($item in $list.CheckedOutFiles) {
- if (!$item.Url.EndsWith(".aspx")) {
- continue
- }
- $writeTable = @ {
- "URL" = $spWeb.Site.MakeFullUrl("$($spWeb.ServerRelativeUrl.TrimEnd('/'))/$($item.Url)");
- "Checked Out By" = $item.CheckedOutBy;
- "Author" = $item.File.CheckedOutByUser.Name;
- "Checked Out Since" = $item.CheckedOutDate.ToString();
- "File Size (KB)" = $item.File.Length / 1000;
- "Email" = $item.File.CheckedOutByUser.Email;
- }
- New - Object PSObject - Property $writeTable
- }
- foreach($item in $list.Items) {
- if ($item.File.CheckOutStatus - ne "None") {
- if (($list.CheckedOutFiles | where {
- $_.ListItemId - eq $item.ID
- }) - ne $null) {
- continue
- }
- $writeTable = @ {
- "URL" = $spWeb.Site.MakeFullUrl("$($spWeb.ServerRelativeUrl.TrimEnd('/'))/$($item.Url)");
- "Checked Out By" = $item.File.CheckedOutByUser.LoginName;
- "Author" = $item.File.CheckedOutByUser.Name;
- "Checked Out Since" = $item.File.CheckedOutDate.ToString();
- "File Size (KB)" = $item.File.Length / 1000;
- "Email" = $item.File.CheckedOutByUser.Email;
- }
- New - Object PSObject - Property $writeTable
- }
- }
- }
- foreach($subWeb in $spWeb.Webs) {
- GetCheckedItems($subWeb)
- }
- $spWeb.Dispose()
- }
- GetCheckedItems($spWeb) | Out - GridView
- # As an alternate option you can export the data to a textfile as well.Uncomment below code to do that
- # GetCheckedItems($spWeb) | Out - File c: \CheckedOutItemsInSiteCollection.txt - width 300
Once you run the PowerShell script using PowerShell ISE, you get the below output.
Hope it helps fellow developers.