Wednesday, May 4, 2011

Opening any SharePoint page as model dialog SP2010

Hi Guys,

As we all know that sharepoint 2010 is opening a lot of forms in its newly featured model dialog.

Few of this pages are list forms like NewForm.aspx, EditForm.aspx, DispForm.aspx, pages while creating new object like list,site…etc.

While customize your sharepoint site, if by any chance you required any of your page to show in model dialog then below is the code for the same.
function OpenDialog(strPageURL,strDialogTitle) {

//Creating New Object for SharePointDialog
var newDialogOptions = SP.UI.$create_DialogOptions();

//set URL for Dialog to open
newDialogOptions.url = strPageURL;

//you can set your custom size by setting height and width
autoSize: true,

//set title to show on head of dialog
newDialogOptions.title = strDialogTitle;

// This line required only if you want to call a function when we close dialog -- here we refresh the page
newDialogOptions.dialogReturnValueCallback = Function.createDelegate(null,CallbackFunc);

//open the dialog
SP.UI.ModalDialog.showModalDialog(newDialogOptions);

return false;
}

function CallbackFunc(result, target) {
//Refresh the current page
location.reload(true);
//some suggest to use below line but sometime this was not working
//so we are working on that
//SP.UI.ModalDialog.RefreshPage(SP.UI.DialogResult.OK);

}



The good thing for us is you can open any page with this dialog.

That page might have you custom webpart as well or anything will work.

Even JavaScript, query string, postback etc… available on that page because that page work similar as normal page it will also remove limitation of traditional model dialog.

If we want to refresh parent page after closing popup then also we can do that (as shown in code)

One more thing if you want to open another model dialog from already open model dialog then also this one is working

Thanks Antonio Lanaro for his wonderful post on this one.

Enjoy the popup.

2 comments:

Ben Attia Bacem said...

hello

i have the same requierement.

but this did not work for me in IE v8 and it work fine with FireFox and chrome.

any suggestion ?
thank you a lot.

SharePoint Kings said...

Ben,

this javascript is default and sharepoint also internally use this.

so this should work, we checked and its working perfectly fine in IE.

just put alert after each line and check where exactly its breaking.




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com