SharePoint Pals
 | Sign In
Step by Step procedure to Programmatically Implement LogAnalytics against a SharePoint 2013 ListItem
by Sathish Nadarajan 23 Jul 2013
SharePoint MVP
Today  :   4     Total  :    15259

Let us have a look into the SharePoint 2013 Analytics namespace in the Microsoft.SharePoint.Client.dll, which will be used to LogAnalytics against any list item programmatically. There are certain steps needs to be followed to do the LogAnalytics. Let us dive into them.

In SharePoint 2013, by going to the Central Administration->Monitoring->Reporting, we can identify the number of hits to a site, number of users etc. But as far as concerning a product catalog list or any custom list, if we want to see how many times, the item has been viewed, modified kind of information, then there is no possibility by default.

The practical scenario for this would be as follows. I have a List which contains the information about the Products. The search should be optimized in such a way that, if a particular product is been viewed by most of the viewers, then that product should be listed on top of the Search. This is obvious that the site should behave like this only.

To make this work, Microsoft has given a separate namespace called Microsoft.SharePoint.Client.Analytics, which is internally works with Search index and there are many articles to explain about them. In this article, we are going to see the procedure to Implement LogAnalytics from a Provider Hosted Application. First of all, we should be clear about the problem statement. Hence the problem statement would be

“From the Provider Hosted Application(PHA), I will be viewing the products(List Item in the ProductCatalog List). The products which were viewed highest number of times, should be listed on the top of the Search Results.”

This is the one line problem statement. To achieve this programatically, MSFT has given certain API’s. Since we are going to log everything from the PHA. Basically, what we are going to do is, log certain events. Viewing is an event, which needs to be logged, every time the event occurred. Hence the Search service will take the count (Number of times the event occurred), and list it on the top of the Results page.

To achieve this, we need to register the event to the SharePoint first. For that, I am going to use the following PowerShell Script.

 Add-PSSnapin "Microsoft.SharePoint.PowerShell"
 $Site = Get-SPSite "My Site Collection URL"
 $SSP = Get-SPEnterpriseSearchServiceApplicationProxy
 $EventGuid = [Guid]::NewGuid();
 $EventName = "View the Item";
 $tenantConfig = $SSP.GetAnalyticsTenantConfiguration([Guid]::Empty);#
 $newEventType = $tenantConfig.RegisterEventType($EventGuid, $EventName, "TestValue")
 Write-Host $EventGuid 

On the Successful registration, the output would be a GUID on the PowerShell window. Copy the GUID. That needs to be passed from the Provider Hosted Application.

Now, our event got registered and we got the GUID corresponding to the event. We need to verify whether any event has been registered on the SharePoint or not right. There is a way to find this on the Physical files of SharePoint Farm.

Navigate to the path “C:\Program Files\Microsoft Office Servers\15.0\Data\Office Server\Analytics_******”. There you will be able to find some txt files which will contains the information about the event which we registered.

Now we are ready to register the Event from our Provider Hosted Application. Now, let us go back to our .Net (PHA). There let us assume that we have a button to register the event. Hence on the Button Click, the code will be

 protected void btnLogAnalytics_OnClick(object sender, EventArgs e)
         CsomHelper csomHelper = new CsomHelper();
         ClientContext clientContext = csomHelper.GetClientContext("MySiteCollection URL", HttpContext.Current.User);
         Guid guid = new Guid("4f09fbec-180a-430d-a77c-241566853926"); -> The copied URL from the SharePoint Farm.
         AnalyticsUsageEntry.LogAnalyticsAppEvent(clientContext, guid, GetItemURLByNumber("MySiteCollection URL", "Value", "ListName"));
     catch (Exception ex)
         string strException = ex.Message;

//GetItemURLByNumber – Method will return a string in the form of URL of the List Item. Basically on the method, we are trying to get the URL of the List Item based on the Number in a particular column and the List name.

The URL of the List Item should be as follows. https://c4968397007.dc07.loc:9470/sites/ADFSTest/Lists/ProductCatalog/1_.000

 private string GetItemURLBySKUNumber(string SPHostUrl, string Number, string ListName)
     string ItemUrl = string.Empty;
     Helper csomHelper = new Helper();
     //How to get the ClientContext has been discussed early in this blog.  ( )
     ClientContext clientContext = csomHelper.GetClientContext(SPHostUrl, HttpContext.Current.User);
     Web web = clientContext.Web;
     var oList = web.Lists.GetByTitle(ListName);
     CamlQuery camlQuery = new CamlQuery();
     camlQuery.ViewXml = "<View><Query><Where><Eq><FieldRef Name='" + "Number" + "'/><Value Type='Text'>" + Number + "</Value></Eq></Where></Query></View>";
     Microsoft.SharePoint.Client.ListItemCollection collListItem = oList.GetItems(camlQuery);
     Microsoft.SharePoint.Client.ListItem oListItem = collListItem.First();
     char[] delimiters = new char[] { '/' };
     string[] temp = Convert.ToString(oListItem.FieldValues["FileRef"]).Split(delimiters);
     ItemUrl = string.Format("{0}Lists/{1}/{2}", Convert.ToString(clientContext.Url), ListName, temp[temp.Count() - 1]);
     return ItemUrl;

With this, we are successfully able to call the LogAnalyticsAppEvent method. Even to verify this also, the go to the same location on the SharePoint Farm and you can find an entry with the same entry.

blog comments powered by Disqus

SharePoint Pals

SharePoint Pals, a community portal for SharePoint developers, Administrators and End Users. Let's join hands and share the point together.
Read this on mobile


Angular Js Training In Chennai
Advanced Angular Js training with real world developer scenarios
Angular Js, Web Api and Ionic for .Net Developers
All in one client side application development for .Net developers
Angular Js For SharePoint Developers
Get ready for the future. Its no more just C#

Get Connected

SharePoint Resources

SharePoint 2013 and 2010 Web Parts
Free Web Parts with Source Code for SharePoint Community

SharePoint 2013 Books and Tutorials
Collection of free SharePoint 2013 books and tutorials (eBooks, pdfs)

Supported By

Contribute your article and be eligible for a one month Free Subscription for Plural Sight. The Author of the most popular New Article (published in the previous month) will be awarded with a Free One month Plural Sight Subscription. Article can be sent to in a word document.

Related Resources

Recent Tweets

Twitter October 23, 22:21
How to Add/Remove User Custom Actions (in Site Actions Menu) Programmatically using CSOM PNP in SharePoint -

Twitter October 21, 21:34
How to Add a JS Link Reference to the Display Form or Any other ASPX Programmatically using CSOM PNP in SharePoint -

Twitter October 20, 13:01
How to Add a JS Link Reference to the NewForm and EditForm Programmatically using CSOM PNP in SharePoint Office 365-

Twitter October 12, 12:15
How to Deploy Provider Hosted Apps (Add-Ins) by App Stapling in SharePoint Office 365 -

Twitter October 11, 13:39
How to Deploy Provider HostedApp programmatically using CSOM in SharePoint Office 365 Activating Developer Feature -

Follow us @SharePointPals
Note: For Customization and Configuration, CheckOutRecent Tweets Documentation