Hi Coders,
I am trying to get hit count of each item (i.e., How many peopled viewed each page) using SharePoint Online Client Object Model. Below is the code:
using (var context = new ClientContext(targetWebUrl))
{
context.Credentials = new SharePointOnlineCredentials(userName, pwd);
Site site = context.Site;
List pagesList = context.Web.Lists.GetByTitle("Pages");
CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
ListItemCollection items = pagesList.GetItems(query);
context.Load(items, li => li);
context.Load(site);
context.ExecuteQuery();
Microsoft.SharePoint.Client.Search.Analytics.UsageAnalytics UsageAnalyticsObj = new Microsoft.SharePoint.Client.Search.Analytics.UsageAnalytics(context, site);
List<Microsoft.SharePoint.Client.Search.Analytics.AnalyticsItemData> lstAnalyticsData = new List<Microsoft.SharePoint.Client.Search.Analytics.AnalyticsItemData>();
foreach (ListItem listItem in items)
{
Microsoft.SharePoint.Client.Search.Analytics.AnalyticsItemData eachListItemAnalyticsData = UsageAnalyticsObj.GetAnalyticsItemData(1, listItem);
context.Load(eachListItemAnalyticsData);
context.ExecuteQuery();
//Getting Error in below line
Console.WriteLine(eachListItemAnalyticsData.TotalHits);
lstAnalyticsData.Add(eachListItemAnalyticsData);
}
Console.ReadLine();
}
I am receiving the following error:
An unhandled exception of type 'Microsoft.SharePoint.Client.PropertyOrFieldNotInitializedException' occurred in Microsoft.SharePoint.Client.Runtime.dll Additional information: The property or field 'TotalHits' has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested.
Please correct the code where I am going wrong. Let me know if there are any pointers of MSDN or some useful links to meet this requirement.