SharePoint JSOM KeywordQuery更改了它正在搜索的ContentType

时间:2015-09-25 16:31:29

标签: angularjs sharepoint sharepoint-2013 sharepoint-jsom

我目前正在使用AngularJS和SharePoint JSOM对结果源执行一些关键字查询。

我有一个接受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的值,如果我没有指定结果源名称,它会返回结果。

当我将相同的确切变量传递给它时,有什么可能导致这种情况的想法?

1 个答案:

答案 0 :(得分:0)

不知道为什么会这样,但删除了

        resultSourceQuery.get_properties().set_item('SourceName', resultSourceName);
        resultSourceQuery.get_properties().set_item('SourceLevel', resultSourceLevel);
来自init方法的

并将其添加到getData方法修复了该问题。不知道为什么它会起作用,因为如果调用代码使用getData方法,那么应始终在ready方法之前设置它。

如果有人知道为什么修复它,请指出它。与此同时,我的问题已经解决。