Tue. Jan 4th, 2022
Function Check-in-Files([String]$WebDomain,[String]$sprint,[String]$srcURL,[String]$dstURL,[String]$SiteTitle,[String]$ReportOutput, $SummaryFile, $choice, $Credentials, $SPOCredentials)
{

     Try{

        $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($srcURL);
        $ctx.Credentials = New-Object System.Net.NetworkCredential($src_Username, $Networkpassword)

        #Get the Web
        $Web = $Ctx.Web
        $Ctx.Load($Web)
        $Ctx.Load($Web.Webs)
        $Ctx.ExecuteQuery()
        $Timestamp = GetTimeStamp
 
        #Get All Lists from the web
        $Lists = $Web.Lists
        $Ctx.Load($Lists)
        $Ctx.ExecuteQuery()
        $BatchSize=3000
        #Prepare the CAML query
    
        $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
        $Query.ViewXml = "@
        <View Scope = 'RecursiveAll'>
        <RowLimit Paged='True'>$BatchSize</RowLimit>
        <Query>
            <Where>
                <IsNotNull><FieldRef Name='CheckoutUser' /></IsNotNull>
            </Where>
        </Query>
        </View>"

        #Array to hold Checked out files
        $CheckedOutFiles = @()
        Write-host -f Yellow "Processing Web:"$Web.Url
       
        #Iterate through each document library in the web
        ForEach($List in ($Lists | Where-Object {$_.BaseTemplate -eq 101 -or $_.BaseType -eq "GenericList" }) )
        {
            $CurrentList = $List.Title
            $BaseType = $List.BaseType
            
            if ($Global:listExclusions.Contains($List.Title) -eq $false)
            {

             Write-host -f Yellow "Processing Document Library:"$List.Title
             #Exclude System Lists

             try{
               If($List.Hidden -eq $False)
               {
 #       if($list.title -eq "Testing"){                
                   $Ctx.Load($List)
                   $Ctx.ExecuteQuery() 
                              
                #Batch Process List items
                Do {
                        $ListItems = $List.GetItems($Query)
                        $Ctx.Load($ListItems) 
                        $Ctx.ExecuteQuery()
                        $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition

                        $CheckedOutCounter=0
                        #Get All Checked out files
                        ForEach($Item in $ListItems)
                        {
                            try
                            {
                            #Get the Checked out File data
                        $File = $Web.GetFileByServerRelativeUrl($Item["FileRef"])
                        $Ctx.Load($File)
                        $CheckedOutByUser = $File.CheckedOutByUser
                        
                        $Ctx.Load($CheckedOutByUser)
                        $Ctx.ExecuteQuery()

                        if($CheckedOutByUser.LoginName -ne $null -and $CheckedOutByUser.LoginName -ne "SHAREPOINT\system")
                        {
                            $CheckedOutCounter++    
                            Write-Host -f Green "Found a Checked out File '$($File.Name)' at $WebDomain$($Item['FileRef']), Checked Out By: $($CheckedOutByUser.LoginName)"
                            $CheckedOutFiles += New-Object -TypeName PSObject -Property @{
                                                FileName = $File.Name
                                                List = $List.Title
                                                Checkedoutstatus = "Checked Out"
                                                Location = $WebDomain+$Item['FileRef']
                                                CheckedOutBy = $CheckedOutByUser.LoginName
                                                CheckedoutTo = $CheckedOutByUser.Email}
                         
                            #check in file programmatically     
                            $File.CheckIn("Checked-in By Administrator through PowerShell!", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn)  
                            $Ctx.ExecuteQuery()
                            Write-Host -f Green "File '$($File.Name)' Checked-In Successfully!"
                         }
                        }catch
                         {
                          write-host -f Red "Error finding file!" $File.Name $_.Exception.Message 
                         }
                    }#End Foreach

                  
                }While($Query.ListItemCollectionPosition -ne $Null) #End Do
#} # if Testing
                                    
               }
                }Catch
                {
                     if($BaseType -ne "GenericList")
                    {
                    write-host -f Red "Error connecting to List $CurrentList Use Sharegate UI to capture Checked out files on this list." $_.Exception.Message
                    $Summaryoutput = "$Sprint,$srcURL,$SiteTitle,$BaseType,$CurrentList"
                    Add-Content $SummaryFile $Summaryoutput 
                    }
                }
            }
 
        If($CheckedOutCounter -gt 0)
        {
            #Export the Findings to CSV File
            $ReportFullPath = $ReportOutput + '\' + $SiteTitle + '_' + $CheckedOutCounter.ToString() +  '_CheckedOutFiles ' + $Timestamp + '.csv'
            write-host Saving to $ReportFullPath - $CheckedOutCounter -ForegroundColor Green
            $CheckedOutFiles| Export-CSV $ReportFullPath -NoTypeInformation -Append
            #$URLfromiManager = Get-Site-From-iManager -SiteTitle $SiteTitle -credentials $credentials
            #write-host "SiteURL from iManager" $URLfromiManager

            IF($choice -eq $true)
            {
                $ReportFullPath = $ReportOutput + '\' + $SiteTitle + '_' + $CheckedOutCounter.ToString() +  '_MigratedCheckedInFiles ' + $Timestamp + '.csv'
                MigrateCheckedInFiles -srcURL $srcURL -dstURL $dstURL -SiteTitle $SiteTitle -List $List.title -ReportOutput $ReportFullPath -Credentials $Credentials -SPOCredentials $SPOCredentials
            }#if

        }#if

        }#For EAch
    }
    Catch {
        write-host -f Red "Error Generating Checked Out Files Report!" $_.Exception.Message
    }
}

This website uses cookies. By continuing to use this site, you accept our use of cookies.