8

What is the best way to add redirect function into the save button on a newform in SharePoint 2013.

When the user create a list item, he/she should be redirected to another form. Is it possible to get and parse the new generated item ID?

Start - Newform URL: http://mysharepoint.com/sites/project/lists/alist/NewForm.aspx

End - Editform URL: http://mysharepoint.com/sites/project/lists/alist/EditForm.aspx?ID=88

Thanks

1
  • I agree with Danny. Doesn't recommend by clicking edit button and edit item on the datasheet view. This is my own view.
    – Sara
    Apr 7, 2017 at 15:24

4 Answers 4

9

Add the URL of the next form as the &Source=[url] parameter

The Source parameter in the URL is where the browser is redirected to after a Form is saved. Open an EditForm and investigate the URL The URL has to be URL encoded (and it needs to be within the SharePoint domain)

https://xxx.sharepoint.com/sites/VM/Lists/Tasks/NewForm.aspx
?Source=
 https%3A%2F%2Fxxx.sharepoint.com%2Fsites%2FVM%2FLists%2FTasks%2FAllItems%2Easpx
&RootFolder=

So if you replace that Source with URL of a NewForm

https%3A%2F%2Fxxx.sharepoint.com%2Fsites%2FVM%2FLists%2FTasks%2FNewForm.aspx

You can add an item and then be returned to a Blank NewForm

On that new form you will have to add code to retrieve the last added ID

You can do it with JavaScript code or put a hidden ListView WebPart on the NewForm page that lists the last created item and then use some script to extract what you need and update the fields in the Form

... takes just a bit of scripting

4
  • Thank you Danny, could you provide more details? Aug 20, 2015 at 7:00
  • 1
    It's working in custom list perfectly. But in survey list it works in SharePoint on-premise but it fails in Office 365 - SharePoint Online. Please suggest some workaround for that. Sep 26, 2016 at 7:30
  • in sharepoint online, it fails!! i second dikesh's argument/comment. i am also facing the same issue.the source parameter is alaways is allitems.aspx or i am unable to add a query string parameter in the url before redirecting
    – samolpp2
    Oct 22, 2018 at 9:01
  • Note the given answer is over 3 years old. Most likely Microsoft changed behaviour; as they did with many other ways of hacking into SharePoint/ Oct 23, 2018 at 10:54
1

Danny gave a great answer already, but in case someone is looking to do this within a SharePoint-Hosted App, you can create a custom button within Default.aspx with this code:

<asp:HyperLink CssClass="menuLink" runat="server" NavigateUrl="JavaScript:window.location = _spPageContextInfo.webAbsoluteUrl + 
            '/Lists/ListName/NewForm.aspx?Source=' + _spPageContextInfo.webAbsoluteUrl + '';" Text="Add New List Item" />

This button will bring the user to the NewForm.aspx and after they save, it will bring them back to Default.aspx in the App / Add-in Web

0

I racked my brain trying to figure out how to both save and redirect to another page and after almost a week of failed attempts I discovered this page on msdn that reveals the different properties of the SharePoint:SaveButton. https://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.savebutton_members.aspx

After tinkering around for a bit I discovered that by using RedirectURL I could both save and redirect to another page. See code below:

<SharePoint:SaveButton  RedirectUrl="http://mysitecollection/Pages/Thank-You.aspx" runat="server" ControlMode="Edit" id="savebutton2"/>

I only tested this on SP2013 on premise. It only seemed to work when I used a url that was in my site collection(and only after I used http://mysitecollection/Pages/Thank-You.aspx rather than ../../Thank-You.aspx), but that's what I needed it for anyways so it solved my problem. Perhaps there's something else that needs to be done to redirect to an external page, but otherwise this was a breakthrough for me.

Hope that helps others that come across this page.

0

This worked for me. Give it a try.

function PreSaveAction() {
    var URL = location.pathname.replace('NewForm.aspx', 'EditForm.aspx');
    if (GetUrlKeyValue('IsDlg') === '1') {
        URL += "?IsDlg=1";
    }
    $("#aspnetForm").attr('action', location.pathname + "?Source=" + URL);
    return true;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.