private RESTSearchResults ExecuteRESTRequest(string RequestUri) { var res = string.Empty; HttpWebRequest request = null; RESTSearchResults searchResults = null; //List<T> items = new List<T>(); //string accessToken = Configuration.OAuthToken; try { request = (HttpWebRequest)WebRequest.Create(RequestUri); request.Method = "GET"; request.Accept = "application/json"; // request.Expect = "100-continue"; request.Headers.Add("Accept-Encoding", "gzip, deflate"); if (serviceBroker.Service.ServiceConfiguration.ServiceAuthentication.AuthenticationMode == AuthenticationMode.Impersonate || serviceBroker.Service.ServiceConfiguration.ServiceAuthentication.AuthenticationMode == AuthenticationMode.ServiceAccount) { request.UseDefaultCredentials = true; } if (serviceBroker.Service.ServiceConfiguration.ServiceAuthentication.AuthenticationMode == AuthenticationMode.OAuth) { string accessToken = serviceBroker.Service.ServiceConfiguration.ServiceAuthentication.OAuthToken; string headerBearer = String.Format(CultureInfo.InvariantCulture, "Bearer {0}", accessToken); request.Headers.Add("Authorization", headerBearer.ToString()); } if (serviceBroker.Service.ServiceConfiguration.ServiceAuthentication.AuthenticationMode == AuthenticationMode.Static) { request.Credentials = GetCredentials();//unlikely to work for office 365 } request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; using (HttpWebResponse Response = (HttpWebResponse)request.GetResponse()) { using (Stream st = Response.GetResponseStream()) { using (StreamReader sr = new StreamReader(st)) { res = sr.ReadToEnd(); } searchResults = Newtonsoft.Json.JsonConvert.DeserializeObject <RESTSearchResults>(res); } } } catch (WebException wex) { // should throw exception to force reauth throw; } catch (Exception ex) { throw; } finally { request = null; } return(searchResults); }
public static RESTSearchResultsSerialized ProcessResults(RESTSearchResults res, SearchInputs inputs) { RESTSearchResultsSerialized SerializedResults = new RESTSearchResultsSerialized(); SerializedResults.Inputs = inputs; if (res != null) { SerializedResults.ExecutionTime = res.ElapsedTime; if (res.PrimaryQueryResult != null && res.PrimaryQueryResult.RelevantResults != null) { SerializedResults.TotalRows = res.PrimaryQueryResult.RelevantResults.TotalRows; SerializedResults.ResultRows = res.PrimaryQueryResult.RelevantResults.RowCount; SerializedResults.ResultTitle = res.PrimaryQueryResult.RelevantResults.ResultTitle; SerializedResults.SearchResults = res.PrimaryQueryResult.RelevantResults.Table; SerializedResults.ResultTitleUrl = res.PrimaryQueryResult.RelevantResults.ResultTitleUrl; } else { SerializedResults.TotalRows = 0; SerializedResults.ResultRows = 0; } SerializedResults.SpellingSuggestions = res.SpellingSuggestion; // set SourceId from execution results Guid sid = Guid.Empty; SearchProperty SourceId = res.Properties.Where(p => p.Key.Equals("sourceid", StringComparison.InvariantCultureIgnoreCase)).First(); if (SourceId != null && Guid.TryParse(SourceId.Value, out sid)) { SerializedResults.Inputs.SourceId = sid; } } return SerializedResults; }
public RESTSearchResultsSerialized ExecuteSharePointSearch(Property[] inputs, RequiredProperties required, Property[] returns, MethodType methodType, ServiceObject serviceObject) { serviceObject.Properties.InitResultTable(); ClientResult <ResultTableCollection> results = null; SearchInputs SearchInputs = GetInputs(inputs); RESTSearchResultsSerialized SerializedResults = new RESTSearchResultsSerialized(); SerializedResults.Inputs = SearchInputs; //KeywordQuery keywordQuery = new KeywordQuery(cc); //keywordQuery.QueryText = SearchInputs.Search; //SearchExecutor searchExecutor = new SearchExecutor(cc); if (SearchInputs.StartRow.HasValue && SearchInputs.StartRow.Value > -1) { //keywordQuery.StartRow = SearchInputs.StartRow.Value; } //keywordQuery.RowsPerPage = int.Parse(txtRowsPerPage.Text); if (SearchInputs.RowLimit.HasValue && SearchInputs.RowLimit.Value > -1) { //keywordQuery.RowLimit = SearchInputs.RowLimit.Value; } //keywordQuery.Culture = Configuration.LocaleId; if (SearchInputs.SourceId != null && SearchInputs.SourceId != Guid.Empty) { //keywordQuery.SourceId = SearchInputs.SourceId; } if (SearchInputs.Sort.Count > 0) { } if (SearchInputs.EnableNicknames.HasValue && SearchInputs.EnableNicknames.Value) { //keywordQuery.EnableNicknames = SearchInputs.EnableNicknames.Value; } if (SearchInputs.EnablePhonetic.HasValue && SearchInputs.EnablePhonetic.Value) { //keywordQuery.EnablePhonetic = SearchInputs.EnablePhonetic.Value; } // updated for inputs RESTSearchResults res = ExecuteRESTRequest(BuildSearchText(SearchInputs)); if (res != null) { int executiontime = res.ElapsedTime; int totalresults = res.PrimaryQueryResult.RelevantResults.TotalRows; int resultrows = res.PrimaryQueryResult.RelevantResults.RowCount; SerializedResults.ResultTitle = res.PrimaryQueryResult.RelevantResults.ResultTitle; SerializedResults.ResultTitleUrl = res.PrimaryQueryResult.RelevantResults.ResultTitleUrl; SerializedResults.SpellingSuggestions = res.SpellingSuggestion; SerializedResults.SearchResults = res.PrimaryQueryResult.RelevantResults.Table; // set SourceId from execution results Guid sid = Guid.Empty; SearchProperty SourceId = res.Properties.Where(p => p.Key.Equals("sourceid", StringComparison.InvariantCultureIgnoreCase)).First(); if (SourceId != null && Guid.TryParse(SourceId.Value, out sid)) { SerializedResults.Inputs.SourceId = sid; } } return(SerializedResults); }