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 👍👍👍.

     

Resources