Function

$().SPServices.SPGetLastItemId

Certification

Certified for SharePoint 2007 Certified for SharePoint 2010

Functionality

Function to return the ID of the last item created on a list by a specific user. Useful for maintaining parent/child relationships. This function was built for use by the $().SPServices.SPRedirectWithID function, but is also useful in other circumstances.

Prerequisites

None

Syntax

$().SPServices.SPGetLastItemId({	
	webURL: "",
	listName: "",
	userAccount: "",
	CAMLQuery: ""
});

webURL
The URL of the Web (site) which contains the listName. If not specified, the current site is used. Examples would be: "/", "/Accounting", "/Departments/HR", etc. Note: It's always best to use relative URLs.

listName
The name or GUID of the list. If you choose to use the GUID, it should look like: "{E73FEA09-CF8F-4B30-88C7-6FA996EE1706}". Note also that if you use the GUID, you do not need to specify the relationshipWebURL if the list is in another site.

userAccount
The account for the user in DOMAIN\username format. If not specified, the current user is used.

CAMLQuery
The CAMLQuery option allows you to specify an additional filter on the relationshipList. The additional filter will be <And>ed with the existing CAML which is checking for matching items based on the parentColumn selection. Bacause it is combined with the CAML required to make the function work, CAMLQuery here should contain a CAML fragment such as:

CAMLQuery: "<Eq><FieldRef Name='Status'/><Value Type='Text'>Active</Value></Eq>"

Example

The following example will return the most recently created item's ID for the current user from the States list in the current site.

<script language="javascript" type="text/javascript" src="../../jQuery%20Libraries/jquery-1.4.1.min.js"></script>
<script language="javascript" type="text/javascript" src="../../jQuery%20Libraries/jquery.SPServices-0.5.1.min.js"></script>
<script language="javascript" type="text/javascript">
  var lastId = $().SPServices.SPGetLastItemId({ 
    listName: "States"
  }); 
</script>

Last edited Feb 25, 2011 at 10:45 PM by sympmarc, version 12

Comments

premsshrestha Jun 11, 2012 at 7:14 PM 
The last returned ID is always 0 now for some reason. It was working and all of sudden nor working...

premsshrestha Jun 11, 2012 at 7:13 PM 
It STOPPED working for me today...

Anyone facing similar issue?? Thanks.

sympmarc Oct 29, 2010 at 3:19 PM 
It's built to work for a specific user. However, the highest current ID number would tell you which item was most recently created. Pretty simple to do with jQuery and GetListItems.

variable Oct 28, 2010 at 5:48 PM 
Can I use this to get the last item ID on a list by ANY user? Thanks.