Wednesday, August 24, 2011

How to open Modal Popup from Silverlight in Sharepoint 2010 using Visual Web part


In our application we are using Silverlight to display some of the site information. I have created a Silverlight Application and deployed the XAP file in the Site root using Module.
In the Silverlight application we are showing SharePoint users as hyperlinks. When we click on the user, a SharePoint dialogue should open which opens the user info page. We know that we can call java script functions from Silverlight using the command .But if use SharePoint 2010 Built in SharePoint Silverlight Web part, we cannot define the JavaScript function that relates to this Silverlight application.
So I have created a Visual Web part which holds silver light object (the XAP file) in it, the syntax is as simple as we add Silver light Object (XAP file )to the html file.
Note: If we use Visual Web Part, then the ClientContext .Current object in Silverlight will return null value.So we need to change our code from ClientContext ctx = ClientContext.Current
 to  ClientContext ctx = new ClientContext(siteCollectionUrl);

Here siteCollectionUrl will be passed to the Silverlight using initparams from the Visual Web part that holds the Silverlight Object. Below is the code.
SilverVisualWPUserControl.ascx
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="SilverPropertiesTabUserControl.ascx.cs" Inherits="DLRSilverTab.SilverPropertiesTab.SilverPropertiesTabUserControl" %>
    <script type="text/javascript">
        function onSilverlightError(sender, args) {

            var appSource = "";
            if (sender != null && sender != 0) {
                appSource = sender.getHost().Source;
            }
            var errorType = args.ErrorType;
            var iErrorCode = args.ErrorCode;

            var errMsg = "Unhandled Error in Silverlight 2 Application " + appSource + "\n";

            errMsg += "Code: " + iErrorCode + "    \n";
            errMsg += "Category: " + errorType + "       \n";
            errMsg += "Message: " + args.ErrorMessage + "     \n";

            if (errorType == "ParserError") {
                errMsg += "File: " + args.xamlFile + "     \n";
                errMsg += "Line: " + args.lineNumber + "     \n";
                errMsg += "Position: " + args.charPosition + "     \n";
            }
            else if (errorType == "RuntimeError") {
                if (args.lineNumber != 0) {
                    errMsg += "Line: " + args.lineNumber + "     \n";
                    errMsg += "Position: " + args.charPosition + "     \n";
                }
                errMsg += "MethodName: " + args.methodName + "     \n";
            }

            throw new Error(errMsg);
        }

        function OpenDialogueWidow(url) {
            var options = SP.UI.$create_DialogOptions();
            options.width = 750;
            options.height = 650;
            options.url = url;
            options.dialogReturnValueCallback = Function.createDelegate(
                        null, portal_modalDialogClosedCallback);
            SP.UI.ModalDialog.showModalDialog(options);

           
        }

        function portal_modalDialogClosedCallback(result, value) {
           
        }

       
    </script>
   
    <div id="silverlightObject" runat="server"></div>

SilverVisualWPUserControl.ascx.cs Page_Load method
protected void Page_Load(object sender, EventArgs e)
        {
            string siteCollectionUrl = SPContext.Current.Web.Url;
           
            string html =string.Format("<object data=\"data:application/x-silverlight-2,\" type=\"application/x-silverlight-2\" "
            +"style=\"display:block\" height=\"250px\" width=\"100%\"  >"
            +"<param name=\"source\" value=\"/SilverTabControl.xap\"/>"
            + "<param name=\"onerror\" value=\"onSilverlightError\" />"
            + "<param name=\"background\" value=\"white\" />"
            + "<param name=\"minRuntimeVersion\" value=\"2.0.31005.0\" />"
            + "<param name=\"initParams\" value=\"PropertyList=Property Information,PropertyContactList=Properties,PropertyID=Property_x0020_ID,PropertyIDValue=1,Duration=6000,url={0}\" />"
            + "<param name=\"autoUpgrade\" value=\"true\" />"
            + "<a href=\"http://go.microsoft.com/fwlink/?LinkID=124807\" style=\"text-decoration: none;\">"
            + "<img src=\"http://go.microsoft.com/fwlink/?LinkId=108181\" alt=\"Get Microsoft Silverlight\" style=\"border-style: none\"/>"
            + "</a></object>"
                  +"<iframe style='visibility:hidden;height:0;width:0;border:0px;' scrolling=no></iframe>",siteCollectionUrl);
            silverlightObject.InnerHtml = html;
        }


Silverlight Application MainPage.xaml.cs  How to invoke JavaScript Method to open Dialogue Window.
HyperlinkButton contactLink = new HyperlinkButton();

            contactLink.Click += new RoutedEventHandler(contactLink_Click);

            contactLink.Tag = new Uri("/_layouts/userdisp.aspx?ID=" + spUser.Id);
            contactLink.Cursor = Cursors.Hand;


void contactLink_Click(object sender, RoutedEventArgs e)
        {
            HyperlinkButton link= sender as HyperlinkButton;
            if(link!=null && link.Tag !=null)
            HtmlPage.Window.Invoke("OpenDialogueWidow", link.Tag.ToString());
        }

In the above Code OpenDialogueWidow is the JavaScript  function name which defined in the Visual Web part