Creating Web Parts by A J Noon
This blog is not intended to be an exhaustive tutorial there is much more detail in my document on the subject Creating Web Parts by A J Noon . Below are the steps I’ve followed to get my web parts working:
Installing the templates
This will install a Visual Studio template project for creating web part using either C# or VB.NET.
- Install the web part templates for VS.NET
Creating a web part
- Create your web part
- Start VS.NET and choose to create a new Web Part Library Project
- When you create the project it adds a RenderWebPart method, similar to the RenderWebControl method for a web control, which is used for outputting the UI.
Putting a simple line like ‘output.Write("Hello");’ will be sufficient to test that the web part works.
Deploying a web part
- Edit the dwp file
- Make sure the Assembly and TypeName properties are correct
- Assembly should be the name of the namespace you are using i.e. MyWebPart
- TypeName is the namespace followed by your web part class i.e. MyWebPart.TestWebPart
- If you have more than one web part in your web part library make sure that each web part class file has a corresponding dwp file and that each is specified in your manifest.xml file
- Add a CAB Project to your solution
- Right click the CAB Project and select Add -> Project output
- Select your web part project/s and click OK
- Compile the solution
- Add your web part package to SPS
- From a command prompt run the following command
- stsadm.exe -o addwppack -globalinstall -force -filename [file.cab]
- This will install your web part into SPS forcing an overwrite of any previous web parts with the same name
- Register your namespace as safe
- Add the following to the servers web.config file (usually in c:\inetpub\wwwroot\)
<SafeControls> <SafeControl Namespace="FlashCardWebPart" TypeName="*" /> </SafeControls> - Add the web part to your page!
Debugging a web part
- To debug
- In the Configuration Properties -> Debugging section of your project properties dialogue
- Set Enable ASP and ASP.NET debugging to true
- In the Configuration Properties -> Build section of your project properties dialogue
- Set the output path to your servers inetpub\wwwroot\bin directory
- Now you can attach to the w3wp.exe (.NET) process to debug your web part (Debug -> Processes -> w3wp.exe -> CLR)
Remote Debugging a web part
- Install remote debugging services on the remote server
- Share the directory hosting the sharepoint application usually c:\inetpub\wwwroot\bin. and the directory that holds sharepoint.dll, typically this is c:\Program Files\Common Files\Microsoft Shared\Web server extensions\60\ISAPI
- Open the web part project on your local machine
- Make sure your compilation path is set to the remote machines share you created earlier
- Now debug as normal but when you go to attach to the w3wp.exe process make sure to navigate to the remote machine!
Error messages
By default the error reporting in Sharepoint is switched off and you’ll just get a generic error when something fails, look familiar?
There are a couple of things to help one is to use the call stack to identify where the error occurred to get a stack trace: - Edit the web.config file found in your SPS virtual folder usually c:\inetpub\wwwroot
- Find the <safeMode> tag and change it to:
<SafeMode MaxControls="50" CallStack="true" /> - Also make sure the <customErrors> tag is disabled:
<customErrors mode="Off" /> You will now get a proper stack trace and summary of the error ala ASP.NET when an error occurs.
Another option for reporting on the success or failure of your web part is to enable tracing, this will tag a summary to the end of each page (success or fail), this is probably too much information for most purposes but to enable it you need to add the following line to the <system.web> tag of your web.config file:
<trace enabled="true" pageOutput="true"/>
And what you will get is a load of information about the process being run tagged onto the end of each page, like: