SharePoint 2007: Writing a web control library for use in a masterpage

  1. In visual studio 2005, create a new web control library project and code controls as required
  2. Ensure that the project / assembly is strongly named. (from the menu Project -> <projectname> Properties -> Signing)
  3. Compile you project into a dll.
  4. Add the dll to your SharePoint bin directory (this will normally reside at somewhere like C:\Inetpub\wwwroot\wss\VirtualDirectories\41ea6637-fb56-4dcb-8a2e-9d1e91b56a20\bin – check in iismanager to see where the main SharePoint sites home directory is). Note that you could instead add the dll to the GAC if required.
  5. Add the control to the safe controls node in your main SharePoint web.config (the web.config will normally reside at somewhere like C:\Inetpub\wwwroot\wss\VirtualDirectories\41ea6637-fb56-4dcb-8a2e-9d1e91b56a20). <SafeControl Assembly=”TestSharepointWebControl, Version=, Culture=neutral, PublicKeyToken=cdfbe353a0e07440″ Namespace=”TestSharepointWebControl” TypeName=”*” Safe=”True” />
  6. Perform an issreset (not fully sure if this is actually required).
  7. Add a reference to the control library you have created (ensuring that the version number is correct). E.g. <%@ Register Tagprefix=”TestSharepointWebControl” Namespace=”TestSharepointWebControl” Assembly=”TestSharepointWebControl, Version=, PublicKeyToken=cdfbe353a0e07440, Culture=neutral” %>
  8. Call the relevant controls within the masterpage as required. E.g. <TestSharepointWebControl:WebCustomControl1 id=”WebCustomControl1″ runat=”server” />
  9. Upload the masterpage and checkin / publish accordingly. The master page should now run the relevant controls.

If your webcontrol hooks in to SharePoint, its best to inherit from the SPControl class as opposed to the System.Web.UI.WebControls.WebControl class. If it does hook into the SharePoint object model, add a reference to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI\Microsoft.SharePoint.dll and using statements for Microsoft.SharePoint and Microsoft.SharePoint.WebControls if required.

If you don’t sign, and add to safe controls in web.config (as per the steps described), you will get an error such as The control type “TestSharepointWebControl.WebCustomControl1″ is not allowed on this page.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>