Commentor Blog

When Quality Matters

Commentor A/S

When Quality Matters

Contact usSend mail

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2010

Basic Web Part Creation

 1.     Create C# Class Library
Name the class library according to needs and relevant guidelines

2.     Rename class according to your web part
E.g. MyOwnWebPart

3.     Add refence to System.Web
This is the most needed reference for writing Web Parts – just add any other references your need accordingly.

4.     Derive from System.Web.UI.WebControls.WebParts.WebPart
Notice the fact that the solution derives from a “System.Web” class. If you need to connect web parts on different pages, need to create client side connections or leverage data caching in WSS 3 – you should use the Microsoft.Web.UI.WebControls.WebParts.WebPart (remember to reference Microsoft.SharePoint assembly in that case)

5.     Override The RenderContents method to suit your needs
This is where your imagination comes in J Build whatever functionality you need to output and handle the rendering here.
Remember if you need to access e.g. SharePoint web services, you’ll have to consider code access security and/or deploy to GAC scenario.

6.     Add [assembly: System.Security.AllowPartiallyTrustedCallers()] to AssemblyInfo.cs
This is needed unless you add your assembly to the GAC. If you try to add your Web Part to a page without this it will show a dialog box and refuse to add your solution.

7.     Make post-build event (xcopy)
E.g. 
xcopy "*.dll" "C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin\" /y

Reconstruct the line above to suit your own SharePoint installation. You could choose your output location for the dll to be the same as above, but that’s not recommended. That’s hard to maintain if you work against automatic builds that uses the project files to determine file locations and if your co-developers is not aware of it and wonders why they can’t find the dll after build.

8.     Sign assembly (project properties)
This is a reasonable practice and will make your standard development procedure more prepared for real-world deploy where signing with certificates etc. might be relevant.

9.     Build solution
To make your dll, copy it to the bin folder of the SharePoint solution you’re developing for, and make the signing available in the dll (so you can find the public key, you’ll need it in a minute), you’ll have to build the solution - so go ahead.

10.      Find the relevant information for SafeControl entry in web.config
The SafeControl entry may look like this (one line):

<SafeControl Assembly="WebPartTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4580592393728f0b" Namespace="WebPartTest" TypeName="WebPartTest" Safe="True" />

The SafeControl entry is necessary in order to add the WebPart correctly to the WebPart gallery (and for the WebPart to function properly).

1.       First drop your dll on .NET Reflector (that extremely useful tool, for inspecting dll’s etc. Go get it, you’ll not be sorry). This will provide you with the “Assembly” part of the SafeControl entry.

2.       Then copy the Namespace from your solution to the “Namespace” part of the entry.

3.       Change the “TypeName” part of the entry to match your Class name from the WebPart solution.  

11.                       Add your Web Part to the Web Part gallery 
Choose: Site Actions →Site Settings → Web Parts (gallery) →”New”

Then navigate the list until you find your Web Part and check the checkbox.

Choose populate gallery to add your Web Part to the gallery

12.                       Add your Web Part to a page
Choose: Site Settings → Edit Page to add your Web Part to the current page.

When changing your implementation, all you need to do is to build the solution again, and the Web Part dll will be updated. Reload your page and voila!

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: SharePoint
Posted by jane.noesgaard.larsen on Monday, August 18, 2008 2:26 AM
Permalink | Comments (0) | Post RSSRSS comment feed