Jquery(Ajax) 调用 SharePoint 2013 Search Rest API 并使用Josn反回结果并简单显示

Jquery(Ajax) 调用 SharePoint 2013 Search Rest API 并使用Josn反回结果并简单显示

Jquery(Ajax) 调用 SharePoint 2013 Search Rest API 并使用Josn反回结果并简单显示:

 

SharePoint 2010 中使用的 search.asmxSOAPwebservice在SharePoint 2013中已经被标记为过期, 但为了和老版本的Solution兼容还是可以用的。

而替代search.asmx的技术为SharePoint 2013 Search Rest API:

http://blogs.msdn.com/b/nadeemis/archive/2012/08/24/sharepoint-2013-search-rest-api.aspx

主要用于三方系统需要集成SharePoint 的 Enterprise Search 功能:

 

query

http://host/site/_api/search/query

使用Get方式取得查询结果,URL长度有限制

postquery

http://host/site/_api/search/postquery

使用Post方式取得查询结果,可以克服URL长度有限制

suggest

http://host/site/_api/search/suggest

使用Get 方式取得search suggestion(搜索建议)

 

本文将展示用Jquery(Ajax) 调用 SharePoint 2013 Search Rest API 并使用Josn反回结果并简单显示的代码:

 

  1.  
  2.  
    <html xmlns="http://www.w3.org/1999/xhtml" >
  3.  
    <head>
  4.  
    <title>Untitled Pagetitle>
  5.  
    <script type="text/javascript" language="javascript" src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.8.0.js">script>
  6.  
    <script type="text/javascript" language="javascript">
  7.  
    function StartSearch() {
  8.  
    resultDiv.style.dispaly = "none";
  9.  
    $( "#resultTable").empty();
  10.  
     
  11.  
    // 显示Loading data 图片
  12.  
    loadingDataDiv.style.display = "block";
  13.  
     
  14.  
    // 在搜索之前先要取到X-RequestDigest, 并使用Ajax request的Header 传到服务器端,如果没有X-RequestDigest 会出现 403 错误
  15.  
    $.ajax(
  16.  
    {
  17.  
    url: "http://host/_api/contextinfo",
  18.  
    type: "Post",
  19.  
    dataType: "xml",
  20.  
    contentType: "text/xml; charset=\"utf-8\"",
  21.  
    complete: ProcessDigest
  22.  
    });
  23.  
    }
  24.  
     
  25.  
    var loadingDataDiv;
  26.  
    var resultDiv;
  27.  
    $( document).ready(function () {
  28.  
    loadingDataDiv = $( "#dataloadingDiv")[0];
  29.  
    resultDiv = $( "#searchResultDiv")[0];
  30.  
     
  31.  
    // 开始要隐藏 Loading data 图片
  32.  
    loadingDataDiv.style.display = "none";
  33.  
    resultDiv.style.dispaly = "none";
  34.  
     
  35.  
    // 注意该句不加时,若Ajax跨站点访问会出现 “No Transport” error
  36.  
    jQuery.support.cors = true;
  37.  
    });
  38.  
     
  39.  
    // 处理http://host/_api/contextinfo 返回的结果(提取X-RequestDigest 并发送搜索请求至 SharePoint Search Rest API)
  40.  
    function ProcessDigest(xData, status) {
  41.  
    if (xData.status == 200) {
  42.  
     
  43.  
    // Use the $.parseXML throw out unsupported pseudo, so use the beetleheaded way
  44.  
    // var xmlDoc = $.parseXML(xData.responseText.replace("d:", ""));
  45.  
    // $xml = $(xmlDoc);
  46.  
    // var xRequestDigest = $xml.find("FormDigestValue").text();
  47.  
     
  48.  
    var xRequestDigest = xData.responseText.SubStringBetween("", "");
  49.  
     
  50.  
    // 取用户输入搜索字符串
  51.  
    var queryText = $("#SearchText")[0].value;
  52.  
     
  53.  
    // 发送搜索请求至 SharePoint Search Rest API
  54.  
    $.ajax(
  55.  
    {
  56.  
    url: "http://host/_api/search/postquery",
  57.  
    type: "Post",
  58.  
    dataType: "application/json;odata=verbose",
  59.  
    data: JSON.stringify({
  60.  
    'request': {
  61.  
    'Querytext': queryText,
  62.  
    'StartRow': 1,
  63.  
    'RowLimit': 8,
  64.  
    'SelectProperties': {
  65.  
    'results': ['Title', 'ContentSource', 'DisplayAuthor', 'Path']
  66.  
    },
  67.  
    'TrimDuplicates': true,
  68.  
    'Refiners': 'companies,contentclass,FileType(filter=6/0/*)',
  69.  
    'RefinementFilters': { 'results': ['filetype:equals("docx")'] }
  70.  
    }
  71.  
    }),
  72.  
    headers: {
  73.  
    "accept": "application/json;odata=verbose",
  74.  
    "content-type": "application/json;odata=verbose",
  75.  
    "X-RequestDigest": xRequestDigest
  76.  
    },
  77.  
    complete: ProcessSearchResult
  78.  
    });
  79.  
     
  80.  
    }
  81.  
    else {
  82.  
     
  83.  
    alert(status + xData.responseText);
  84.  
    loadingDataDiv.style.display = "none";
  85.  
    }
  86.  
    }
  87.  
     
  88.  
    // 处理Search Rest API返回的数据,将其转换成 Josn对象并显示在表格中
  89.  
    function ProcessSearchResult(xData, status) {
  90.  
    if (xData.status == 200) {
  91.  
     
  92.  
    // 隐藏 Loading data 图片
  93.  
    loadingDataDiv.style.display = "none";
  94.  
     
  95.  
    // 将搜索结果转换成 Josn对象
  96.  
    var josnData = $.parseJSON(xData.responseText);
  97.  
     
  98.  
    // 清空表格内容
  99.  
    $( "#resultTable").empty();
  100.  
    var row = "TitleContentSourceDisplayAuthorPath";
  101.  
    $( '#resultTable').append(row);
  102.  
     
  103.  
    // 遍历搜索结果并逐条插入表格
  104.  
    $.each(josnData.d.postquery.PrimaryQueryResult.RelevantResults.Table.Rows.results, function () {
  105.  
     
  106.  
    var title;
  107.  
    var contentSource;
  108.  
    var displayAuthor;
  109.  
    var path
  110.  
    $.each( this.Cells.results, function () {
  111.  
    if (this.Key == "Title")
  112.  
    title = this.Value;
  113.  
     
  114.  
    if (this.Key == "ContentSource")
  115.  
    contentSource = this.Value;
  116.  
     
  117.  
    if (this.Key == "DisplayAuthor")
  118.  
    displayAuthor = this.Value;
  119.  
     
  120.  
    if (this.Key == "Path")
  121.  
    path = this.Value;
  122.  
    });
  123.  
     
  124.  
    row = '' + title + '' + contentSource + '' + displayAuthor + '' + path + '';
  125.  
    $( '#resultTable').append(row);
  126.  
    });
  127.  
     
  128.  
    // 显示搜索结果
  129.  
    resultDiv.style.dispaly = "block";
  130.  
    }
  131.  
    else {
  132.  
    alert(status + xData.responseText);
  133.  
    loadingDataDiv.style.display = "none";
  134.  
    }
  135.  
    }
  136.  
     
  137.  
    // String 方法扩展, 由于$.ParseXML方法报错unsupported pseudo,所以采用分割字符串的笨方法来取FormDigestValue的值
  138.  
    String.prototype.SubStringBetween = function (prefix, suffix) {
  139.  
    var strArray = this.split(prefix);
  140.  
    var strArray1 = strArray[1].toString().split(suffix);
  141.  
    return strArray1[0];
  142.  
    };
  143.  
    script>
  144.  
    head>
  145.  
    <body>
  146.  
    <div id="inputDiv" >
  147.  
    <input id="SearchText" type="text"/>
  148.  
    <input id="Search" type="button" value="button" οnclick="StartSearch()" />
  149.  
    div>
  150.  
     
  151.  
    <div id="dataloadingDiv" >
  152.  
    <img src="Loading3.gif" />
  153.  
    div>
  154.  
    <div id="searchResultDiv">
  155.  
    <table id="resultTable" border="1">
  156.  
     
  157.  
    table>
  158.  
    div>
  159.  
    body>
  160.  
    html>


 

转载于:https://www.cnblogs.com/xdanny/p/11456770.html

你可能感兴趣的:(Josn反回结果并简单显示,JqueryAjax,PostQuery,Search,Rest,API,SharePoint,2013,SharePoint,JavaScript)