0

I am trying to get modern page view count by graph,search or powershell. But dont find any proper documentation on this. has anyone tried this ?

1 Answer 1

0

Please run the following the PowerShell script:

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll" 
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll" 
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Search.dll" 
 
function Get-SPOListView  
 {  
   param(
   [Parameter(Mandatory=$true,Position=1)]  
   [string]$Username,  
   [Parameter(Mandatory=$true,Position=2)]  
   $AdminPassword,  
   [Parameter(Mandatory=$true,Position=3)]  
   [string]$Url,  
   [Parameter(Mandatory=$true,Position=4)]  
   [string]$ListTitle,  
   [Parameter(Mandatory=$true,Position=5)]  
   [string]$TenantUrl  
   )  
  #Get the SharePoint List/Library.
   $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)  
   $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $Password)  
   $ll=$ctx.Web.Lists.GetByTitle($ListTitle)  
   $ctx.load($ll)  
   $ctx.ExecuteQuery()  
  #Get all items from the List/Library.
   $qry = [Microsoft.SharePoint.Client.CamlQuery]::CreateAllItemsQuery()  
   $items = $ll.GetItems($qry)   
   $ctx.Load($items)  
   $ctx.ExecuteQuery()  
   foreach($listItem in $items)  
   {  
     Write-Host "ID - " $listItem["ID"] "Title - " $listItem["Title"] "EncodedAbsUrl - " $listItem["FileRef"]     
     $fileurl = $TenantUrl+ $listItem["FileRef"]  
     #Using Search API - Create the instance of KeywordQuery and set the properties.
     $keywordQuery = New-Object Microsoft.SharePoint.Client.Search.Query.KeywordQuery($ctx)   
     #Sample Query - To get the result of last year.
     $queryText="Path:" + $fileurl  
     $keywordQuery.QueryText = $queryText  
     $keywordQuery.TrimDuplicates=$false  
     $keywordQuery.SelectProperties.Add("ViewsLifeTime")  
     $keywordQuery.SelectProperties.Add("ViewsLifeTimeUniqueUsers")  
     $keywordQuery.SortList.Add("ViewsLifeTime","Asc")   
     #Search API - Create the instance of SearchExecutor and get the result.
     $searchExecutor = New-Object Microsoft.SharePoint.Client.Search.Query.SearchExecutor($ctx)  
     $results = $searchExecutor.ExecuteQuery($keywordQuery)  
     $ctx.ExecuteQuery()  
     #Result Count  
     Write-Host $results.Value[0].ResultRows.Count  
     #CSV file location, to store the result  
     $exportlocation = "C:\Pages_ViewsCount.csv"  
     foreach($result in $results.Value[0].ResultRows)  
     {  
       $outputline='"'+$result["Title"]+'"'+","+'"'+$result["Path"]+'"'+","+$result["ViewsLifeTime"]+","+$result["ViewsLifeTimeUniqueUsers"]   
       Add-Content $exportlocation $outputline   
     }   
    
   }  
   }
  


# Insert the credentials along with Admin & Tenant URLs and Call above Function.      
#Enter Username here      
$Username="username@***.onmicrosoft.com"      
#Enter Password Here      
$Password=Read-Host -Prompt "Password" -AsSecureString      
#URL of the site collection      
$Url= "site collection url"      
$ListTitle= "Site Pages"      
$TenantUrl= "Your tenant URL E.g. https://YourCompany.sharepoint.com"      
Get-SPOListView -Username $Username -AdminPassword $Password -Url $Url -ListTitle $ListTitle -TenantUrl $TenantUrl 

More information, please refer to this article.

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.