Level: Beginning
Prerequisite: JSAPI User Authentication
Example: People Search Example
Summary:This tutorial covers the basic JSAPI people search call, options for configuration, how to work with the returned object and present it in the browser. When you've completed the tutorial, you will have all the information you need to present people search information in your web application.
Note that all code samples in this document use jQuery, so that's the syntax being used.
The framework is loaded with the instruction to let the user stay logged in:
And the authentication handler goes on the login button:
A <div> should be created where the output can be injected when it returns.
The basic people search call returns a set of people objects. When no parameters or fields are sent, this result is essentially a random collection of people, ordered by popularity.
The IN.API.PeopleSearch() method always returns a set of people.
Additional behaviors can be chained on as additional methods using the "." format. So in the example above, the method is called with a ".result" function to tell the framework what to do when the result is returned. The .result method can be used with an anonymous function as it is above, or can be given a reference to a different function to call when the results are returned.
Methods which can be chained on general JSAPI methods:
The PeopleSearch() method is fairly useless in its default state - the goal of this method is actually to allow the application to search the LinkedIn graph for people matching a particular filter. Using the .params() method, you can send additional parameters to the backend REST API call. As an example, let's ask for people who match the keyword "python", restricted to 10 results, and sorted based on their connection distance from the logged in user.
The output in this case will be similar to the above, but the people will be restricted to ones that match the filters.Documentation on what parameters are meaningful for this call are on the People Search API page.
The call given above returns the default fields for a People Search call, which are "headline","id","lastName","pictureUrl","firstName".
You can request any valid field or fields in a people search call, however.
The fields available for a connections request are documented on the Profile Fields page in the general REST API documentation. Note that that page presents the field names in a dashed manner ("first-name") while the JavaScript API expects them in a studly caps manner ("firstName") so you'll need to translate the fields you want to make them work. Also pay attention to the restrictions of the fields that can only be used for the logged in user versus connections or people outside their network.
If you check the output for the calls above, you'll see what the returned objects look like. All we have done up until now is print the string version of the object in the browser, but processing those objects is relatively simple.
People Search Call
The results are returned as JavaScript objects, which can be operated on using standard JavaScript notation. Whether a single ID or multiple IDs are requested, the results are returned as a list, so you need to access the value you need by looping through the results or directly referencing the index: profile = result.values[0];
You can access each of the fields you requested (or the default fields) to build the HTML you want to inject into your page, and then inject it like any other AJAX content.