none
Redirect to Another Page from NewForm.aspx with the New Item’s ID with REST API RRS feed

  • Question

  • I'm working in SharePoint 2013 Using REST API and I'm trying to Redirect to Another Page from NewForm.aspx with the new Item’s ID.

    This was possible with SP Services  created by Marc D anderson . 

    The idea is to have users create a new item and on save action the user is redirected to EditForm.aspx?ID= (currentitemID)

    Has anyone else done this and if so please provide code and guidance.

    Thank you!




    • Edited by Ace.08.90 Wednesday, November 13, 2019 9:31 PM
    Wednesday, November 13, 2019 9:21 PM

Answers

  • Hi,

    When you creating list item by rest api, you could get the created item id, so you could redirect to edit page with created item id.

    Sample script:

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script type="text/javascript">
            function CreateItem() {
                $.ajax
                    ({
                        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('TestList')/items",
                        type: "POST",
                        data: JSON.stringify
                            ({
                                __metadata:
                                {
                                    type: "SP.Data.TestListListItem"
                                },
                                Title: "Rest api Test"
                            }),
                        headers:
                        {
                            "Accept": "application/json;odata=verbose",
                            "Content-Type": "application/json;odata=verbose",
                            "X-RequestDigest": $("#__REQUESTDIGEST").val()                    
                        },
                        success: function (data, status, xhr) {                        
                            window.location.href = _spPageContextInfo.webAbsoluteUrl + "/Lists/TestList/EditForm.aspx?ID=" + data.d.Id+""
                        },
                        error: function (xhr, status, error) {
                            console.log(data.responseJSON.error);
                        }
                    });
            }
    
        </script>
        <input id="Button1" onclick="CreateItem()" type="button" value="button" />

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Proposed as answer by Dennis GuoModerator Monday, November 18, 2019 8:26 AM
    • Marked as answer by Ace.08.90 Tuesday, November 19, 2019 6:04 PM
    Thursday, November 14, 2019 1:48 AM
  • Hi,

    Updated the script to get value from input control.

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script type="text/javascript">
            function CreateItem() {
                $.ajax
                    ({
                        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('TestList')/items",
                        type: "POST",
                        data: JSON.stringify
                            ({
                                __metadata:
                                {
                                    type: "SP.Data.TestListListItem"
                                },
                                Title: $('#txtTitle').val()
                            }),
                        headers:
                        {
                            "Accept": "application/json;odata=verbose",
                            "Content-Type": "application/json;odata=verbose",
                            "X-RequestDigest": $("#__REQUESTDIGEST").val()
                        },
                        success: function (data, status, xhr) {
                            window.location.href = _spPageContextInfo.webAbsoluteUrl + "/Lists/TestList/EditForm.aspx?ID=" + data.d.Id + ""
                        },
                        error: function (xhr, status, error) {
                            console.log(data.responseJSON.error);
                        }
                    });
            }
    
        </script>
        <div>
            Title:
            <br />
            <input type="text" id="txtTitle" />
            <br />
            <input id="Button1" onclick="CreateItem()" type="button" value="button" />
        </div>

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by Ace.08.90 Wednesday, November 20, 2019 1:48 AM
    Wednesday, November 20, 2019 1:21 AM

All replies

  • Hi,

    When you creating list item by rest api, you could get the created item id, so you could redirect to edit page with created item id.

    Sample script:

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script type="text/javascript">
            function CreateItem() {
                $.ajax
                    ({
                        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('TestList')/items",
                        type: "POST",
                        data: JSON.stringify
                            ({
                                __metadata:
                                {
                                    type: "SP.Data.TestListListItem"
                                },
                                Title: "Rest api Test"
                            }),
                        headers:
                        {
                            "Accept": "application/json;odata=verbose",
                            "Content-Type": "application/json;odata=verbose",
                            "X-RequestDigest": $("#__REQUESTDIGEST").val()                    
                        },
                        success: function (data, status, xhr) {                        
                            window.location.href = _spPageContextInfo.webAbsoluteUrl + "/Lists/TestList/EditForm.aspx?ID=" + data.d.Id+""
                        },
                        error: function (xhr, status, error) {
                            console.log(data.responseJSON.error);
                        }
                    });
            }
    
        </script>
        <input id="Button1" onclick="CreateItem()" type="button" value="button" />

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Proposed as answer by Dennis GuoModerator Monday, November 18, 2019 8:26 AM
    • Marked as answer by Ace.08.90 Tuesday, November 19, 2019 6:04 PM
    Thursday, November 14, 2019 1:48 AM
  • Hi,

    I am checking to see how things are going there on this issue. Please let us know if you would like further assistance.

    You can mark the helpful post as answer to help other community members find the helpful information quickly.

    If you need further help, it would be better to provide details requirement(demo screenshot) so we could try to research.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, November 19, 2019 8:06 AM
  • Lee,

     

    Thank you so much for taking your time to reply! This works for the exception of having the title field preset to "Rest api Test". Is there a way to create the fields in HTML and bind them to the script to submit the data? Also, can you use people picker and date picker with this approach?  e.g.

    <div>
    Title:
    <br />
    <input type="text" id="txtTitle" />
    </div>

    Thank you,

    Ace


    • Edited by Ace.08.90 Tuesday, November 19, 2019 6:02 PM spelling
    Tuesday, November 19, 2019 6:02 PM
  • Hi,

    Updated the script to get value from input control.

    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
        <script type="text/javascript">
            function CreateItem() {
                $.ajax
                    ({
                        url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/GetByTitle('TestList')/items",
                        type: "POST",
                        data: JSON.stringify
                            ({
                                __metadata:
                                {
                                    type: "SP.Data.TestListListItem"
                                },
                                Title: $('#txtTitle').val()
                            }),
                        headers:
                        {
                            "Accept": "application/json;odata=verbose",
                            "Content-Type": "application/json;odata=verbose",
                            "X-RequestDigest": $("#__REQUESTDIGEST").val()
                        },
                        success: function (data, status, xhr) {
                            window.location.href = _spPageContextInfo.webAbsoluteUrl + "/Lists/TestList/EditForm.aspx?ID=" + data.d.Id + ""
                        },
                        error: function (xhr, status, error) {
                            console.log(data.responseJSON.error);
                        }
                    });
            }
    
        </script>
        <div>
            Title:
            <br />
            <input type="text" id="txtTitle" />
            <br />
            <input id="Button1" onclick="CreateItem()" type="button" value="button" />
        </div>

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by Ace.08.90 Wednesday, November 20, 2019 1:48 AM
    Wednesday, November 20, 2019 1:21 AM
  • Lee,

    This works beautifully thank you so much! I'm knew to development and it would've taken me months to figure this out. Any idea on the SharePoint People picker and Date Picker?

    Wednesday, November 20, 2019 1:53 AM
  • Hi,

    Thanks for marking the replies as answer. I’m pleased to know that the information is helpful to you. I hope you are delighted with the service we provided you.

    Here I will provide a brief summary of this post for your information. 

    Requirement

    ====================

    Redirect to Another Page from NewForm.aspx with the New Item’s ID with REST API

     

    Solution

    ====================

    Create list item by rest api and then getting the created item id, redirect to other page by window.location.href.

    For the People picker and Date Picker requirement, I would suggest post a ne thread and we/other community members could try to help you in new thread, the reason why we recommend posting ‘one topic one thread’ is this will help other community members who has similar requirement could find helpful information quickly.

    Thanks for your understanding.

    Best Regards,

    Lee


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, November 20, 2019 3:28 AM