Step by Step procedure to Programmatically Implement LogAnalytics against a SharePoint 2013 ListItem

Sathish Nadarajan
 
Solution Architect
July 23, 2013
 
Rate this article
 
Views
21457

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.

 cls
 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")
 $tenantConfig.Update($SSP)
 
 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)
 {
     try
     {
         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"));
         clientContext.ExecuteQuery();
 
         clientContext.Dispose();
 
 
     }
     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.  (https://www.sharepointpals.com/post/Getting-the-Client-Context-from-Claims-Aware-Provider-Hosted-Application-in-SharePoint-2013. )
     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);
 
     clientContext.Load(collListItem);
     clientContext.ExecuteQuery();
 
     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]);
 
     clientContext.Dispose();
 
     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.

Author Info

Sathish Nadarajan
 
Solution Architect
 
Rate this article
 
Sathish is a Microsoft MVP for SharePoint (Office Servers and Services) having 15+ years of experience in Microsoft Technologies. He holds a Masters Degree in Computer Aided Design and Business ...read more
 

Leave a comment