Example #1
0
        public virtual void OnSelecting(SearchDataSourceSelectingEventArgs args)
        {
            var handler = Events[_selecting] as EventHandler <SearchDataSourceSelectingEventArgs>;

            if (handler != null)
            {
                handler(this, args);
            }
        }
        protected override IEnumerable ExecuteSelect(DataSourceSelectArguments args)
        {
            args.AddSupportedCapabilities(DataSourceCapabilities.Page);
            args.AddSupportedCapabilities(DataSourceCapabilities.RetrieveTotalRowCount);

            args.RaiseUnsupportedCapabilitiesError(this);

            var parameters = Owner.SelectParameters.GetValues(HttpContext.Current, Owner);

            var filter       = GetFilter(args, parameters);
            var logicalNames = GetLogicalNames(args, parameters);
            var pageSize     = GetPageSize(args, parameters);
            var pageNumber   = GetPageNumber(args, parameters, pageSize);
            var queryText    = GetQueryText(args, parameters);
            ContextLanguageInfo contextLanguage;
            var multiLanguageEnabled = TryGetLanguageCode(out contextLanguage);

            if (string.IsNullOrWhiteSpace(queryText) && string.IsNullOrWhiteSpace(filter))
            {
                args.TotalRowCount = 0;

                return(new CrmEntitySearchResultPage(new List <ICrmEntitySearchResult>(), 0, pageNumber, pageSize));
            }

            var provider = SearchManager.GetProvider(Owner.SearchProvider);
            var query    = new CrmEntityQuery(queryText, pageNumber, pageSize, logicalNames, contextLanguage.ContextLanguage, multiLanguageEnabled, filter);

            var selectingArgs = new SearchDataSourceSelectingEventArgs(provider, query);

            Owner.OnSelecting(selectingArgs);

            if (selectingArgs.Cancel)
            {
                args.TotalRowCount = 0;

                return(new CrmEntitySearchResultPage(new List <ICrmEntitySearchResult>(), 0, pageNumber, pageSize));
            }

            try
            {
                using (var searcher = provider.GetIndexSearcher())
                {
                    var results = searcher.Search(query);

                    args.TotalRowCount = results.ApproximateTotalHits;

                    Owner.OnSelected(new SearchDataSourceStatusEventArgs(provider, query, results));

                    return(results);
                }
            }
            catch (Exception e)
            {
                ADXTrace.Instance.TraceError(TraceCategory.Application, e.ToString());

                var selectedArgs = new SearchDataSourceStatusEventArgs(provider, query, new CrmEntitySearchResultPage(new List <ICrmEntitySearchResult>(), 0, pageNumber, pageSize))
                {
                    Exception = e
                };

                Owner.OnSelected(selectedArgs);

                if (!selectedArgs.ExceptionHandled)
                {
                    throw;
                }

                args.TotalRowCount = 0;

                return(new CrmEntitySearchResultPage(new List <ICrmEntitySearchResult>(), 0, pageNumber, pageSize));
            }
        }