Soledad Pano's Blog

Just Technical Stuff

December 2007 - Posts

Hosting a Window Form Control in a SharePoint WebPart

Some months ago I posted about Hosting a Windows Form Control in a web page. I explained there how we can run a WinForm control from Internet Explorer by hosting it in a web page using the <object> tag. Now suppose you want to use the same solution in a custom web part that is included in a Sharepoint site. I will describe here where the control library should be placed and how it can be referenced from the web part.

When we develop a custom web part, we place its resources under the _wpresources web site, into a folder named <WebPartAssemblyName>/<WebPartAssemblyVersion>__<PublicKeyToken>. You can create there a subfolder (named "bin" por example) to place the win control assembly. This folder must have execute permissions set to Script Only (not Script and Executables as is the default under a SharePoint site). The picture bellow shows the resulting structure when placing the win form control assembly in a web part called MyCustomWebPart that is part of the MyCustomWebParts assembly:


Once the control assembly is placed in the correct place, the web part can render the <object> tag within the RenderWebPart method:

namespace MyCustomWebParts


  [XmlRoot(Namespace = "MyCustomWebParts")]

  public class MyCustomWebPart : WebPart


    protected override void RenderWebPart(HtmlTextWriter output)



        string resourcePath = SPWebPartManager.GetClassResourcePath(SPContext.Current.Web, GetType());


        output.Write( ReplaceTokens("<h1>Windows Form Control:</h1>" +

          "<object id='MyWinControl' classid='" + resourcePath + 

          "/bin/WindowsControlLibrary1.dll#WindowsControlLibrary1.UserControl1'" +

          "height='300' width='800' VIEWASTEXT>" +

          "  <PARAM name='Prop1Str' value='blabla'  valuetype='data'>" +

          "  <PARAM name='Prop2Int' value='2' valuetype='data'>" +

          " </object>"));






Posted: Dec 06 2007, 10:48 PM by spano | with 2 comment(s)
Filed under:
More Posts