none
Saving list items RRS feed

  • Question

  • Hi how can I execute custom code when the item is saved on a newform.aspx ? (save button pressed)

    using SPD

    Wednesday, July 30, 2014 2:13 PM

Answers

  • One way to accomplish that would be to control how the new item form is opened. If you open it yourself as a modal dialog box via JavaScript, you can access the "dialog result" of the box when it's closed, allowing you to run additional code if the user didn't click the cancel button, for example.

    <div style='display:inline-block;cursor:pointer;background-color:lightgray;' onclick='SP.UI.ModalDialog.commonModalDialogOpen("http://mysite/myweb/lists/mylist/NewForm.aspx?Source="+document.location.pathname,{},postSaveCallBack); '>Create New Item</div>
    <script>
    function postSaveCallBack(result){
    	if(result != SP.UI.DialogResult.cancel){
    		//TO DO: client-side code to get ID of most recent item created by current user, then open edit form
    	}
    }
    </script>


    Thursday, July 31, 2014 8:40 PM

All replies

  • Hi.

    Using SPD (SharePoint Designer???) you can not add custom server side code.

    You can create an event receiver using Visual Studio 

    http://msdn.microsoft.com/en-us/library/ee231563.aspx

    http://msdn.microsoft.com/en-us/library/office/gg749858(v=office.14).aspx


    Regards,
    Bubu
    http://zsvipullo.blogspot.it

    Please mark my answer if it helped you, I would greatly appreciate it.

    Wednesday, July 30, 2014 2:52 PM
  • Hi I want to add javascript code in the newform,aspx. I want this to be executed
    Wednesday, July 30, 2014 3:01 PM
  • You can overload the JavaScript function PreSaveAction() to run JavaScript when the save button is pressed but prior to the save being committed. This can be useful for additional page validation (by returning false to cancel the save).

    You can add a content editor web part to newform.aspx with the following code:

    <script> function PreSaveAction(){
    //Your code goes here
    return true; } </script>

    I recommend saving that code to a text file and uploading it to a document library on your site. Then reference that file via the content link property of the content editor web part. This will prevent SharePoint from sanitizing your code, and naughty SharePoint Designer from executing it when you're trying to edit the page.

    Wednesday, July 30, 2014 3:43 PM
  • Hi

    The code has to run after the commit. Basically I'm trying to open the item in edit mode after the save. I'm using javacript to do this but I need the ID of the newly created item. This is why I need to run code after creation.

    Any ideas?

    Thanks

    Thursday, July 31, 2014 6:43 AM
  • One way to accomplish that would be to control how the new item form is opened. If you open it yourself as a modal dialog box via JavaScript, you can access the "dialog result" of the box when it's closed, allowing you to run additional code if the user didn't click the cancel button, for example.

    <div style='display:inline-block;cursor:pointer;background-color:lightgray;' onclick='SP.UI.ModalDialog.commonModalDialogOpen("http://mysite/myweb/lists/mylist/NewForm.aspx?Source="+document.location.pathname,{},postSaveCallBack); '>Create New Item</div>
    <script>
    function postSaveCallBack(result){
    	if(result != SP.UI.DialogResult.cancel){
    		//TO DO: client-side code to get ID of most recent item created by current user, then open edit form
    	}
    }
    </script>


    Thursday, July 31, 2014 8:40 PM