Forum Discussion

Jussi Palo's avatar
Jussi Palo
Iron Contributor
Feb 15, 2017

Office PnP PowerShell: Get file CheckOutStatus

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!
  • 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
    }  

     

     

    • Jussi Palo's avatar
      Jussi Palo
      Iron Contributor

      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()
        } 
  • Pete Gilchrist's avatar
    Pete Gilchrist
    Copper Contributor

    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.

Resources