Office PnP PowerShell: Get file CheckOutStatus

Iron Contributor

How to check for publishing page's CheckOutStatus?

 

        # Get pages
        foreach($page in Get-PnPListItem -Web $subweb -List "pages" -Fields "FileRef" | Where { $_.FieldValues.FSObjType -eq 0})
        {       
            Write-Host "Page '$($page["FileRef"])'"
            $file = Get-PnPFile -Url $page["FileRef"] -Web $subweb
            $file.CheckOutStatus #NONONO!
5 Replies

Check the below sample to get the file checkout status using PnP.

 

$DocLibName = "Documents"
$Context =Get-PnPContext
$List=Get-PnPList -Identity "Documents"
$Allfiles= $List.RootFolder.Files
$Context.Load($Allfiles)
$Context.ExecuteQuery()
foreach( $file in $Allfiles)
{
Write-Host "FileName" $file.Name "Status" $file.Level
}  

 

 

Thanks, looks like it cannot really be done with PnP.

 

How come the -Web parameter isn't working when looping subsites, it is compaining about wrong context:

 

    foreach($subweb in Get-PnPSubWebs -Recurse)
    {
        Write-Host "Subweb '$($subweb.Title)'"
        
        $context = Get-PnPContext

        $list = Get-PnPList -Identity "pages" -Web $subweb # <<< Here I need to get hold of current subweb pages     
        
        $allfiles = $list.RootFolder.Files
        $context.Load($allfiles)
        $context.ExecuteQuery()

        foreach($file in $allfiles)        
        {   
            Write-Host "Page '$($file.ServerRelativeUrl)'"
            
            write-host $file.Level
         }
}

Please try the below modified script.

 

$cred=Get-Credential 
Connect-PnPOnline -Url https://itrmxxxx.sharepoint.com/sites/contosobeta -Credentials $cred
   
   $subwebs=Get-PnPSubWebs -Recurse

    foreach($subweb in $subwebs)
    {
        Write-Host "Subweb '$($subweb.Title)'"

        Connect-PnPOnline -Url $subweb.Url -Credentials $cred
        
        $context = Get-PnPContext

        $list = Get-PnPList -Identity "Documents" 
        
        $allfiles = $list.RootFolder.Files
        $context.Load($allfiles)
        $context.ExecuteQuery()

        foreach($file in $allfiles)        
        {   
            Write-Host "Page '$($file.ServerRelativeUrl)'"
            
            write-host $file.Level
         }
       $Context.Dispose()
       $context.ExecuteQuery()
} 

Using $context.Load and ExecuteQuery does the trick $file.Level is loaded. However if using Get-PnPFile or Get-PnPListItem the Level attribute seems to not be loaded.

 

I didn't find a way to include more fields besides using the full execute query. Maybe a bug / feature request?

I have managed to get the checked out status of a file this way : 

 

$lists = Get-PnPList
foreach ($list in $lists | where {$_.Title -eq "Site Pages" -or $_.Title -eq "Documents"})
{
    $Items = Get-PnPListItem -List $list
    foreach ($Item in $Items) 
    {
        if ($Item.FieldValues.CheckoutUser -ne $null)
        {
            write-host $list.Title -ForegroundColor Green
            write-host ($Item.FieldValues).FileLeafRef -ForegroundColor Yellow
            write-host $Item.FieldValues.CheckoutUser.Email -ForegroundColor Red
        }
    }
}

 

If the item is checked out (in either "Site Pages" or "Documents", in this example), this script will show you the filename, and the email of the person who has it checked out.