People search using CSOM API
-
29-09-2020 - |
Question
I am using CSOM in API. I used following code for content search in documents and now want to integrate People search which can search users from sharepoint website using CSOM.
MethodResponse<ClientResult<ResultTableCollection>> response = new MethodResponse<ClientResult<ResultTableCollection>>();
UserCredential userCredential = new UserCredential();
try
{
userCredential = VerifyToken(token);
if (userCredential != null)
{
_ctx = GetClientContext(userCredential);
if (_ctx != null)
{
KeywordQuery keywordQuery = new KeywordQuery(_ctx);
keywordQuery.QueryText = Convert.ToString(text);
SearchExecutor searchExecutor = new SearchExecutor(_ctx);
ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);
_ctx.ExecuteQuery();
response.ErrorCode = "200";
response.ErrorDescription = "No Error";
response.Response = results;
}
}
}
I tried searching for this but in vain. Please help.
Solution
I have solved it myself , indeed may help someone..
public MethodResponse> GetPeoples(string text, string token) { MethodResponse> response = new MethodResponse>(); List lstMetadata = new List(); UserCredential userCredential = new UserCredential(); try { userCredential = VerifyToken(token); if (userCredential != null) { _ctx = GetClientContext(userCredential); if (_ctx != null) { KeywordQuery keywordQuery = new KeywordQuery(_ctx); keywordQuery.QueryText = text; // keywordQuery.HiddenConstraints = "scope:\"People\""; keywordQuery.TrimDuplicates = true; keywordQuery.SourceId = Guid.Parse("B09A7990-05EA-4AF9-81EF-EDFAB16C4E31"); SearchExecutor searchExecutor = new SearchExecutor(_ctx); ClientResult results = searchExecutor.ExecuteQuery(keywordQuery); _ctx.ExecuteQuery(); if (results.Value[0].RowCount > 0) { foreach (var resultRow in results.Value[0].ResultRows) { lstMetadata.Add(resultRow["PreferredName"].ToString()); } response.ErrorCode = "200"; response.ErrorDescription = "No error."; response.Response = lstMetadata; } else { response.ErrorCode = "400"; response.ErrorDescription = "No users found."; response.Response = lstMetadata; } } } } catch (Exception ex) { response.ErrorCode = "400"; response.ErrorDescription = ex.Message; response.Response = null; } finally { _ctx.Dispose(); } return response; }
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange