/// <summary> /// Executes a keyword query against Office search service. /// </summary> /// <param name="keywordQuery">A keyword query instance.</param> /// <param name="refiners">A list of <see cref="SearchRefiner"/> objects where refinement results are populated.</param> /// <returns>Results returned from Office search service.</returns> public static ResultTable ExecuteQuery(KeywordQuery keywordQuery, SearchRefiner[] refiners) { CommonHelper.ConfirmNotNull(keywordQuery, "keywordQuery"); LastQueryText = keywordQuery.QueryText; if (refiners != null) { keywordQuery.Refiners = String.Join(",", refiners.Select(v => v.PropertyName).ToArray()); keywordQuery.RefinementFilters.AddRange(refiners.Where(v => v.RefinementToken != null).Select(v => v.RefinementToken).ToArray()); } SearchExecutor executor = new SearchExecutor(); ResultTableCollection queryResults = executor.ExecuteQuery(keywordQuery); ResultTable relevantResults = queryResults.Filter("TableType", KnownTableTypes.RelevantResults).FirstOrDefault(); if (relevantResults == null) { throw new Exception("Search executor did not return result table of type RelevantResults"); } if (refiners != null) { ResultTable refinementResults = queryResults.Filter("TableType", KnownTableTypes.RefinementResults).FirstOrDefault(); if (refinementResults == null) { throw new Exception("Search executor did not return result table of type RefinementResults"); } foreach (SearchRefiner refiner in refiners) { foreach (DataRow row in refinementResults.Table.Rows.OfType <DataRow>().Where(v => refiner.PropertyName.Equals(v["RefinerName"]))) { refiner.AddRefinement((string)row["RefinementName"], (string)row["RefinementToken"], (int)row["RefinementCount"]); } } } return(relevantResults); }
protected override void ProcessRecord() { base.ProcessRecord(); try { KeywordQuery query = new KeywordQuery(this.Site.Read()); query.QueryText = this.Query; query.SelectProperties.AddRange(this.Select); SearchExecutor executor = new SearchExecutor(); ResultTableCollection result = executor.ExecuteQuery(query); ResultTable resultTable = result.Filter("TableType", KnownTableTypes.RelevantResults).FirstOrDefault(); if (resultTable == null) { throw new Exception("Search executor did not return result table of type RelevantResults"); } DataTable dataTable = new DataTable(); dataTable.Load(resultTable); foreach (DataRow row in dataTable.Rows) { PSObject obj = new PSObject(); foreach (DataColumn column in dataTable.Columns) { obj.Members.Add(new PSNoteProperty(column.Caption, row[column])); } WriteObject(obj); } } catch (Exception ex) { ThrowTerminatingError(ex, ErrorCategory.NotSpecified); } }
public List <Event> GetEvents() { List <Event> events; using (new SPMonitoredScope("Boxing Events Search Retrieval")) { KeywordQuery query = new KeywordQuery(SPContext.Current.Site); query.SelectProperties.AddRange(new[] { "Title", "EventDate", "Path" }); query.QueryText = "ContentType:\"CT-Event\""; var searchExecutor = new SearchExecutor(); ResultTableCollection results = searchExecutor.ExecuteQuery(query); ResultTable resultTable = results.Filter("TableType", KnownTableTypes.RelevantResults).Single(); events = (from DataRow row in resultTable.ResultRows select new Event( (string)row["Title"], (DateTime)row["EventDate"], (string)row["Path"]) ).OrderByDescending(ev => ev.EventDate) .ThenBy(ev => ev.Title) .ToList(); } return(events); }
//按时间显示开始的十条 /// <summary> /// 1为博文,2为汇总 /// </summary> /// <param name="queryState"></param> /// <returns></returns> private DataTable FullQuery(int queryState) { DataTable queryDataTable = null; SPSecurity.RunWithElevatedPrivileges(delegate() { SPServiceContext context = SPServiceContext.Current;// ServerContext.Current;//ServerContext.GetContext SearchServiceApplicationProxy ssap = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(SPContext.Current.Site)); using (KeywordQuery qry = new KeywordQuery(ssap)) { qry.EnableStemming = true; qry.TrimDuplicates = true; if (queryState == 1) { qry.RowLimit = totalCount; } else { qry.RowLimit = 5000; } string queryText = "-author:系统帐户 -author:administrator ContentClass:STS_ListItem_Posts"; if (queryState > 1) { double dValue = -30; //if (queryState == 3) // dValue = -60; queryText = queryText + " Created:" + DateTime.Now.AddDays(dValue).ToString("yyyy-MM-dd") + ".." + DateTime.Now.ToString("yyyy-MM-dd"); } qry.QueryText = queryText;// qry.SelectProperties.AddRange(new string[] { "WorkId", "Title", "Author", "AuthorOWSUSER", "Created", "Path", "EditorOWSUSER", "HitHighlightedSummary", "ParentLink" }); qry.SortList.Add("Created", Microsoft.Office.Server.Search.Query.SortDirection.Descending); SearchExecutor searchExecutor = new SearchExecutor(); ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(qry); IEnumerator <ResultTable> iResult = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults).GetEnumerator(); iResult.MoveNext(); ResultTable resultTable = iResult.Current; queryDataTable = resultTable.Table; foreach (DataRow dr in queryDataTable.Rows) { string author = dr["Author"].ToString(); if (author.IndexOf(";") > 0)//多个作者 { dr["Author"] = author.Substring(0, author.IndexOf(";")); } } queryDataTable.AcceptChanges(); } }); return(queryDataTable); }
private DataTable FullQuery(string txtFrom, string txtTo) { SPServiceContext context = SPServiceContext.Current; SearchServiceApplicationProxy ssap = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(SPContext.Current.Site)); using (KeywordQuery qry = new KeywordQuery(ssap)) { qry.EnableStemming = true; qry.TrimDuplicates = true; qry.RowLimit = 10000; string queryText = GetFullQueryString(); string txtTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); txtTime = txtTime.Substring(txtTime.IndexOf(" ")); qry.QueryText = "Created:" + txtFrom + ".." + txtTo + " " + queryText; qry.SelectProperties.AddRange(new string[] { "WorkId", "Title", "Author", "Created", "Path", "ContentClass", "FileExtension" }); qry.SortList.Add("Created", Microsoft.Office.Server.Search.Query.SortDirection.Descending); SearchExecutor searchExecutor = new SearchExecutor(); ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(qry); IEnumerator <ResultTable> iResult = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults).GetEnumerator(); iResult.MoveNext(); ResultTable resultTable = iResult.Current; DataTable queryDataTable = resultTable.Table; if (queryDataTable.Rows.Count > 0) { foreach (DataRow dr in queryDataTable.Rows) { //小时加8 dr["Created"] = ((DateTime)dr["Created"]).AddHours(8); string author = dr["Author"].ToString(); if (author.IndexOf(";") > 0)//多个作者,修改者也加到了里面 { dr["Author"] = author.Substring(0, author.IndexOf(";")); } } queryDataTable.AcceptChanges(); //当天查询减去24小时 if (dateFrom.SelectedDate == DateTime.Today.AddDays(-1) && DateTo.SelectedDate == DateTime.Today) { DataRow[] drs = queryDataTable.Select("Created>='" + txtFrom + txtTime + "' and Created<='" + DateTo.SelectedDate.ToString("yyyy-MM-dd") + txtTime + "'", "Created desc"); DataSet ds = new DataSet(); DataTable dt = queryDataTable.Clone(); ds.Tables.Add(dt); ds.Merge(drs); queryDataTable = ds.Tables[0]; } } return(queryDataTable); } }
//查询结果 private int FullQuery(string txtFrom, string txtTo) { SPServiceContext context = SPServiceContext.Current;// ServerContext.Current;//ServerContext.GetContext SearchServiceApplicationProxy ssap = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(SPContext.Current.Site)); using (KeywordQuery qry = new KeywordQuery(ssap)) { qry.EnableStemming = true; qry.TrimDuplicates = true; qry.RowLimit = 10000; string queryText = "ContentClass:STS_ListItem_Posts"; //queryText = "-Author:系统帐户 -Author:administrator "; qry.QueryText = queryText + " Created:" + txtFrom + ".." + txtTo; qry.SelectProperties.AddRange(new string[] { "WorkId", "Title", "Author", "Created", "Path", "ContentClass", "FileExtension", "EditorOWSUSER", "HitHighlightedSummary", "ParentLink" }); qry.SortList.Add("Created", Microsoft.Office.Server.Search.Query.SortDirection.Descending); SearchExecutor searchExecutor = new SearchExecutor(); ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(qry); IEnumerator <ResultTable> iResult = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults).GetEnumerator(); iResult.MoveNext(); ResultTable resultTable = iResult.Current; DataTable queryDataTable = resultTable.Table; Response.Clear(); for (int i = 0; i < queryDataTable.Columns.Count; i++) { //Response.Write(i.ToString() + "--name:" + queryDataTable.Columns[i].ColumnName + "--value:" + queryDataTable.Rows[0][i].ToString() + "<br>"); } for (int i = 0; i < queryDataTable.Rows.Count; i++) { for (int j = 0; j < queryDataTable.Columns.Count; j++) { string colName = queryDataTable.Columns[j].ColumnName; Response.Write(colName + ":" + queryDataTable.Rows[i][colName].ToString() + "<br>"); } } //for (int i = 0; i < queryDataTable.Rows.Count; i++) //{ // //Response.Write(i.ToString() + "<br>"); // Response.Write("Author:" + "--value:" + queryDataTable.Rows[i]["Author"].ToString() + "<br>"); // Response.Write("EditorOWSUSER:"******"--value:" + queryDataTable.Rows[i]["EditorOWSUSER"].ToString() + "<br>"); // Response.Write("ParentLink:" + "--value:" + queryDataTable.Rows[i]["ParentLink"].ToString() + "<br>"); // //Response.Write("DocId:" + "--value:" + queryDataTable.Rows[i]["DocId"].ToString() + "<br>"); // //Response.Write("EditorOWSUSER:"******"--value:" + queryDataTable.Rows[i]["EditorOWSUSER"].ToString() + "<br>"); // //Response.Write("Title:" + "--value:" + queryDataTable.Rows[i]["Title"].ToString() + "<br>"); //} return(queryDataTable.Rows.Count); } }
static void Main(string[] args) { using (SPSite siteCollection = new SPSite("http://sp2010/sites/test")) { KeywordQuery keywordQuery = new KeywordQuery(siteCollection); keywordQuery.QueryText = "SharePoint"; keywordQuery.SortList.Add("Author", SortDirection.Ascending); keywordQuery.SortList.Add("Size", SortDirection.Descending); SearchExecutor searchExecutor = new SearchExecutor(); ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(keywordQuery); var resultTables = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults); var resultTable = resultTables.FirstOrDefault(); DataTable dataTable = resultTable.Table; } }
//按时间显示开始的十条 /// <summary> /// 1为博文,2为汇总 /// </summary> /// <param name="queryState"></param> /// <returns></returns> private DataTable FullQuery(int queryState) { DataTable queryDataTable = null; SPSecurity.RunWithElevatedPrivileges(delegate() { SPServiceContext context = SPServiceContext.Current;// ServerContext.Current;//ServerContext.GetContext SearchServiceApplicationProxy ssap = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(SPContext.Current.Site)); using (KeywordQuery qry = new KeywordQuery(ssap)) { qry.EnableStemming = true; qry.TrimDuplicates = true; qry.RowLimit = TotalCount; string titleQue = ""; if (CourseKeyword.Trim().Length > 0) { string[] titles = CourseKeyword.Split(';'); foreach (string titel in titles) { titleQue = titleQue + " title:\"" + titel + "\""; } } string queryText = "ContentClass:STS_ListItem_Posts" + titleQue; SPUser spUser = SPContext.Current.Web.CurrentUser; if (LoginUser == 1 && spUser != null) { queryText = "author:" + spUser.Name + " " + queryText; } ////this.Controls.Add(new LiteralControl(queryText)); qry.QueryText = queryText;// qry.SelectProperties.AddRange(new string[] { "WorkId", "Title", "Author", "Created", "Path", "EditorOWSUSER", "HitHighlightedSummary", "ParentLink" }); qry.SortList.Add("Created", Microsoft.Office.Server.Search.Query.SortDirection.Descending); SearchExecutor searchExecutor = new SearchExecutor(); ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(qry); IEnumerator <ResultTable> iResult = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults).GetEnumerator(); iResult.MoveNext(); ResultTable resultTable = iResult.Current; queryDataTable = resultTable.Table; } }); return(queryDataTable); }
private int FullQuery(string txtFrom, string txtTo) { SPServiceContext context = SPServiceContext.Current;// ServerContext.Current;//ServerContext.GetContext SearchServiceApplicationProxy ssap = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(SPContext.Current.Site)); using (KeywordQuery qry = new KeywordQuery(ssap)) { qry.EnableStemming = true; qry.TrimDuplicates = true; qry.RowLimit = 10000; string queryText = ""; //queryText = "-Author:系统帐户 -Author:administrator "; qry.QueryText = queryText + "Created:" + txtFrom + ".." + txtTo; qry.SelectProperties.AddRange(new string[] { "WorkId", "Title", "Author", "Created", "Path", "ContentClass", "FileExtension" }); qry.SortList.Add("Created", Microsoft.Office.Server.Search.Query.SortDirection.Descending); SearchExecutor searchExecutor = new SearchExecutor(); ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(qry); IEnumerator <ResultTable> iResult = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults).GetEnumerator(); iResult.MoveNext(); ResultTable resultTable = iResult.Current; DataTable queryDataTable = resultTable.Table; return(queryDataTable.Rows.Count); } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { SetPageTitles("Tìm kiếm"); SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(SPContext.Current.Site.Url)) { using (SPWeb web = site.OpenWeb()) { string k = Request.QueryString["k"]; KeywordQuery keywordQuery = new KeywordQuery(web); keywordQuery.QueryText = k; keywordQuery.QueryText += "(contentclass:STS_List OR contentclass:STS_List_DocumentLibrary) CustomeURL:.aspx"; //keywordQuery.QueryText = "scope:\"All Sites\" AND (contentclass:\"STS_Site\" OR contentclass:\"STS_Web\")"; keywordQuery.StartRow = 0; keywordQuery.RowLimit = 500; //keywordQuery.SourceId = PeopleSearch; keywordQuery.EnableNicknames = true; keywordQuery.EnablePhonetic = true; keywordQuery.TrimDuplicates = false; //which columns should be returned in the query result. keywordQuery.SelectProperties.Add("title"); keywordQuery.SelectProperties.Add("CustomeURL"); keywordQuery.SelectProperties.Add("ImageUrl"); SearchExecutor searchExecutor = new SearchExecutor(); ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(keywordQuery); var resultTables = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults); if (resultTables != null && resultTables.Any()) { var resultTable = resultTables.FirstOrDefault(); if (resultTable != null) { DataTable dataTable = resultTable.Table; //rpResult.DataSource = dataTable; //rpResult.DataBind(); BindDataIntoRepeater(dataTable); table = dataTable; } else { table = new DataTable(); } } else { table = new DataTable(); } } } }); } }
private void FullQuery(string txtFrom, string txtTo) { SPServiceContext context = SPServiceContext.Current;// ServerContext.Current;//ServerContext.GetContext SearchServiceApplicationProxy ssap = (SearchServiceApplicationProxy)SearchServiceApplicationProxy.GetProxy(SPServiceContext.GetContext(SPContext.Current.Site)); using (KeywordQuery qry = new KeywordQuery(ssap)) { qry.EnableStemming = true; qry.TrimDuplicates = true; qry.RowLimit = 500; string queryText = ""; //获取id和显示名称 if (userID.ResolvedEntities.Count > 0) { string name = ((PickerEntity)userID.ResolvedEntities[0]).DisplayText; queryText = "Author:" + name + " ";; // userID.CommaSeparatedAccounts.Replace(";", "") + " "; } else { queryText = "-Author:系统帐户 -Author:administrator "; } qry.QueryText = queryText + "Created:" + txtFrom + ".." + txtTo; qry.SelectProperties.AddRange(new string[] { "WorkId", "Title", "Author", "Created", "Path", "ContentClass", "FileExtension" }); qry.SortList.Add("Created", Microsoft.Office.Server.Search.Query.SortDirection.Descending); SearchExecutor searchExecutor = new SearchExecutor(); ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(qry); IEnumerator <ResultTable> iResult = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults).GetEnumerator(); iResult.MoveNext(); ResultTable resultTable = iResult.Current; DataTable queryDataTable = resultTable.Table; DataTable distinctTable = queryDataTable.Clone(); foreach (DataRow dr in queryDataTable.Rows) { //小时加8 dr["Created"] = ((DateTime)dr["Created"]).AddHours(8); switch (dr["ContentClass"].ToString()) { case "STS_ListItem_TasksWithTimelineAndHierarchy": dr["ContentClass"] = "任务"; break; case "STS_ListItem_GenericList": dr["ContentClass"] = "自定义列表"; break; case "STS_ListItem_Posts": dr["ContentClass"] = "博客文章"; break; case "STS_ListItem_851": //Asset Library / Video Channel dr["ContentClass"] = "资产库"; break; case "STS_ListItem_DocumentLibrary": dr["ContentClass"] = "文档库"; break; case "STS_ListItem_MySiteDocumentLibrary": dr["ContentClass"] = "个人网站文档库"; break; case "STS_Site": dr["ContentClass"] = "网站集"; break; case "STS_ListItem_Announcements": dr["ContentClass"] = "通知新闻"; break; case "STS_ListItem_PictureLibrary": dr["ContentClass"] = "图片库"; break; case "STS_ListItem_Comments": dr["ContentClass"] = "博客评论"; break; case "STS_ListItem_Categories": dr["ContentClass"] = "博客类别"; break; case "STS_Web": dr["ContentClass"] = "网站"; break; case "STS_ListItem_544": //MicroBlogList (MicroFeed) dr["ContentClass"] = "新闻源"; break; case "STS_ListItem_Survey": dr["ContentClass"] = "调查"; break; } string author = dr["Author"].ToString(); if (author.IndexOf(";") > 0) //多个作者 { dr["Author"] = author.Substring(0, author.IndexOf(";")); } string url = dr["Path"].ToString(); try { dr["Path"] = url.Substring(url.IndexOf("/", 7)); } catch { } DataRow[] drs = distinctTable.Select("Path='" + dr["Path"] + "'"); if (drs.Length == 0) { distinctTable.Rows.Add(dr.ItemArray); } } queryDataTable.AcceptChanges(); _gvtable = distinctTable.Copy(); errlable.Text = "本次查询共 " + _gvtable.Rows.Count + " 条数据!"; } }
public List <Record> DoSharePointSearch(GDPRSubject search) { GDPRCore.Current.Log("Starting SharePoint Search"); List <Record> records = new List <Record>(); try { using (SPSite siteCollection = GetFirstSPSite()) { KeywordQuery keywordQuery = new KeywordQuery(siteCollection); SearchExecutor searchExecutor = new SearchExecutor(); foreach (GDPRSubjectEmail se in search.EmailAddresses) { keywordQuery.QueryText = se.EmailAddress; ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(keywordQuery); ResultTable resultTable = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults).FirstOrDefault(); DataTable dataTable = resultTable.Table; if (dataTable.Rows.Count > 0) { foreach (DataRow dr in dataTable.Rows) { Record r = GetSearchRecord(dr); if (r != null) { records.Add(r); } } } } foreach (GDPRSubjectPhone se in search.Phones) { keywordQuery.QueryText = se.Raw; ResultTableCollection resultTableCollection = searchExecutor.ExecuteQuery(keywordQuery); ResultTable resultTable = resultTableCollection.Filter("TableType", KnownTableTypes.RelevantResults).FirstOrDefault(); DataTable dataTable = resultTable.Table; if (dataTable.Rows.Count > 0) { foreach (DataRow dr in dataTable.Rows) { Record r = GetSearchRecord(dr); if (r != null) { records.Add(r); } } } } } } catch (Exception ex) { //GDPRCore.Current.Log(ex, LogLevel.Error); } return(records); }