我正在使用AngularJS和SharePoint JSOM對結果源執行一些關鍵字查詢。SharePoint JSOM關鍵字查詢正在搜索的ContentType
我有一個角工廠接受ResultSourceName和ResultSourceLevel變量
angular.module('app.services.sharepoint').factory('resultSourceDataSvc', ['$log', '$q', '$timeout', function ($log, $q, $timeout) {
return function (resultSourceName, resultSourceLevel) {
return new ResultSourceDataService($log, $q, $timeout, resultSourceName, resultSourceLevel);
}
}]);
這被注入到我的控制器和他們使用它像這樣:
var inTheNewsResultDataSource = resultSourceData('TSToday In The News Result Source', 'SPSite');
inTheNewsResultDataSource.ready(function() {
inTheNewsResultDataSource.getData(null, IN_THE_NEWS_COLUMNS, IN_THE_NEWS_MAP_COLUMNS)
.then(function (data) {
$log.debug('Resolving data:', data);
deferred.resolve(data);
},
function (resp) {
$log.error('Error while attempting to retrieve In The News data:', resp);
deferred.reject(resp);
});
});
它的實際肉所有都在我的工廠返回的ResultSourceDataService中處理,這裏是它的代碼:
function ResultSourceDataService($log, $q, $timeout, resultSourceName, resultSourceLevel) {
var deferred = $q.defer(),
promise = deferred.promise,
context = undefined,
resultSourceQuery = undefined,
resultSourceExecutor = undefined,
properties = undefined;
$log = $log.getInstance('app.services.sharepoint.resultSourceDataSvc(' + resultSourceName + ')');
function init() {
$log.debug('Initializing new Result Source Data Service for:', resultSourceName, 'on', resultSourceLevel);
SP.SOD.registerSod('sp.search.js', '/_layouts/15/sp.search.js');
SP.SOD.loadMultiple(['sp.js', 'sp.search.js'], function() {
context = SP.ClientContext ? SP.ClientContext.get_current() : undefined;
// should probably add error checking to make sure context is loaded here
resultSourceQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(context);
resultSourceExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(context);
resultSourceQuery.set_trimDuplicates(false);
resultSourceQuery.get_properties().set_item('SourceName', resultSourceName);
resultSourceQuery.get_properties().set_item('SourceLevel', resultSourceLevel);
$log.debug('Initialized');
deferred.resolve();
});
}
init();
//#region Private Functions
function setSelectProperties(columnsToRetrieve) {
properties = resultSourceQuery.get_selectProperties();
for (var i = columnsToRetrieve.length; i-- > 0;) {
$log.debug('Adding in property to retrieve', columnsToRetrieve[i]);
properties.add(columnsToRetrieve[i]);
}
}
function getResults(data, columnsToRetrieve, columnsToMap) {
return data.map(function (obj, i) {
var result = columnsToRetrieve.length == 0 ? obj : {};
for (var j = columnsToRetrieve.length; j-- > 0;) {
result[columnsToMap[j]] = obj[columnsToRetrieve[j]];
}
$log.debug('getResult', result);
return result;
});
}
//#endregion
//#region Public Functions
this.getData = function (keyword, columnsToRetrieve, columnsToMap) {
var deferred = $q.defer(),
promise = deferred.promise,
data = undefined,
results = [];
keyword = keyword || '*';
columnsToRetrieve = columnsToRetrieve || [];
columnsToMap = columnsToMap || columnsToRetrieve;
resultSourceQuery.set_queryText(keyword);
setSelectProperties(columnsToRetrieve);
$log.info('Attempting to retrieve data from result source with keyword:', keyword, resultSourceQuery);
data = resultSourceExecutor.executeQuery(resultSourceQuery);
context.executeQueryAsync(
function (sender, args) {
$log.debug('Data retrieved successfully', data.get_value().ResultTables);
results = getResults(data.get_value().ResultTables[0].ResultRows, columnsToRetrieve, columnsToMap);
$log.info('Resolving data', results);
deferred.resolve(results);
},
function (sender, args) {
$log.error('Error with resolving data in ResultSourceDataService', args.get_message());
deferred.reject(args.get_message());
});
return promise;
}
this.ready = function (fn) {
promise.then(fn);
}
//#endregion
}
當我加載運行這段代碼的頁面時,它運行正常。當我刷新頁面時,它運行良好。但是,如果我從我的網站中的任何其他頁面導航到此頁面,它會中斷並返回數據,就好像沒有指定結果源一樣。
出於某種原因,QueryModification(如果我是正確的,本質上告訴它它在查詢中查找的ContentType)正在改變。當我最初加載頁面並查看記錄到控制檯的數據時($log.debug('Data retrieved successfully', data.get_value().ResultTables);
),我可以深入瞭解並查看QueryModification中的差異。
當它正常運行時(刷新頁面或直接從站點外部導航到頁面),我看到值爲ContentTypeId:0x0100A9723709C6D74A77B01EA922C770FDD0*
,這是正確的。然而;如果我從SharePoint內部導航到頁面,則會看到值爲* -ContentClass=urn:content-class:SPSPeople
因此,如果我沒有指定「結果源名稱」,則返回的結果將顯示出來。
有什麼想法可能會導致這種情況,當我傳遞相同的確切變量嗎?