5
votes

I have a list within Sharepoint, using a custom new form I have added a custom list form control ("New item form" for the list) and changed the SaveButton to a standard input HTML button, and added an 'onclick' event that is as follows:

onclick="javascript: {ddwrt:GenFireServerEvent('__commit;__redirect={NewFormWizard2.aspx?id=}')}"

This works as in saves the data and redirects to the NewFormWizard2.aspx?id= page.

How do I get the ID of the created item to be passed to the redirected page?

Thus once the form is completed it would redirect to NewFormWizard2.aspx?id=23

6

6 Answers

2
votes

jtherkel was close, but was missing a '}' on the end of the redirect url. I used an extra concat below

<input type="button" value="Submit" name="btnSave" onclick="javascript: {ddwrt:GenFireServerEvent(concat('__commit;__redirect={lists/MyListName/DispForm.aspx?ID=',/dsQueryResponse/Rows/Row/@ID,'}'))}" />
0
votes

I am not sure where the ID will exist on the page you host the Javascript from. Does it appear in the querystring or on a field on the page?

There is nothing in the request or response that will identify the item. I have had this issue when doing some web loadtesting.

I can only suggest that your create the item using the webservices as that at gives you some return xml.

0
votes

This answer does not solve the "new form" issue, but it might help others with the syntax for screens that contain existing list items.

I tested this quickly in my SharePoint (MOSS 2007) environment.

onclick="javascript: {ddwrt:GenFireServerEvent(concat('__commit;__redirect={NewFormWizard2.aspx?id=',/dsQueryResponse/Rows/Row/@ID))}"

The concat syntax is an XSLT instruction that tells the processor to combine the values enclosed in single quotes. I adapted this answer from info I found here.

Loading Values in a Custom List Form http://wssdevelopment.blogspot.com/2007_04_01_archive.html

0
votes

I hope this would be helpfull: 1- In SharePoint Designer create new page, call it for example "LastItem.aspx" and place a dataview on it with a single form view for the destination list item. 2-Limit paging to just one record, set the sorting by ID and descending and filter the list to just show item which is created by [current user]. 3-Now you do not need to pass any query string to this page. just replace the default "OK" button in NewForm.aspx of the list with a standard HTML input button and add this to its definition "onclick="javascript: {ddwrt:GenFireServerEvent(concat('__commit;__redirect={LastItem.aspx}". After submitting a new item to list you will be redirected to an edit view of the created item. You can do the same for save button in LastItem.aspx to redirect to some other page after clicking on save button.

0
votes

found an approach using pure javascript (JQuery) and the SPAPI code from http://darrenjohnstone.net/.

The list contains two fields, title and BodyCopy

I've thewn created a form that asks for a title and a question, both text fields, then the submit button calls the following function: (note that ServerAddress and LIST_question need to be updated to your own details).

The function then uploads the details using the SOAP service within LISTS.ASMX and using the response gets the ID of the new item and redirects the page.

var LIST_question = '{xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}';
var ServerAddress = 'http://xxx/';

function submitQuestion()
    {
    var title = new String($("#title").val());
    var t = new String($("#question").val());
    t=t.trim();
    if(t=="")
        return;

    title=title.trim();
    if(title=="")
        return;


    var lists = new SPAPI_Lists(ServerAddress) ;
    // 
    var newItem = { Title :  title, BodyCopy : t};
    var items = lists.quickAddListItem(LIST_question, newItem); 
    var id=-1; 
    if (items.status == 200) 
    { 
        var rows = items.responseXML.getElementsByTagName('z:row');
        if(rows.length ==1)
        {
            var r = rows[0];
            var id = r.getAttribute('ows_ID'); 
            window.location.href='DispForm.aspx?ID='+id;

        }
        else
        {
            alert("Error: No row added");
        }


    } 
    else
    { 

        alert('There was an error: ' + items.statusText); 
        return;
    }
}