Get User By ID From Sharepoint List Using Javascript Object Model (JSOM)

Step 1: Create or open the list to retrieve the data from and note the list name (Here, it’s Test List).

Test List

Step 2: Open Visual Studio and create SharePoint Empty Project. Add Application page to it and add the script file path in PageHead . Add the code, given below, for retrieving the data by ID.

  1. <script type="text/javascript" src="/_layouts/15/init.js"></script>  
  2. <script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>  
  3. <script type="text/javascript" src="/_layouts/15/sp.js"></script>  
  4. <script type="text/javascript">  
  5.     var itemID = 2 // Item ID  
  6.     function GetUser() {  
  7.         var context = new SP.ClientContext.get_current();  
  8.         var targetList = context.get_web().get_lists().getByTitle('Test List'); //Ur List Name  
  9.         targetListtargetListItem = targetList.getItemById(itemID);  
  10.         context.load(targetListItem, 'Title');  
  11.         context.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));  
  12.     }  
  13.   
  14.     function onQuerySucceeded() {  
  15.         alert('Request succeeded. \n\nRetrieved Item is: ' + targetListItem.get_item('Title'));  
  16.     }  
  17.   
  18.     function onQueryFailed(sender, args) {  
  19.         alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());  
  20.     }  
  21. </script>  
code

Step 3: Add a button and write your function name in OnClientClick event for firing the code, given below:

code

Step 4: Open the Application page in the Browser and click the button for the retrieval of the detail from Test List (created above).

Test List

Step 5: See the results. Data is retrieved from the list, using its ID successfully.

 

results
Ebook Download
View all
Learn
View all