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 126.96.36.199 or equivalent. If you leave stars in the AssemblyVersion, the assembly will get installed multiple times in the GAC (for each version).
Just a quick note as i have previously done by other means – to get all of the groups you belong to, simple run whoami /groups on the command line
- In visual studio 2005, create a new web control library project and code controls as required
- Ensure that the project / assembly is strongly named. (from the menu Project -> <projectname> Properties -> Signing)
- Compile you project into a dll.
- 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.
- 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=188.8.131.52, Culture=neutral, PublicKeyToken=cdfbe353a0e07440″ Namespace=”TestSharepointWebControl” TypeName=”*” Safe=”True” />
- Perform an issreset (not fully sure if this is actually required).
- 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=184.108.40.206, PublicKeyToken=cdfbe353a0e07440, Culture=neutral” %>
- Call the relevant controls within the masterpage as required. E.g. <TestSharepointWebControl:WebCustomControl1 id=”WebCustomControl1″ runat=”server” />
- 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.
Quick summary how to create a webpart for SharePoint 2007…
- In visual studio 2005, create a new class library project (example code follows)
- Add a reference to the System.Web namespace (right-click references in the solution explorer and click add).
- 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.
- Alter your class so that inherits from the System.Web.UI.WebControls.WebParts.WebPart class.
- Override the Render method with whatever you want the webpart to display.
- 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).
- 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
- Compile your project to generate the webpart dll.
public class TestWebPart3 : WebPart
protected override void Render(HtmlTextWriter writer)
SPList pagesList = SPControl.GetContextWeb(Context).Lists
foreach (SPListItem currentItem in pagesList.Items)
Installing webpart for use in SharePoint.
- 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.
- Add the control to the safe controls section of the web.config file e.g. <SafeControl Assembly=”TestWebPart2, Version=220.127.116.11, Culture=neutral, PublicKeyToken=cdfbe353a0e07440″ Namespace=”TestWebPart2″ TypeName=”*” Safe=”True” />
- Do an iisreset
- 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.
- You will then be able to add your pages
Notes & Links
- 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.
- This is excellent article on how to set up a webpart http://blogs.tamtam.nl/mart/PermaLink,guid,a34071a0-ad27-44c7-aa78-3f956d6f920e.aspx
- 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.