maandag 22 oktober 2007

WebParts: what you need to know!

Recently I started with a course about WSS 3.0 and like all courses they start with the classic 'Hello World' exercise. I'm not going to talk about how to make a 'Hello World' because there are plenty of posts where this is already described.I'll tell you where I had some troubles so that you can avoid them.
The creation of the 'Hello World' itself isn't realy a problem as we all know it it's just a print to your screen to learn the basic syntax of the appliction. But when you start building and deploying you may face some problems like I faced some myself.
Let's start with the build and deploy procedure.To build and deploy a WebPart you have two options. The firstone is to build and deploy your .dll to the GAC (Global Assembly Cache). The problem you will face here is that you have to strongly name you assembly for it to work. So before building your assembly make sure that you go to you project properties -> signing -> check the 'signing the assembly'checkbox, choose to make a new key file. You'll see that the newly made key file will be added to your project. Copy paste you assembly to the GAC ( C:\WINDOWS\assmebly ).The other possiblity is to copy/paste your .dll to the bin directory of the server you are working with.So go to c:\inetpub\wwwroot\wss\virtualdirectories\80\bin\ (for the default :80 port) and copy paste your .dll
Putting your .dll in the bin folder is not enough. You have to tell SharePoint that it's safe to import your WebPart.To do this we have to make some minor modifications to our web.config file. You'll find that file in the c:\inetpub\wwwroot\wss\virtualdirectories\80\ folder.Open the web.config and look for the <safecontrols>tags, at the bottom of the list you can add your <safecontrol>tag wich will tell SharePoint your WebPart is safe.To be sure you got the right value for your WebPart you can use Lutz Roeder's .NET Reflector.Simply open your .ddl in it and copy paste the needed value.Your SafeControl tag should look something like this.

<SafeControl Assembly="HelloWorldWP, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" Namespace="HelloWorldWP" TypeName="*" Safe="True" />

Note that if you have deployed your WebPart to the GAC and thus strongly named it you'll get a unique ID for the PublicKeyToken and not the value 'null' like in our example here.
Another problem I faced, not with the 'Hello World' application but with bigger WebPart is the lack of error messages SharePoint is providing. To counter this you can change your web.config file a bit so it'll tells you wich error has occured.To do so change the following things in your web.config:

<sharepoint><safemode callstack="true">
<system.web><customerrors mode="off">
<compilation debug="true" batch="true">

Now that you know what the error is you can start debugging. attach the w3wp.exe to your process and start debugging with VS2005.

I hope this will be helpfull as these were the 2 problems I faced when starting to develop WebParts.

Geen opmerkingen: