Friday, December 16, 2011

Using the Dialog framework in SharePoint 2010 – Part 5


If you have not gone through Part-1 to Part-4 of this series, I would recommend you reading them first and then continue reading from here on.

In this post, we are going to see how we can pass parameters to model dialog window from a parent page.

We are going to take the same example which we had taken in previous posts.

Add a query string to a url parameter to the function.

    function OpenModalDialog() {        

        var sitename = 'SharePointkings';

        var options = {
            url: '/{site url}/_layouts/SharePointActivities/CustomDialog.aspx?sitename=' + sitename,
            tite: 'Open a custom dialog',
            allowMaximize: false,
            showClose: true,
            width: 750,
            height: 550,
             args:parameters,
            dialogReturnValueCallback: FunctionToCall
           
           
        };
        SP.UI.ModalDialog.showModalDialog(options);
    }

If you would like to get an intellisence of Visual studio for SP.UI and SP.UI.Debug javascript file, then add a reference like this

<script type="text/javascript" language="javascript">

   /// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.UI.Dialog.debug.js" />
   /// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.UI.Dialog.js" />
</script>


And then when you type, you get the Intelisence.


And on the receiving model dialog page write down the following JavaScript functions in your model dialog page.

<script type="text/javascript" language="javascript">

   /// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.UI.Dialog.debug.js" />
   /// <reference path="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\SP.UI.Dialog.js" />

   if (typeof (_spBodyOnLoadCalled) == 'undefined' || _spBodyOnLoadCalled) {  
   
   window.setTimeout(AlertParametersPassedFromParent, 0);       
     }  
   else {

       _spBodyOnLoadFunctionNames.push("AlertParametersPassedFromParent");

       }

 function getQuerystring(key, default_) {
     if (default_ == null) default_ = "";
     key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
     var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
     var qs = regex.exec(window.location.href);
     if (qs == null)
         return default_;
     else
         return qs[1];
 }


 function AlertParametersPassedFromParent() {   

     var sitename = getQuerystring('sitename');

     alert(sitename);
   }
   

  </script>


When the page loads, we push the function AlertParametersPassedFromParent to execute and called a function called getQueryString which is actually logic to retrieve query string which is passed from the parent page. There is no ready method in JavaScript like server side methods. So we have to use our own method to get that.

And we have then just prompted the parameter value with alert to see if it has passed properly.

Here is what we get as an end result.




I hope you have enjoyed the entire series of model dialog framework. We will try and add some new content if we find anything interesting about model dialog more. Of course we encourage and appreciate our readers to share their experiences so that we all SharePoint working community get benefited from it.





1 comment:

Anonymous said...

very useful series of articles.
Thanks




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