0
votes

does anyone know how to show the new list element form on button click, that I have on my custom aspx page? Like when you have a sharepoint list and you click on "add item" and a new element form loads up in a new dialog window? I need the same thing to happen when I click on my button. I know how to do that in Sharepoint Designer, but I need to do it programmatically in visual studio. I assume Javascript must be involved in some way and I am terrible with it. Thaks in advance!

One more question - do you know of a way to refresh the original page, the one that has the button, upon closing of the dialog window? Here is the scenario:

  1. I press the button,
  2. it opens the dialog window,
  3. I add an element to a Sharepoint list via it,
  4. I close the dialog window,
  5. I need the original page refreshed, so it repopulates the
    DropDownList and adds the title of this new list item to it.
3

3 Answers

2
votes

The javascript code to achieve that varies with the type of list you are using (Tasks, document library, etc).

To open a Sharepoint dialog window you can use the following javascript:

var options = {
  url: '<url to the add item page>',
  title: '<Title of your Dialog>'
}; 

// add an event handler for the dialog closed callback
options.dialogReturnValueCallback = Function.createDelegate(null, portal_modalDialogClosedCallback);

void(SP.UI.ModalDialog.showModalDialog(options))

function portal_modalDialogClosedCallback(result, value) {
    if(result === SP.UI.DialogResult.OK) { 
        //alert("OK was clicked");
    } 
    if(result === SP.UI.DialogResult.cancel) { 
        //alert("CANCEL was clicked");
    }

    window.frameElement.commitPopup(); // this will cause the list to refresh after the dialog closes
    // note: the above line only refreshes the List (which is the default Sharepoint OOB behavior anyway), to refresh the whole page use window.location.reload(); instead
}

The actual URL to use above varies with the type of list. Some examples:

  • Document Library-based: [url to your list, e.g. /Lists/MyList]/Forms/Upload.aspx
  • Announcements: [url to your list]/NewForm.aspx
  • Tasks: [url to your list]/NewForm.aspx

Hope this helps

1
votes

Take a look at the AjaxControlToolkit, especially ModalPopupExtender

1
votes

You'll have the use the Modal Dialog Framework and the JS client object model.