Jquery(Ajax) 调用 SharePoint 2013 Search Rest API 并使用Josn反回结果并简单显示
2013年01月05日 21:51:43 天涯海角 阅读数 4970 文章标签: Josn反回结果并简单显示JqueryAjaxPostQuerySearch Rest APISharePoint 2013 更多
分类专栏: SharePoint JavaScript
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/farawayplace613/article/details/8470834
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反回结果并简单显示的代码:
-
<!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" >
-
<head>
-
<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">
-
function StartSearch() {
-
resultDiv.style.dispaly = "none";
-
$("#resultTable").empty();
-
// 显示Loading data 图片
-
loadingDataDiv.style.display = "block";
-
// 在搜索之前先要取到X-RequestDigest, 并使用Ajax request的Header 传到服务器端,如果没有X-RequestDigest 会出现 403 错误
-
$.ajax(
-
{
-
url: "http://host/_api/contextinfo",
-
type: "Post",
-
dataType: "xml",
-
contentType: "text/xml; charset=\"utf-8\"",
-
complete: ProcessDigest
-
});
-
}
-
var loadingDataDiv;
-
var resultDiv;
-
$(document).ready(function () {
-
loadingDataDiv = $("#dataloadingDiv")[0];
-
resultDiv = $("#searchResultDiv")[0];
-
// 开始要隐藏 Loading data 图片
-
loadingDataDiv.style.display = "none";
-
resultDiv.style.dispaly = "none";
-
// 注意该句不加时,若Ajax跨站点访问会出现 “No Transport” error
-
jQuery.support.cors = true;
-
});
-
// 处理http://host/_api/contextinfo 返回的结果(提取X-RequestDigest 并发送搜索请求至 SharePoint Search Rest API)
-
function ProcessDigest(xData, status) {
-
if (xData.status == 200) {
-
// Use the $.parseXML throw out unsupported pseudo, so use the beetleheaded way
-
// var xmlDoc = $.parseXML(xData.responseText.replace("d:", ""));
-
// $xml = $(xmlDoc);
-
// var xRequestDigest = $xml.find("FormDigestValue").text();
-
var xRequestDigest = xData.responseText.SubStringBetween("<d:FormDigestValue>", "</d:FormDigestValue>");
-
// 取用户输入搜索字符串
-
var queryText = $("#SearchText")[0].value;
-
// 发送搜索请求至 SharePoint Search Rest API
-
$.ajax(
-
{
-
url: "http://host/_api/search/postquery",
-
type: "Post",
-
dataType: "application/json;odata=verbose",
-
data: JSON.stringify({
-
‘request‘: {
-
‘Querytext‘: queryText,
-
‘StartRow‘: 1,
-
‘RowLimit‘: 8,
-
‘SelectProperties‘: {
-
‘results‘: [‘Title‘, ‘ContentSource‘, ‘DisplayAuthor‘, ‘Path‘]
-
},
-
‘TrimDuplicates‘: true,
-
‘Refiners‘: ‘companies,contentclass,FileType(filter=6/0/*)‘,
-
‘RefinementFilters‘: { ‘results‘: [‘filetype:equals("docx")‘] }
-
}
-
}),
-
headers: {
-
"accept": "application/json;odata=verbose",
-
"content-type": "application/json;odata=verbose",
-
"X-RequestDigest": xRequestDigest
-
},
-
complete: ProcessSearchResult
-
});
-
}
-
else {
-
alert(status + xData.responseText);
-
loadingDataDiv.style.display = "none";
-
}
-
}
-
// 处理Search Rest API返回的数据,将其转换成 Josn对象并显示在表格中
-
function ProcessSearchResult(xData, status) {
-
if (xData.status == 200) {
-
// 隐藏 Loading data 图片
-
loadingDataDiv.style.display = "none";
-
// 将搜索结果转换成 Josn对象
-
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 () {
-
var title;
-
var contentSource;
-
var displayAuthor;
-
var path
-
$.each(this.Cells.results, function () {
-
if (this.Key == "Title")
-
title = this.Value;
-
if (this.Key == "ContentSource")
-
contentSource = this.Value;
-
if (this.Key == "DisplayAuthor")
-
displayAuthor = this.Value;
-
if (this.Key == "Path")
-
path = this.Value;
-
});
-
row = ‘<tr><td>‘ + title + ‘</td><td>‘ + contentSource + ‘</td><td>‘ + displayAuthor + ‘</td><td>‘ + path + ‘</td></tr>‘;
-
$(‘#resultTable‘).append(row);
-
});
-
// 显示搜索结果
-
resultDiv.style.dispaly = "block";
-
}
-
else {
-
alert(status + xData.responseText);
-
loadingDataDiv.style.display = "none";
-
}
-
}
-
// String 方法扩展, 由于$.ParseXML方法报错unsupported pseudo,所以采用分割字符串的笨方法来取FormDigestValue的值
-
String.prototype.SubStringBetween = function (prefix, suffix) {
-
var strArray = this.split(prefix);
-
var strArray1 = strArray[1].toString().split(suffix);
-
return strArray1[0];
-
};
-
</script>
-
</head>
-
<body>
-
<div id="inputDiv" >
-
<input id="SearchText" type="text"/>
-
<input id="Search" type="button" value="button" οnclick="StartSearch()" />
-
</div>
-
<div id="dataloadingDiv" >
-
<img src="Loading3.gif" />
-
</div>
-
<div id="searchResultDiv">
-
<table id="resultTable" border="1">
-
</table>
-
</div>
-
</body>
-
</html>
原文地址:https://www.cnblogs.com/xdanny/p/11456770.html