0

I have a document library with around 350 documents. I have accidentally published all the document in this library (through Site Admin - Content and Structure). Some documents were checked out. Is there a way I am able to run a report to find the checked out files or is there a way to undo what I have done? I know I am able to unpublish but I am not certain if I am going to have the files as they were prior to the action.

1 Answer 1

0

You can use powershell to export all the checked out files into an excel csv format

you need to use following dll for reference in the csom powershell

Microsoft.SharePoint.Client.dll Microsoft.SharePoint.Client.Runtime.dll

  1. You create a ps file and copy the below code in it

    $host.Runspace.ThreadOptions = "ReuseThread"
    
    function Get-CheckedOutDocument
    {
    param ($sCSOMPath, $sSiteUrl,$ListTitle,$sUserName,$sPassword,$sWeburl,$FileName)
    try
    {
        Write-Host "Getting all checked out documents from $($ListTitle)"
    
        #Adding the Client OM Assemblies                    
        Add-Type -Path "C:\\Microsoft.SharePoint.Client.dll"          
        Add-Type -Path "C:\\Microsoft.SharePoint.Client.Runtime.dll"        
    
        #SPO Client Object Model Context 
        $spoCtx = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteUrl) 
        #$spoCredentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($sUserName, $sPassword)   
        $spoCredentials = New-Object System.Net.NetworkCredential($sUserName, $sPassword)   
        $spoCtx.Credentials = $spoCredentials       
    
        $web = $spoCtx.Web 
        $List = $web.Lists.GetByTitle($ListTitle);
    
        $caml="<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='FSObjType' /><Value Type='Lookup'>0</Value></Eq></Where></Query></View>"
        $cquery = New-Object Microsoft.SharePoint.Client.CamlQuery
        $cquery.ViewXml=$caml    
        $listItems = $List.GetItems($cquery)
    
        $spoCtx.Load($List)
        $spoCtx.Load($listItems) 
        $spoCtx.ExecuteQuery() 
    
        $AllListItems = @()
        foreach ($listitem in $listItems)  
        {  
            $Title =""
            $FileType =""
            $FileRef =""
            $FileSize ="0 KB"
            $CreatedBy =  "System Account"
            $ModifiedBy = "System Account"
            $IsCheckedoutToLocal = "No"
            $CheckoutUser =""
            $CheckoutUserEmail =""
            If([string]::IsNullOrEmpty($listitem.FieldValues["FileLeafRef"]))
            {
                $Title = "No Title"
            }
            else
            {
                $Title =$listitem.FieldValues["FileLeafRef"]
            }
    
            If([string]::IsNullOrEmpty($listitem.FieldValues["File_x0020_Type"]))
            {
                $FileType = "None"
            }
            else
            {
                $FileType =$listitem.FieldValues["File_x0020_Type"]
            }
    
            If([string]::IsNullOrEmpty($listitem.FieldValues["FileRef"]))
            {
                $FileRef = "None" 
            }
            else
            {
                $FileRef =($sWeburl + $listitem.FieldValues["FileRef"])
            }
    
            If([string]::IsNullOrEmpty($listitem.FieldValues["File_x0020_Size"]))
            {
                $FileSize = "0 KB" 
            }
            else
            {
                $value = [math]::Round(($listitem.FieldValues["File_x0020_Size"]/1024),2)        
                $decimal = "{0:N2}" -f $value        
                $FileSize = ($decimal + " KB")
                #Write-Host " Field Size2 = $($FileSize)"
            }
    
            If([string]::IsNullOrEmpty($listitem.FieldValues["IsCheckedoutToLocal"]))
            {
                $IsCheckedoutToLocal = "No" 
            }
            else
            {
                $value = $listitem.FieldValues["IsCheckedoutToLocal"]
                if($value -ne "0")
                {
                    $IsCheckedoutToLocal ="Yes"
    
                    If([string]::IsNullOrEmpty($listitem.FieldValues["CheckoutUser"]))
                    {
                        $CheckoutUser = "No" 
                    }
                    else
                    {
                        $FieldCheckoutUser = [Microsoft.SharePoint.Client.FieldUserValue]$listitem.FieldValues["CheckoutUser"]
                        $CheckoutUser =$FieldCheckoutUser.LookupValue
                    }
                }
                else
                {
                    $IsCheckedoutToLocal ="No"
                }
    
            }
    
            if($IsCheckedoutToLocal -eq "Yes")
            {
    
                $AllListItems += New-Object -TypeName PSObject -Property @{
    
                                    SiteUrl =$sSiteUrl
                                    ListTitle = $ListTitle
                                    DocumentID=$listitem.ID
                                    FileName=$Title
                                    FileType= $FileType
                                    FileSize= $FileSize
                                    FileRef= $FileRef   
                                    IsCheckedoutToLocal =$IsCheckedoutToLocal
                                    CheckoutUser =$CheckoutUser
                                    CreatedBy = $listitem.FieldValues["Created_x0020_By"]
                                    ModifiedBy = $listitem.FieldValues["Modified_x0020_By"]
                                    CreatedDate = $listitem.FieldValues["Created_x0020_Date"]
                                    ModifiedDate = $listitem.FieldValues["Last_x0020_Modified"]
    
                                    } | Select  SiteUrl,ListTitle,DocumentID,FileName,FileType,FileSize,FileRef,IsCheckedoutToLocal,CheckoutUser,CreatedBy,ModifiedBy,CreatedDate,ModifiedDate
            }
        } 
        $AllListItems| Export-CSV ($sCSOMPath+"\"+$FileName+".csv") -NoTypeInformation -Append #-Encoding UTF8
    
        $spoCtx.Dispose() 
        Read-Host -Prompt "Document created at $($sCSOMPath)\. Please presss any key to close this"
    
    
    }
    catch [System.Exception] 
            { 
                Write-Host -ForegroundColor Red $_.Exception.ToString()    
                Read-Host -Prompt "Operation failed..! Press any key to close this and re run the script"
            }     
        } 
    
    $FileName = "Checkedout_Document_Collection"
    $sWeburl = "http://rootwebsiteurl"               
    $sSiteUrl = "http://site collection url"    
    $sUserName = "domain\userid"            
    $sPassword =  "********"  
    $ListTitle = "Document LibraryName"
    
    $scriptpath = $MyInvocation.MyCommand.Path
    $dir = Split-Path $scriptpath
    
    
    
    Get-CheckedOutDocument -sCSOMPath $dir -sSiteUrl $sSiteUrl -ListTitle $ListTitle -sUserName $sUserName -sPassword $sPassword -sWeburl $sWeburl -FileName $FileName
    
  2. Give the Client dll path properly and save the ps file

  3. right clieck and run the file with powershell
  4. it will create the csv file which has all checked out documents
3
  • I am not sure if this will help me as all the checked out files are now checked in - How can I get the previous checked in value of the documents which are now published?
    – garvon-77
    Commented Mar 20, 2017 at 21:12
  • This will help you to retrieve all checked out documents only. In your case if you all chekced out files are checked in and published. what you can do is only retrieve all the version history of each file and who checked in Commented Mar 21, 2017 at 4:37
  • I am getting $listitem.FieldValues["File_x0020_Size"] as "blank", similarly If I try EncodedUrl, not getting field values in Powershell CSOM. I am getting values for FileRef, FileLeafRef etc..Whereas If I try this in CSOM, then I am getting values. is there anything we have to do in Powershell? Commented Oct 1, 2019 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.