1

For adding an item to list, the newForm.aspx is opened in new window. After clicking on save button I need to close the window.

I used window.close(), It just closes the window but the item is not saved. It means before saving only the window is closed. What is the right way of doing this?

2

2 Answers 2

4

If you are looking for a solution which closes the new form once item is saved. Then SharePoint provides an OOB way to launch the new forms in a dialog, which will close the form once you save an item. But this is not set by default.

Follow steps to open form in dialog.

  1. Go to list settings
  2. Under general settings section, click on Advanced Settings link
  3. Scroll down the window, and "Check" the second last option "Launch forms in dialog?" and save it.

enter image description here

Update:

If you want to open the list's new item form, from some other page on your SharePoint site. Then you can rely on SP.UI.ModalDialog.showModalDialog(options);, which opens the page in dialog.

function openInDailog() {
     var options = {
         url: '/Lists/YourListName/NewForm.aspx',
         tite: 'New Form',
         allowMaximize: false,
         showClose: true,
         width: 430,
         height: 230
     };

     SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options);

     return false;
}

Refer SP.UI.ModalDialog.showModalDialog for SP 2013

14
  • Is it possible to give a link for this so that clicking on link, that dialog should appear
    – KumarV
    Commented Dec 9, 2015 at 12:45
  • Do you want this link to be on some other page? Which when clicked opens the dialog form?
    – Asad Refai
    Commented Dec 9, 2015 at 12:53
  • yes on other page..
    – KumarV
    Commented Dec 9, 2015 at 12:58
  • I have updated my answer. You can have a look..
    – Asad Refai
    Commented Dec 9, 2015 at 13:18
  • 1
    Writing JavaScript on NewForm is not straight forward. Only way is to add a content editor webpart on it.
    – Asad Refai
    Commented Dec 10, 2015 at 7:25
0

The dialog was automatically getting closed so I made some changes to the code provided by Asad then it worked..

<script>


function openModalDialog() {
         var options = {
             url: '/Lists/YourListName/NewForm.aspx',
             tite: 'Sample Title',
             allowMaximize: false,
             showClose: true,
             width: 980,
             args: {title: "title from arguments"},
             height: 400
         };        
         SP.SOD.execute('sp.ui.dialog.js', 'SP.UI.ModalDialog.showModalDialog', options);        
         return false;
     }
</script>

And then adding code to this newform.aspx which is opened as dialog is same as adding to a normal newform.aspx page i.e. by adding a content editor page.

Your Answer

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

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