Jquery(Ajax) 調用 SharePoint 2013 Search Rest API 並使用Josn反回結果並簡單顯示
版權聲明:本文爲博主原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處連接和本聲明。
Jquery(Ajax) 調用 SharePoint 2013 Search Rest API 並使用Josn反回結果並簡單顯示:javascript
SharePoint 2010 中使用的 search.asmxSOAPwebservice在SharePoint 2013中已經被標記爲過時, 但爲了和老版本的Solution兼容仍是能夠用的。html
而替代search.asmx的技術爲SharePoint 2013 Search Rest API:java
http://blogs.msdn.com/b/nadeemis/archive/2012/08/24/sharepoint-2013-search-rest-api.aspxjquery
主要用於三方系統須要集成SharePoint 的 Enterprise Search 功能:web
本文將展現用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">
-
-
-
-
-