There are plenty of articles out there on how to develop asp.net 2.0 webparts and start using them on your webpages so I’m not going to cover that at all. Instead I’ll just point you towards this article:
…ok I couldn’t find one about create a server control webpart, I’ll post something quick on how to do that shortly…
So here is a picture of our lovely ASP.NET 2.0 webpart that does currency conversion for us.

A nice thing that we can get our asp.net webpart to do is to export a dwp or webpart file which is what we need to import it into a SharePoint 2007 site.

This saves creating one ourselves and having to type out xml. You probably wouldn’t want to use this dwp file to import the webpart on a production server because if you open the file you saved you will see that it contains every personalizable attribute of the asp.net 2.0 webpart. By default the Export option isn’t turned on for asp.net 2.0 webparts. To enable it add
this.ExportMode = WebPartExportMode.All;
to the constructor of your webpart control. You can save the file with a .dwp exstention or leave it as .webpart

Now comes the fiddly step. We can either add our webparts .dll file into the SharePoint servers global assembly cache, or place it in the bin folder of the site. Either way you’ll still be needing to edit web.config to make the webpart a safecontrol for the site as you did with SharePoint 2003.
1, Place it in the virutal directory’s bin folder.
By default SharePoint creates your root level sites at c:\innetpub\wwwroot\wss\Config\VirtualDirectories\…..
Your site will either be a folder that has been given a guid filename or the port number you assigned it when creating. Luckily I am doing this example on a site created using port 5488. If you’re wondering which file your site is for you’ll need to go into IIS -> right click the site -> properties -> home directory … and you’ll find the path in the location path.
If you’ve not added a custom webpart to your SharePoint site before you’ll need to just create the bin folder. Once you’ve done that copy and paste your webpart dll into it. Now the exciting part of making your webpart safe.
Open up web.config that is in your SharePoint sites root folder and about a third of the way down you’ll see a SafeControl element. My assembly (or dll file) is called ASPNETWebPart and the namespace of the webpart was also ASPNETWebPart so adding my control as safe is easy…
<SafeControl TypeName="*" Safe="True" Namespace="ASPNETWebPart" Assembly="ASPNETWebPart"
/>
Save web.config and open a browser to your SharePoint site. Do the following:
Site Actions -> Edit Page -> Add a WebPart -> Advanced Web Part gallery and options
The ‘Add Web Parts’ pane will open now open up as shown below.

Click the small down arrow to the right of ‘Browse’ and select Import from the drop down.
Click the browse button and locate the .dwp or .webpart file you exported earlier from your ASP.NET 2.0 webpage and the webpart will appear in the ‘Uploaded Web Part’ section. Drag and drop the web part as you usually would and bobs your uncle…

2. Place it in the global assembly cache.
Another option is to register the webpart dll in the global assembly cache (gac). You will still need to make the control safe in each SharePoint site you are planning on using the control, but now if you make an update you only need to update it in one place rather than copying it to many bin folders. To add the web part to the gac you need to give it a strong name. This is now really easy in Visual Studio 2005. Just right click on your web part project file, go to the signing tab, and check the checkbox for ‘sign the assembly’. Select from the drop down to create new and then enter a filenname for the key such as ASPNETWebPart.snk. Once you’ve entered a .snk filename it will appear in the dropdown box where you selected to create a new one. You can now rebuild your project and the assembly will be signed with a strong name. Woot!
Now to deploy your assembly to the gac. Open a Visual Studio 2005 command prompt and navigate to the directory where your WebPart assembly is. Type in the following to register your WebPart in the gac
gacutil /i ASPNETWebPart.dll
replacing the name of the dll with whatever your assembly is called. Then do an IIS reset by typing iisreset in at the command prompt. Now that your assembly is stored in the gac it is available to be used by every SharePoint site that it is imported as long as the control is made safe in web.config. Since we’ve registered the .dll in the gac there are another couple of bit of information we need to be able to register the control as safe, these being the version and the public key. You can get these by going:
Control Pannel -> Administrative Tools -> Microsoft .Net Framework 2.0 Configuration -> Manage the Assembly Cache -> View List of Assemblies in the Assembly Cache.
You can then scroll down to your assembly name and pick out the version and public key. To be able to copy and paste the public key out right click on the assembly and select properties – from there you can highlight it.
Find the web.config file as before for the SharePoint site that you want to add the control to and add the following line to the SafeControls list:
<SafeControl TypeName="*" Safe="True" Namespace="ASPNETWebPart" Assembly="ASPNETWebPart,
Version=1.0.0.1, Culture=neutral, PublicKeyToken=f1333f77682cf865" />
replacing the Version and PublicKeyToken with the values you got from the gac. Doing iisreset again after any web.config change is a good thing. One final change to make is to either recreate the .dwp file from your asp.net 2.0 webpart page by adding the webpart control that has been registered in the gac and re-exporting it, or just manually going in with notepad and editing it. Basically we need to edit the top line under the metadata tag of the .dwp file from:
<Type name="ASPNETWebPart.ASPNETWebPart" />
to
<Type name="ASPNETWebPart.ASPNETWebPart, ASPNETWebPart, Version=1.0.0.1,
Culture=neutral, PublicKeyToken=f1333f77682cf865" />
As you can see we are just entering a little more information like we had to in web.config.
So now we are ready to go. We edit the SharePoint page as before and browse to our edited/new .dwp file, and then drag and drop the webpart to which ever zone we like. One thing that I did do was to delete the bin folder that I created in the first option of how to add the webpart. I don’t think it would cause any problems if you left it there, but I’ll check that out and post back if it does.
Anyway, hope that helps. Being able to use asp.net 2.0 web parts in both normal .Net 2.0 and SharePoint 2007 sites is quite exciting as you can now develop web parts that can be used by a lot more people. Any questions…..fire away!
Technorati Tags : ASP.NET 2.0 SharePoint 2007