Forum Discussion

Alan2022's avatar
Alan2022
Iron Contributor
Oct 12, 2022
Solved

Sharepoint shared excel file user active session?

Hi Community,

 

Is there a way to list all the user active session who opened the shared excel file in sharepoint?

Thanks.

  • Alan2022's avatar
    Alan2022
    Oct 13, 2022

    Hi ganeshsanap,

     

    Thank you very much for the guide. With this PS script it can already view user who open the shared file in excel.

    $UserName = ""
    $Password = ""
    $credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $UserName,$Password
    
    # Set Location of Sharepoint 
    $url = "https://<name>.sharepoint.com/personal/<site>"
    
    # Connect to Sharepoint
    Connect-PnpOnline $url -Credential $credential
    
    # Get Context
    $clientContext = Get-PnPContext
     
    # -Url: relative path of the file
    # -AsListItem: Retrieves the file and returns it as a ListItem object
    $ListItem = Get-PnPFile -Url "/Documents/<filename.xlsx>" -AsListItem
    
    # Get the File object
    $targetFile = $ListItem.File
     
    $clientContext.Load($targetFile)
     
    $clientContext.ExecuteQuery()
    
    # Get user who locked out the file
    $User = $targetFile.LockedByUser
     
    $clientContext.Load($User)
     
    $clientContext.ExecuteQuery()
    
    if($User.Title){
        $User.Title
    }
    else{
        Write-Host "No User found!"
    }
     
    Disconnect-PnPOnline

     

    Cheers 👍👍👍.

     

  • Alan2022 Do you want to check if user opened file for "viewing" or "editing"?

     

    If you want to get the information related to who locked file for editing, you can use REST API endpoint like: 

     

    _api/web/GetFileByServerRelativeUrl('URL')/lockedByUser

     


    Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

    • Alan2022's avatar
      Alan2022
      Iron Contributor

      Hi ganeshsanap,

       

      Thank you very much for the guide. With this PS script it can already view user who open the shared file in excel.

      $UserName = ""
      $Password = ""
      $credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $UserName,$Password
      
      # Set Location of Sharepoint 
      $url = "https://<name>.sharepoint.com/personal/<site>"
      
      # Connect to Sharepoint
      Connect-PnpOnline $url -Credential $credential
      
      # Get Context
      $clientContext = Get-PnPContext
       
      # -Url: relative path of the file
      # -AsListItem: Retrieves the file and returns it as a ListItem object
      $ListItem = Get-PnPFile -Url "/Documents/<filename.xlsx>" -AsListItem
      
      # Get the File object
      $targetFile = $ListItem.File
       
      $clientContext.Load($targetFile)
       
      $clientContext.ExecuteQuery()
      
      # Get user who locked out the file
      $User = $targetFile.LockedByUser
       
      $clientContext.Load($User)
       
      $clientContext.ExecuteQuery()
      
      if($User.Title){
          $User.Title
      }
      else{
          Write-Host "No User found!"
      }
       
      Disconnect-PnPOnline

       

      Cheers 👍👍👍.

       

      • Alan2022 You're welcome. Glad it helped you to look into right direction!


        Please click Mark as Best Response & Like if my post helped you to solve your issue. This will help others to find the correct solution easily. It also closes the item. If the post was useful in other ways, please consider giving it Like.

Resources