Tuesday, February 19, 2013

Calling Winform function from SharePoint page.

Consider a scenario like passing a value from SharePoint page(ex. document library URL) or something to a windows form application function and execute a code based on the value passed. Web page and windows form may seem like two completely non-linked entities. But there is a way to establish connection to and fro.
 
Below are the steps to call a windows form function from a SharePoint page. 

1. Create a Win formand add a webbrowser control. Give the SharePoint site URL as the webbrowser control URL property.

2. Inside the Win formadd the ComVisibleAttribute to the class level.

 [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public partial class Save : System.Windows.Forms.Form
    {}

3. Add the ObjectForScripting property. This will act as a interface between the webpage and Win form

        [BrowsableAttribute(false)]
        public Object ObjectForScripting { get; set; }


4.  Add a function in Win form with a parameter just for test purpose. We will call this function from the SharePoint page.


        public void Test(String docurl)
        {
          MessageBox.Show(docurl);
        }

5. Now in the SharePoint page in the aspx page(if its an application page) call the Winform function by calling the below javascript code.

window.external.Test('url');

This will call the parent WinForm function and popup the message box.

6. To call this in .cs file code register with client script like below

this.Page.ClientScript.RegisterStartupScript(this.GetType(), "opendoc", "window.external.Test('" + locationpath + "/" + docname.Text);", true);


No comments:

Post a Comment