JSAPI Tutorial: People Search

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.

Setup

First, authentication needs to be set up as explained in JSAPI User Authentication.

Setup Details

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.


Basic People Search Call

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.

Output

You'll get a large number of person objects back, one for each connection.

Details about the Basic Call


Adding Parameters

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.

Details about Parameters

Documentation on what parameters are meaningful for this call are on the People Search API page.


Selecting Fields

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.

Output


Details about Fields

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.


Processing the Return Object

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

Results


Details about Processing Results

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.



Example Code