none
Call RecordPageClick from custom JS / HTML RRS feed

  • Question

  • I've got exactly the same question as this one:

    https://social.technet.microsoft.com/Forums/windowsserver/en-US/ee7a529f-06dc-4851-b9c6-48897defcbcf/feedback-to-search-engine-while-using-rest-api?prof=required

    But I don't know how to call RecordPageClick. There is no proper documentation available, the only thing I could find is:

    https://msdn.microsoft.com/en-us/library/hh630829(v=office.12).aspx

    Some background: I use jQuery UI autocomplete to directly show search results, using Search REST API. I want to log each click, e.g. such that the keyword will become a search suggestion after 6 clicks.

    Using Office 365.
    Monday, June 15, 2015 4:46 AM

Answers

  • Hi,

    Here's an example about using RecordPageClick in SharePoint Online.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Security;
    using System.Threading.Tasks;
    using Microsoft.SharePoint.Client;
    using Microsoft.SharePoint.Client.Search;
    using Microsoft.SharePoint.Client.Search.Query;
    using System.IO;
    
    namespace SPOnlinePageCallback2
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (ClientContext clientContext = new ClientContext("https://xxxxxxx.sharepoint.com/"))
                {
    
                    SecureString passWord = new SecureString();
                    foreach (char c in "XXXXXXXX".ToCharArray()) passWord.AppendChar(c);
                    clientContext.Credentials = new SharePointOnlineCredentials("xxxxxx@xxxxxx.onmicrosoft.com", passWord);
    
                    
                    StringBuilder sbOut = new StringBuilder();
    
    
    
                string resultTitleToClick = "tt";
                string resultUrlToClick = "https://xxxxxx.sharepoint.com/Lists/clist1/DispForm.aspx?ID=4";
    
                string immediacyQueryString = "tt";
    
                
                KeywordQuery keywordQuery = new KeywordQuery(clientContext);
                keywordQuery.RowLimit = 50;
                keywordQuery.QueryText = immediacyQueryString;
                keywordQuery.TrimDuplicates = true;
                keywordQuery.EnableStemming = true;
                keywordQuery.StartRow = 0;
    
                SearchExecutor searchExecutor = new SearchExecutor(clientContext);
                ClientResult<ResultTableCollection> resultTables = searchExecutor.ExecuteQuery(keywordQuery);
    
                clientContext.ExecuteQuery();
                
                //pageImpressionId_partitionBucket_LCID (1131_1820_1033)
                //NOTE: pageImpressionId is the search result page Id that it will change every time
                string pageInfo = resultTables.Value.Properties["piPageImpression"].ToString();
    
                string clickType = "Result";
    
     
                string immediacySourceId = resultTables.Value.Properties["SourceId"].ToString();
                    //Microsoft.SharePoint.Client.Search.Query.
                ResultTable relevantResults = resultTables.Value.Where(
                    result => result.TableType == KnownTableTypes.RelevantResults).FirstOrDefault(result => result.QueryRuleId == "00000000-0000-0000-0000-000000000000");
    
                List<IDictionary<string, object>> resultRows = (List<IDictionary<string, object>>)relevantResults.ResultRows;
    
    
                //int clickResultBlockId = (int)relevantResults.Properties["piPageImpressionBlockType"];
                int blockType = (int)relevantResults.Properties["piPageImpressionBlockType"];
    
                int subResultIndex = 0;
    
                //var clickThroughResult = resultRows[5];
                //string clickResultId = (string)relevantResults.Properties["piSearchResultId"];
    
                Dictionary<string, object> clickThroughResult = null;
                foreach(Dictionary<string, object> resultRow in resultRows)
                {
                    string resultTitle = (string)resultRow["Title"];
                    string resultUrl = (string)resultRow["Path"];
                    if ((resultTitle.ToLower() == resultTitleToClick.ToLower()) && (resultUrl.ToLower() == resultUrlToClick.ToLower()))
                    {
                        clickThroughResult = resultRow;
                        break;
                    }
                }
                //var clickThroughResult = resultRows[1];
                if (clickThroughResult != null)
                {
                    string clickedResultId = (string)clickThroughResult["piSearchResultId"];
                    string immediacyTitle = (string)clickThroughResult["Title"];
                    string immediacyUrl = (string)clickThroughResult["Path"];
    
                    
                    searchExecutor.RecordPageClick(pageInfo, clickType, blockType, clickedResultId, subResultIndex, immediacySourceId, immediacyQueryString, immediacyTitle, immediacyUrl);
                    clientContext.ExecuteQuery();
                    
                    sbOut.Append(clickedResultId + "\r\n");
                    sbOut.Append(immediacyTitle + "\r\n");
                    sbOut.Append(immediacyUrl + "\r\n");
                    sbOut.Append("Clicked");
                }
    
                int resultCount = resultRows.Count;
                Console.Write(sbOut.ToString());
    
                    Console.ReadLine();
                }
            }
        }
    }
    

    Please add reference to the following three dll for test:

    Microsoft.SharePoint.Client.dll

    Microsoft.SharePoint.Client.Runtime.dll

    Microsoft.SharePoint.Client.Search.dll

    In addition, regarding introduction to the parameters, please refer to this link:

    https://msdn.microsoft.com/en-us/library/hh630829(v=office.12).aspx

    Thanks,
    Reken Liu


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Tuesday, July 7, 2015 2:37 AM
    Moderator

All replies

  • Hi Roland,

    The RecordPageClick method is available in both SharePoint CSOM and JSOM, and here are two additional articles:

    https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.search.query.searchexecutor.recordpageclick.aspx

    https://msdn.microsoft.com/es-es/library/office/jj916814.aspx

    However, if your environment is a custom external web application, you may not be able to use it. As a workaround, I suggest you change your code to a SharePoint hosted App, and in the App, you are still able to custom UI with JQuery, and meanwhile, SharePoint JSOM can be available. Please refer to the following article about how to use JSOM in a SharePoint app:

    http://blogs.msdn.com/b/officeapps/archive/2012/09/04/using-the-javascript-object-model-jsom-in-apps-for-sharepoint.aspx

    Thanks,
    Reken Liu 


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Tuesday, June 16, 2015 5:22 AM
    Moderator
  • Hi, thank you. It's inside SharePoint, so that's not a problem.

    I've seen the definition, but what values should I pass for each parameter? What is pageInfo? clickType? immediacyTitle? 

    As I don't have access to SQL in Office 365, there's no way to tell whether the click has made it to the SQL Search logging database. The only way to find out is to wait a day to see if the query has made it to become a search suggestion.

    Any way to test it?

    Tuesday, June 16, 2015 9:25 PM
  • Hi,

    Here's an example about using RecordPageClick in SharePoint Online.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Security;
    using System.Threading.Tasks;
    using Microsoft.SharePoint.Client;
    using Microsoft.SharePoint.Client.Search;
    using Microsoft.SharePoint.Client.Search.Query;
    using System.IO;
    
    namespace SPOnlinePageCallback2
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (ClientContext clientContext = new ClientContext("https://xxxxxxx.sharepoint.com/"))
                {
    
                    SecureString passWord = new SecureString();
                    foreach (char c in "XXXXXXXX".ToCharArray()) passWord.AppendChar(c);
                    clientContext.Credentials = new SharePointOnlineCredentials("xxxxxx@xxxxxx.onmicrosoft.com", passWord);
    
                    
                    StringBuilder sbOut = new StringBuilder();
    
    
    
                string resultTitleToClick = "tt";
                string resultUrlToClick = "https://xxxxxx.sharepoint.com/Lists/clist1/DispForm.aspx?ID=4";
    
                string immediacyQueryString = "tt";
    
                
                KeywordQuery keywordQuery = new KeywordQuery(clientContext);
                keywordQuery.RowLimit = 50;
                keywordQuery.QueryText = immediacyQueryString;
                keywordQuery.TrimDuplicates = true;
                keywordQuery.EnableStemming = true;
                keywordQuery.StartRow = 0;
    
                SearchExecutor searchExecutor = new SearchExecutor(clientContext);
                ClientResult<ResultTableCollection> resultTables = searchExecutor.ExecuteQuery(keywordQuery);
    
                clientContext.ExecuteQuery();
                
                //pageImpressionId_partitionBucket_LCID (1131_1820_1033)
                //NOTE: pageImpressionId is the search result page Id that it will change every time
                string pageInfo = resultTables.Value.Properties["piPageImpression"].ToString();
    
                string clickType = "Result";
    
     
                string immediacySourceId = resultTables.Value.Properties["SourceId"].ToString();
                    //Microsoft.SharePoint.Client.Search.Query.
                ResultTable relevantResults = resultTables.Value.Where(
                    result => result.TableType == KnownTableTypes.RelevantResults).FirstOrDefault(result => result.QueryRuleId == "00000000-0000-0000-0000-000000000000");
    
                List<IDictionary<string, object>> resultRows = (List<IDictionary<string, object>>)relevantResults.ResultRows;
    
    
                //int clickResultBlockId = (int)relevantResults.Properties["piPageImpressionBlockType"];
                int blockType = (int)relevantResults.Properties["piPageImpressionBlockType"];
    
                int subResultIndex = 0;
    
                //var clickThroughResult = resultRows[5];
                //string clickResultId = (string)relevantResults.Properties["piSearchResultId"];
    
                Dictionary<string, object> clickThroughResult = null;
                foreach(Dictionary<string, object> resultRow in resultRows)
                {
                    string resultTitle = (string)resultRow["Title"];
                    string resultUrl = (string)resultRow["Path"];
                    if ((resultTitle.ToLower() == resultTitleToClick.ToLower()) && (resultUrl.ToLower() == resultUrlToClick.ToLower()))
                    {
                        clickThroughResult = resultRow;
                        break;
                    }
                }
                //var clickThroughResult = resultRows[1];
                if (clickThroughResult != null)
                {
                    string clickedResultId = (string)clickThroughResult["piSearchResultId"];
                    string immediacyTitle = (string)clickThroughResult["Title"];
                    string immediacyUrl = (string)clickThroughResult["Path"];
    
                    
                    searchExecutor.RecordPageClick(pageInfo, clickType, blockType, clickedResultId, subResultIndex, immediacySourceId, immediacyQueryString, immediacyTitle, immediacyUrl);
                    clientContext.ExecuteQuery();
                    
                    sbOut.Append(clickedResultId + "\r\n");
                    sbOut.Append(immediacyTitle + "\r\n");
                    sbOut.Append(immediacyUrl + "\r\n");
                    sbOut.Append("Clicked");
                }
    
                int resultCount = resultRows.Count;
                Console.Write(sbOut.ToString());
    
                    Console.ReadLine();
                }
            }
        }
    }
    

    Please add reference to the following three dll for test:

    Microsoft.SharePoint.Client.dll

    Microsoft.SharePoint.Client.Runtime.dll

    Microsoft.SharePoint.Client.Search.dll

    In addition, regarding introduction to the parameters, please refer to this link:

    https://msdn.microsoft.com/en-us/library/hh630829(v=office.12).aspx

    Thanks,
    Reken Liu


    TechNet Community Support
    Please remember to mark the replies as answers if they help, and unmark the answers if they provide no help. If you have feedback for TechNet Support, contact tnmff@microsoft.com.

    Tuesday, July 7, 2015 2:37 AM
    Moderator