0

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.

1 Answer 1

3

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;
        }
1
  • Thanks, this helped me. I was missing using the Result Source.
    – Water
    Commented Feb 13, 2019 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.