none
How to search only People using javascript (sp.search.js)?

    Question

  • My code:

    $("#searchButton").click(function () {       

    var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(context);       

    keywordQuery.set_queryText($("#searchTextBox").val());       

    var searchExecutor = new Microsoft.SharePoint.Client.Search.Query.SearchExecutor(context);

    results = searchExecutor.executeQuery(keywordQuery);       

    context.executeQueryAsync(onQuerySuccess, onQueryError);   

    });       

    function onQuerySuccess() {       

    $("#resultsDiv").append('<table>');       

    $.each(results.m_value.ResultTables[0].ResultRows, function () {     

    $("#resultsDiv").append('<tr>');           

    $("#resultsDiv").append('<td>' + this.Author + '</td>');           

    $("#resultsDiv").append('<td>' + this.Title + '</td>');           

    $("#resultsDiv").append('</tr>');       

    });       

    $("#resultsDiv").append('</table>');   

    }


    But this code actually find all results. How to filter to show only People results?


    Monday, September 23, 2013 8:14 PM

Answers

All replies

  • Set the keywordQuery's sourceId property to the local people results:

    keywordQuery.set_sourceid = "B09A7990-05EA-4AF9-81EF-EDFAB16C4E31";


    Blog |SharePoint Field Notes Dev Tool | SPFastDeploy

    • Marked as answer by João S. Silva Tuesday, September 24, 2013 8:02 PM
    Tuesday, September 24, 2013 2:40 AM
  • Thank you!

    My new code:

    var keywordQuery = new Microsoft.SharePoint.Client.Search.Query.KeywordQuery(context);
            keywordQuery.set_sourceid = "B09A7990-05EA-4AF9-81EF-EDFAB16C4E31";
            keywordQuery.set_queryText($("#searchTextBox").val());

    But the results are the same. What im doing wrong?

    I need to get only the user 'joao.silva' on my search result, but im getting all documents writen by 'joao.silva'

    Tuesday, September 24, 2013 7:13 PM
  • Thank you Steven. I found the error!!

    To set source id

    right way => keywordQuery.set_sourceId("B09A7990-05EA-4AF9-81EF-EDFAB16C4E31");

    • Marked as answer by João S. Silva Tuesday, September 24, 2013 8:02 PM
    Tuesday, September 24, 2013 8:01 PM
  • Hi Steve,

    I'm working on a similar scenario, but i got access denied error, when executing query async.

    I'm calling the code in a separate asp.net web site outside of SharePoint itself.

    I'm guessing i should add my credential to the ClientContext?

    Any comment is welcomed.

    Thanks in advance,

    ZZ

    Tuesday, October 08, 2013 5:40 AM
  • I have search the people in sharepoint online by using following code. It's work fine.I hope it's use full for some one

    var targetSite = new Uri("siteURL");
                var login = "username";\\same as your email
                var password = "password";
                var securePassword = new SecureString();
                foreach (char c in password)
                {
                    securePassword.AppendChar(c);
                }
                var onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
                using (ClientContext clientContext = new ClientContext(targetSite ))
                {
                    clientContext.Credentials = onlineCredentials;
                    KeywordQuery keywordQuery = new KeywordQuery(clientContext);
                    keywordQuery.QueryText = "Query text";
                    keywordQuery.SourceId = Guid.Parse ("B09A7990-05EA-4AF9-81EF-EDFAB16C4E31");
                    SearchExecutor searchExecutor = new SearchExecutor(clientContext);
                    ClientResult<ResultTableCollection> results = searchExecutor.ExecuteQuery(keywordQuery);
                    clientContext.ExecuteQuery();
                    foreach (var resultRow in results.Value[0].ResultRows)
                    {
                        string test = resultRow["PreferredName"].ToString();
                    }
                }


    Ravin Singh D

    Thursday, January 09, 2014 2:13 PM