I have fixed the code example as there were missing some backslashes for escaping quotes in a string. Thanks to Gary Poirier for alerting me.
03.03.2012
Updated the solution to use spjs-utility.js for compatibility with newer versions of jQuery. See download link below.
24.04.2011
I have added a few lines to take into account the dialog’s in SP2010.
I have done a few articles on this subject earlier, but here is another method which is simpler to use. What it does is to modify the form action in NewForm to redirect you to DispForm or EditForm after the form has been submitted. You are redirected without the ID parameter in the URL. When in DispForm or EditForm, the lack of an ID parameter triggers a query to find the last item added to the list by the current user.
This code examples refers jQuery from Google’s site. All other code is kept in the CEWP.
Note:
This example uses the parameter “_spUserId” which is provided by SharePoint and represents the logged in user’s ID. This code will not work for anonymous users.
Learn how to insert CEWP on NewForm, DispForm or EditForm
Start by adding this code in a CEWP below NewForm.aspx:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script type="text/javascript"> function PreSaveAction(){ var URL = location.pathname.replace('NewForm.aspx','DispForm.aspx'); if(GetUrlKeyValue('IsDlg')==='1'){ URL+="?IsDlg=1"; } $("#aspnetForm").attr('action',location.pathname+"?Source="+URL); return true; } </script>
This code will send you to “DispForm” when submitting the form. To have it redirect you to EditForm, replace “DispForm” with EditForm” in line 05.
In DispForm.aspx (or EditForm.aspx), add this code in a CEWP below the form:
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script type="text/javascript" src="/YourLocalScriptRepository/spjs-utility.js"></script> <script type="text/javascript"> // List GUID or list display name of the current list var thisListGuid = '{A162F3E2-32D8-4CA2-B849-14879CDC5494}'; // This code runs only if the ID parameter is omitted if(GetUrlKeyValue('ID')===''){ var lastID = getLastItemID(); if(lastID!==''){ var newURL = location.pathname+"?ID="+lastID; if(GetUrlKeyValue('IsDlg')==='1'){ newURL+="&IsDlg=1"; } location.href=newURL; } } /******************************************** Do not modify anything below this line *********************************************/ function getLastItemID(){ var queryBuffer = []; queryBuffer.push("<Where><Eq><FieldRef Name='Author' LookupId='TRUE' /><Value Type='User'>"+_spUserId+"</Value></Eq></Where>"); queryBuffer.push("<OrderBy><FieldRef Name='ID' Ascending='FALSE' /></OrderBy>"); var res = spjs_QueryItems({listName:thisListGuid,query:queryBuffer.join(''),viewFields:['ID'],rowLimit:1}); if(res.count<0){ alert("An error occurred. Most likely the parameter \"thisListGuid\" is wrong."); }else if(res.count>0){ return res.items[0].ID; }else{ return ''; } } </script>
You find spjs-utility.js here. Ensure you get the latest version
If you prefer to use a local copy of jQuery, download it from here.
In line 05 you must provide the list GUID (recommended) or the display name of the current list. Learn how to find the list GUID
Ask if anything is unclear
Alexander