.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!



Home >> Articles >> SharePoint >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Get Search Results in SharePoint Hosted APP Using JavaScript

Posted By:Kaviya Balasubramanian       Posted Date: October 13, 2015    Points: 200    Category: SharePoint    URL: http://www.dotnetspark.com  

This article provides code to get search results in a SharePoint Hosted app using JavaScript.
 

In this article I would like to share the code to get the search results in a SharePoint Hosted app using JavaScript.

SharePoint provides custom ways to get search results using REST. JSOM. Here I provide the code to get search results using the JavaScript object model.

getSearchResults Method

In this method I've got the search results based on content type and bind the search result title value in a lable on OnQuerySuccess.

Use the following code to get the search results.

  1. var ctx;
  2. var results;  
  3. // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model   
  4. $(document).ready(function ()  
  5. {  
  6.    // Get the URI decoded URLs.    
  7.    appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));  
  8.    ExecuteOrDelayUntilScriptLoaded(getSearchResults, "sp.js");  
  9.   
  10. });  
  11. //This function is used to get the hostweb url  
  12. function manageQueryStringParameter(paramToRetrieve)
  13. {  
  14.    var params = document.URL.split("?")[1].split("&");  
  15.    var strParams = "";  
  16.    for (var i = 0; i < params.length; i = i + 1) 
  17.    {  
  18.       var singleParam = params[i].split("=");  
  19.       if (singleParam[0] == paramToRetrieve) 
  20.       {  
  21.          return singleParam[1];  
  22.       }  
  23.    }  
  24. }  

  25. //Get Search results based on Content Type ID  

  26. function getSearchResults() 
  27. {  
  28.    // As per your requirement you can change the "queryStr", here I got the result based on content type  
  29.    var queryStr = "ContentTypeId:0x0100BEAD925C1BB0494FB50B0D4B0B9F7325*"//content Type ID  
  30.    ctx = new SP.ClientContext(appWebUrl); // get current App Context     
  31.    var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(ctx);  
  32.    keywordQuery.set_queryText(queryStr);  
  33.    var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(ctx);  
  34.    results = searchExecutor.executeQuery(keywordQuery);  
  35.    ctx.executeQueryAsync(onQuerySuccess, onQueryFail);  
  36.    // method will exit here and onQuerySuccess or onQueryFail will be called asynchronously  
  37.   
  38. }  
  39.   
  40. //bind the Results in Div  

  41. function onQuerySuccess()
  42. {  
  43.   
  44.   var searchResult= results.m_value.ResultTables[0].ResultRows;
  45.    $.each(searchResults, function () {   
       //if you want more properties you can get by managed properties in search results
       var NewsTitle = this.Title != null ? this. Title: "";
       document.getElementById(Title).innerText += NewsTitle;
       });
  46. }  
  47.     
  48. function onQueryFail(sender, args)
  49. {  
  50.    alert('Query failed. Error:' + args.get_message());  
  51. }  
Note

In the AppManifest.xml file provide QueryAsUserIgnoreAPPPrincipal permission to the Search.

QueryAsUserIgnoreAPPPrincipal

Summary

In this article we explored how to get the search results based on Content Type ID using JavaScript. I hope that the code above will be really useful for a SharePoint hosted app.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend