VS2005 Post Build Events: adding an assembly to the GAC

An assembly must be strongly named before it can be added to the GAC. The correct way to do this in visual studio 2005 is specify the path to a strong name key file (snk file – generated with sn -k ) in the signing tab of the projects properties dialog.

Once this is done, under the Build Events tab of the projects properties dialog, specify the following to install the assembly to the GAC on a successful build:
"C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin\gacutil.exe" /i "$(TargetPath)"

Note that you do not need to worry about uninstalling the existing assembly from the GAC before installing the new one – the above command takes care of this.

Also Note – make sure that you set the AssemblyVersion in AssemblyInfo.css to 1.0.0.0 or equivalent. If you leave stars in the AssemblyVersion, the assembly will get installed multiple times in the GAC (for each version).

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=1.0.0.0, 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=1.0.0.0, 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.

Notes:
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.

SharePoint 2007: Writing a webpart

Quick summary how to create a webpart for SharePoint 2007…

  1. In visual studio 2005, create a new class library project (example code follows)
  2. Add a reference to the System.Web namespace (right-click references in the solution explorer and click add).
  3. Add the top of the class add using statements for the System.Web.UI and System.Web.UI.WebControls.WebParts namespaces. Note that you don’t need any references to any SharePoint namespaces unless you are going to use the SharePoint object model.
  4. Alter your class so that inherits from the System.Web.UI.WebControls.WebParts.WebPart class.
  5. Override the Render method with whatever you want the webpart to display.
  6. The assembly must be strongly named, irrelevant of if it is to go in the GAC or Bin directory. To do this, generate a keypair (sn ?k to generate .snk file) and add the command line options for “/keycontainer” and “/keyfile” to the project build command line options (via project settings).
  7. If the assembly is going to be installed in the SharePoint bin directory instead of the GAC, mark the assembly with AllowPartiallyTrustedCallers. See http://support.microsoft.com/?kbid=839300
  8. Compile your project to generate the webpart dll.

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Security;
[assembly: AllowPartiallyTrustedCallers]
namespace TestWebPart2
{
    public class TestWebPart3 : WebPart
    {
        protected override void Render(HtmlTextWriter writer)
        {
            SPList pagesList = SPControl.GetContextWeb(Context).Lists
["Pages"];
            foreach (SPListItem currentItem in pagesList.Items)
            {
                writer.Write(currentItem["Title"]);
                writer.Write("<br />");
            }
        }
    }
}

Installing webpart for use in SharePoint.

  1. To install the webpart for use in SharePoint, you need to first copy the dll generated above to either the GAC (C:\WINDOWS\assembly) or the SharePoint Bin directory (find out the name of the site using iis manager – look for home directory for SharePoint on port 80). The SharePoint bin directory will reside at something like C:\Inetpub\wwwroot\wss\VirtualDirectories\41ea6637-fb56-4dcb-8a2e-9d1e91b56a20\bin.
  2. Add the control to the safe controls section of the web.config file e.g. <SafeControl Assembly=”TestWebPart2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=cdfbe353a0e07440″ Namespace=”TestWebPart2″ TypeName=”*” Safe=”True” />
  3. Do an iisreset
  4. Go to the top-level site settings for the web where you want to activate the webpart and click on “Webparts”. Click on the new button. This will take you through to http://localhost/_layouts/NewDwp.aspx which will detect which webparts are available from dlls etc. Your webpart should show up here. To be able to use the webpart in your site collection you must tick the relevant check box, then hit the populate gallery button.
  5. You will then be able to add your pages

Notes & Links

  1. Its seems that SharePoint will only detect one webpart per dll currently (although strangely there are two webparts from the Microsoft.Office.Excel.WebUI namespace: ExcelWebRenderer and InternalEwr). Hopefully this is something that will be fixed in the beta 2 technical refresh.
  2. This is excellent article on how to set up a webpart http://blogs.tamtam.nl/mart/PermaLink,guid,a34071a0-ad27-44c7-aa78-3f956d6f920e.aspx
  3. Another excellent article: http://blah.winsmarts.com/2006/05/14/writing-custom-webparts-for-sharepoint-2007.aspx. This pays special attention to permissions of the SharePoint bin directory.