You can see the CRUD operations using REST here. In this article, we are going to discuss how to join two lists in SharePoint 2013 using REST.

I have two lists in the Office 365 SharePoint site.

  1. ListA
  2. ListB

ListA contains two columns -> Title and Code.

ListB contains four columns including Lookup column -> Title, EmployeeName, Salary, Dept (looks up to ListA).

Find the below screenshots of ListA and ListB.

SharePoint 2013

SharePoint 2013

Now, I want to join these two Lists and query the ListB.

The REST query will looks like the below.

https://server/Sites/SiteName/__api/web/lists/GetByTitle('ListB')/items?$Select=ListBField1,ListBField2 LookUpColumnInListB/ListAField,LookUpColumnInListB/ListAFiled&$expand=LookUpColumn

In our example, the REST query looks like the following after these setting values.

https://servername/Sites/SiteName/__api/web/lists/GetByTitle('ListB')/items?$Select=Title,EmployeeName,Salary,Dept/Title,Dept/Code&$expand=Dept

SharePoint 2013

SharePoint 2013

SharePoint 2013

Once you create the project, you will find default pages and scripts under Project Solution.

SharePoint 2013

On the default.aspx page, we will make some changes to display the data.

Write the below HTML code for User Interface under ContentPlaceHolderID tag.

  1. <table>
  2. <tr>
  3. <td>
  4. <table id="tblEmployees" class="mytable">
  5. </table>
  6. </td>
  7. </tr>
  8. </table>

SharePoint 2013

Method name - GetEmployeeDetails()

Call this method in Document.ready function.

  1. $(document).ready(function () {
  2. GetEmployeeDetails();
  3. });
  4. function GetEmployeeDetails() {
  5. $.ajax({
  6. url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/GetByTitle('" + listName + "')/items?$Select=Title,EmployeeName,Salary,Dept/Title,Dept/Code&$expand=Dept",
  7. type: "GET",
  8. headers: { "Accept": "application/json;odata=verbose" }, // return data format
  9. success: function (data) {
  10. var table = $("#tblEmployees");
  11. var html = "<thead><tr><th>Title</th><th>EmployeeName</th><th>Salary</th><th>DepartmentName</th><th>DepartmentCode</th></tr></thead>";
  12. for (var i = 0; i < data.d.results.length; i++) {
  13. var item = data.d.results[i];
  14. html += "<tr><td>" + item.Title + "</td><td>" + item.EmployeeName + "</td><td>" + item.Salary + "</td><td>" + item.Dept.Title + "</td><td>" + item.Dept.Code + "</td></tr>";
  15. }
  16. table.html(html);
  17. },
  18. error: function (error) {
  19. alert(JSON.stringify(error));
  20. }
  21. });
  22. }

Finally, App.js file looks like below.

  1. 'use strict';
  2. var listName = "ListB";
  3. ExecuteOrDelayUntilScriptLoaded(initializePage, "sp.js");
  4. function initializePage() {
  5. var context = SP.ClientContext.get_current();
  6. var user = context.get_web().get_currentUser();
  7. // This code runs when the DOM is ready and creates a context object which is needed to use the SharePoint object model
  8. $(document).ready(function () {
  9. GetEmployeeDetails();
  10. });
  11. function GetEmployeeDetails() {
  12. $.ajax({
  13. url: _spPageContextInfo.siteAbsoluteUrl + "/_api/web/lists/GetByTitle('" + listName + "')/items?$Select=Title,EmployeeName,Salary,Dept/Title,Dept/Code&$expand=Dept",
  14. type: "GET",
  15. headers: { "Accept": "application/json;odata=verbose" }, // return data format
  16. success: function (data) {
  17. //console.log(data.d.results);
  18. var table = $("#tblEmployees");
  19. var html = "<thead><tr><th>Title</th><th>EmployeeName</th><th>Salary</th><th>DepartmentName</th><th>DepartmentCode</th></tr></thead>";
  20. for (var i = 0; i < data.d.results.length; i++) {
  21. var item = data.d.results[i];
  22. //$("#tblEmployees").append(item.Title + "\t" + item.Salary + "\t" + item.Address + "<br/>");
  23. html += "<tr><td>" + item.Title + "</td><td>" + item.EmployeeName + "</td><td>" + item.Salary + "</td><td>" + item.Dept.Title + "</td><td>" + item.Dept.Code + "</td></tr>";
  24. }
  25. table.html(html);
  26. },
  27. error: function (error) {
  28. alert(JSON.stringify(error));
  29. }
  30. });
  31. }
  32. // This function prepares, loads, and then executes a SharePoint query to get the current users information
  33. function getUserName() {
  34. context.load(user);
  35. context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFail);
  36. }
  37. // This function is executed if the above call is successful
  38. // It replaces the contents of the 'message' element with the user name
  39. function onGetUserNameSuccess() {
  40. $('#message').text('Hello ' + user.get_title());
  41. }
  42. // This function is executed if the above call fails
  43. function onGetUserNameFail(sender, args) {
  44. alert('Failed to get user name. Error:' + args.get_message());
  45. }
  46. }
Before deploying the solution, give the appropriate permissions under AppManifest file.

SharePoint 2013

Try from your side and please let me know if you have any queries. I am attaching the code for your reference.