Redirect from NewForm.aspx to DispForm.aspx or EditForm.aspx of newly created item

You may need to redirect from NewForm.aspx to DispForm.aspx (or other page) of newly created item when creating SharePointsolutions like Invoices and Orders management. There are a couple of blogs out there that already have a solution for this. Because at the moment of item creation there is no item ID yet all we can do is to redirect to other page where we can query SharePoint list using web services for the last created item by current user. Then we can just redirect to wanted page using queried item ID.

I can’t remember already where the idea came from first so I just write down how I’m doing this in my SharePoint solutions. This time I’ll going to use only SPAPI no need to use jQuery or SharePoint Designer.

  1. Edit NewForm.aspx by adding CEWP to it with following code:
    // This function is executed before save in SharePoint new and edit forms
    function PreSaveAction() {
        var srcUrl = GetSource();
    
        var pathparts = document.location.pathname.split('/');
    
        //you may need to modify this depending on where you site is located
        var url = 'http://' + document.location.host + '/' + pathparts[1] + '/';
    
        var newSrcUrl = url + "Custom Pages/NewFormRedirect.aspx";
        var i = aspnetForm.action.lastIndexOf(srcUrl);
    
        //changing Source parameter before form submit. SharePoint will redirect to new Source parameter provided by us after form submit.
        aspnetForm.action = aspnetForm.action.substring(0, i) + newSrcUrl;
    
        return true;
    }
  2. Create document library called Custom Pages.
  3. Create Web Part page called NewFormRedirect.aspx with full page vertical layout in Custom Pages library.
  4. Add CEWP toNewFormRedirect.aspx with the following code in it:
    //Set up SPAPI (in my case I have installed SPAPI in Layouts directory)
    <script src="/_layouts/SPAPI/SPAPI_Core.js" type="text/javascript"></script>
    <script src="/_layouts/SPAPI/SPAPI_Lists.js" type="text/javascript"></script>
    <script type="text/javascript">
    
    //You may need do modify this function depending where your site collection is located
    function GetRootUrl() {
        var pathparts = document.location.pathname.split('/');
        var url = 'http://' + document.location.host + '/' + pathparts[1] + '/';
        return url;
    }
    
    var lists = new SPAPI_Lists(GetRootUrl())
    
    //quering last created item by current user
    //i'm using _spUserId to get current user
    var items = lists.getListItems(
    		'Orders',   // listName
    		'',         // viewName
    		'<Query><OrderBy><FieldRef Name="Modified" Ascending="FALSE"></FieldRef></OrderBy><Where><Eq><FieldRef Name="Author" LookupId="TRUE"/><Value Type="Lookup">' + _spUserId + '</Value></Eq></Where></Query>',  // query
    		'<ViewFields><FieldRef Name="ID"/></ViewFields>',  // viewFields
    		'1',  // rowLimit
    		'<QueryOptions><IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns></QueryOptions>'  // queryOptions
    	);
    
    if (items.status == 200) {
        var rows = items.responseXML.getElementsByTagName('z:row');
        var lastID = rows[0].getAttribute('ows_ID');
    
        //redirect to wanted page using quered last item ID
        //here i'm redirecting to DispForm.aspx of newly cerated item
        var url = GetRootUrl() + "Lists/Orders/DispForm.aspx?ID=" + lastID + "&Source=%2FLists%2FOrders%2FAllItems%2Easpx"
        document.location.href = url;
    }
    </script>
  5. Also check out Mark’s solution of doing this with the help of SPServices library.

Posted in Customization, javascript
7 comments on “Redirect from NewForm.aspx to DispForm.aspx or EditForm.aspx of newly created item
  1. HI If your just finding his last ebtry says:

    This could of been done by a view much simpler….

    But bloody cool

  2. Good says:

    Nice post….

  3. Ty says:

    Seems to works, superb & easy to implement

  4. in says:

    Thanks for the script but I see you referred getsource() in the 1st script and I cannot find that any where? what is that function?

  5. admin says:

    get source should return “source” parameter value from query string

  6. Mike says:

    I received a ViewState error: Validation of viewstate MAC failed.

    • Mike says:

      Never mind. In 2013 I used the PreSaveAction feature to redirect the submit location to the Custom Pages/NewFormRedirect.aspx.

      On the NewFormRedirect.aspx I used the GetItemList webservice to get the last ID created and then redirected the user to the EditForm.aspx?ID=XXX.

      Seems to work well, has some holes in it with multiple users possibly creating documents at the same time, but with so little users actually inputting data I figured I’d take the risk.
      (Cost versus Benefits)

Subcribe to news