Condividi tramite


SP.List.getItemById(id) Method

Applies to: SharePoint Foundation 2010

Returns the list item with the specified list item identifier.

var value = SP.List.getItemById(id);

Parameters

  • id
    Specifies the list item identifier.

Type: Int32

Return Value

Type: SP.ListItem

Exceptions

  • [System.ArgumentException]
    List item does not exist. Error code: -2147024809.
  • [System.UnauthorizedAccessException]
    The current user has insufficient permissions. Error code: -2147024891.

Applies To

SP.List Class

Example

The following example creates an input button on an application page that gets a list item with a specified identifier.

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<script type="text/ecmascript" language="ecmascript">

   //Get the list item from the Announcements list whose Id is 4. Note that this is the ID of the item in the list, not a reference to its position in the collection.
   var itemId = 4;   
   var targetListItem;

   function runCode() {
     var clientContext = new SP.ClientContext(); 
     var targetList = clientContext.get_web().get_lists().getByTitle('Announcements');
     targetListItem = targetList.getItemById(itemId);
     clientContext.load(targetListItem, 'Title');
     clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
   }

   function onQuerySucceeded() {
   
       alert('Request succeeded. \n\nRetrieved Item is: ' + targetListItem.get_item('Title'));
    
   }

   function onQueryFailed(sender, args) {
     alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
   }

</script>

    <input id="Button1" type="button" value="Run Code" onclick="runCode()" />

</asp:Content>

See Also

Reference

SP.List Methods

SP.List Properties

SP Namespace