Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
SharePoint - Search: Code Snippet for using KeywordQuery in Server Object Model
WhatsApp
Prasham Sabadra
Feb 13
2015
3.7
k
0
0
//SPSite object is the parameter
using
(KeywordQuery keywordQuery =
new
KeywordQuery(site))
{
keywordQuery.QueryText =
string
.Format(
"contentclass:STS_WEB contentclass:STS_SITE"
);
var sourceID =contentSourceID;
//Properties which we want in result
keywordQuery.SelectProperties.Add(
"Title"
);
keywordQuery.SelectProperties.Add(
"Path"
);
//avoiding duplicate results
keywordQuery.TrimDuplicates =
false
;
//Sorting
keywordQuery.EnableSorting =
true
;
keywordQuery.SortList.Add(
"Title"
, SortDirection.Ascending);
var executor =
new
SearchExecutor();
using
(
new
SPMonitoredScope(
"Workspaces search query"
))
{
rtc = executor.ExecuteQuery(workspacesQuery);
}
queryResultsTable = rtc.Filter(
"TableType"
, KnownTableTypes.RelevantResults).First();
}
//Converting result table into DataTable
if
(queryResultsTable !=
null
&& queryResultsTable.RowCount > 0)
{
workspacesTable =
new
DataTable();
workspacesTable.Load(queryResultsTable, LoadOption.OverwriteChanges);
}
SharePoint
KeywordQuery
Server Object Model
Up Next
SharePoint - Search: Code Snippet for using KeywordQuery in Server Object Model