Jquery(Ajax) 调用 SharePoint 2013 Search Rest API 并使用Josn反回结果并简单显示
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
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 功能:
本文将展示用Jquery(Ajax) 调用 SharePoint 2013 Search Rest API 并使用Josn反回结果并简单显示的代码:
-
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
-
<html xmlns="http://www.w3.org/1999/xhtml" >
-
-
<title>Untitled Page</title>
-
<script type="text/javascript" language="javascript" src="http://ajax.microsoft.com/ajax/jQuery/jquery-1.8.0.js"></script>
-
<script type="text/javascript" language="javascript">
-
-
resultDiv.style.dispaly = "none";
-
$("#resultTable").empty();
-
-
-
loadingDataDiv.style.display = "block";
-
-
-
-
-
url: "http://host/_api/contextinfo",
-
-
-
contentType: "text/xml; charset=\"utf-8\"",
-
-
-
-
-
-
-
$(document).ready(function () {
-
loadingDataDiv = $("#dataloadingDiv")[0];
-
resultDiv = $("#searchResultDiv")[0];
-
-
-
loadingDataDiv.style.display = "none";
-
resultDiv.style.dispaly = "none";
-
-
-
jQuery.support.cors = true;
-
-
-
-
function ProcessDigest(xData, status) {
-
if (xData.status == 200) {
-
-
-
-
-
-
-
var xRequestDigest = xData.responseText.SubStringBetween("<d:FormDigestValue>", "</d:FormDigestValue>");
-
-
-
var queryText = $("#SearchText")[0].value;
-
-
-
-
-
url: "http://host/_api/search/postquery",
-
-
dataType: "application/json;odata=verbose",
-
-
-
-
-
-
-
‘results‘: [‘Title‘, ‘ContentSource‘, ‘DisplayAuthor‘, ‘Path‘]
-
-
-
‘Refiners‘: ‘companies,contentclass,FileType(filter=6/0/*)‘,
-
‘RefinementFilters‘: { ‘results‘: [‘filetype:equals("docx")‘] }
-
-
-
-
"accept": "application/json;odata=verbose",
-
"content-type": "application/json;odata=verbose",
-
"X-RequestDigest": xRequestDigest
-
-
complete: ProcessSearchResult
-
-
-
-
-
-
alert(status + xData.responseText);
-
loadingDataDiv.style.display = "none";
-
-
-
-
-
function ProcessSearchResult(xData, status) {
-
if (xData.status == 200) {
-
-
-
loadingDataDiv.style.display = "none";
-
-
-
var josnData = $.parseJSON(xData.responseText);
-
-
-
$("#resultTable").empty();
-
var row = "<tr><td>Title</td><td>ContentSource</td><td>DisplayAuthor</td><td>Path</td></tr>";
-
$(‘#resultTable‘).append(row);
-
-
-
$.each(josnData.d.postquery.PrimaryQueryResult.RelevantResults.Table.Rows.results, function () {
-
-
-
-
-
-
$.each(this.Cells.results, function () {
-
-
-
-
if (this.Key == "ContentSource")
-
contentSource = this.Value;
-
-
if (this.Key == "DisplayAuthor")
-
displayAuthor = this.Value;
-
-
-
-
-
-
row = ‘<tr><td>‘ + title + ‘</td><td>‘ + contentSource + ‘</td><td>‘ + displayAuthor + ‘</td><td>‘ + path + ‘</td></tr>‘;
-
$(‘#resultTable‘).append(row);
-
-
-
-
resultDiv.style.dispaly = "block";
-
-
-
alert(status + xData.responseText);
-
loadingDataDiv.style.display = "none";
-
-
-
-
-
String.prototype.SubStringBetween = function (prefix, suffix) {
-
var strArray = this.split(prefix);
-
var strArray1 = strArray[1].toString().split(suffix);
-
-
-
-
-
-
-
<input id="SearchText" type="text"/>
-
<input id="Search" type="button" value="button" οnclick="StartSearch()" />
-
-
-
<div id="dataloadingDiv" >
-
<img src="Loading3.gif" />
-
-
<div id="searchResultDiv">
-
<table id="resultTable" border="1">
-
-
-
-
-