Skip to main content

Cloud

Web Parts circa 2007

WebParts Circa 2007:

SharePoint 2007 (MOSS) Web Parts

Since I started working on the Office 2007 Beta 1 there was some confusing information out in cyberspace regarding webparts for the latest version of SharePoint designated v3.0.

Because of the plethora of SharePoint 2003 (v2.0) webparts, Microsoft built in backwards compatibility and you can develop webparts using the same templates and coding techniques as before. Microsoft however recommends deriving from System.Web.UI.WebControls.WebParts.WebPart instead of Microsoft.SharePoint.WebControls.WebPartPages.WebPart. This ASP.Net 2.0 compatible class does things a little differently and this combined with a new deployment mechanism is the purpose of this blog.

The key differences for ASP.Net compliant webparts:

· Do not use the v2.0 attributes

· Override Render instead of WebPartRender

· No .dwp or Manifest.xml required

· Deployed and added through Web Part Gallery admin page rather than STSADM.exe/Upload mechanism of old.

If we look at the key pieces of code shown in Listing 1.

  • using System.Web.UI.WebControls.WebParts (line 7) – Derive the class from the WebPart class in this namespace.
  • [Personalizable(PersonalizationScope.Shared)] (line 17) – This attribute decorates the public property for either Personal or Shared storage and the property shows up in the Miscellaneous configuration section
  • protected override void RenderContents(HtmlTextWriter writer) (line 30) – Same as the v2.0 RenderWebPart method where the custom coding goes.

To deploy the webpart:

  1. Copy the compiled dll into the _app_bin folder of the target portal e.g. C:InetPubwwwrootwssvirtualdirectories80_app_bin
  2. In the web.config of the portal root directory add the SafeControl entry e.g. <SafeControl Assembly="WP20Test2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=955f21a09493739e" Namespace="WP20Test2" TypeName="*" Safe="True" />
  3. In the Site Settings section of the Portal site select Webparts in the Galleries section (see Figure 2)
  4. From the tools menu in select New
  5. If all has gone well you should see the webpart listed alphabetically as shown in Figure 3. Select the checkbox and then the Upload button: Note the automatically created .dwp entry.
  6. The web part will show up as shown in Figure 4 and clicking on it will test it without having to navigate to a webpart page (Figure 5)

And that’s all there is to it. For access to the SharePoint object model references need to be added as in v2.0.

Happy coding.


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Web.UI.WebControls.WebParts;
  8. namespace WP20Test2
  9. {
  10. public class WPTuesdayTest : WebPart
  11. {
  12. private string m_someText = = "Good Morning PointBridge!!";
  13. [Personalizable(PersonalizationScope.Shared)]
  14. public string SomeTextProperty
  15. {
  16. get
  17. {
  18. return this.m_someText;
  19. }
  20. set
  21. {
  22. this.m_someText = value;
  23. }
  24. }
  25. protected override void RenderContents(HtmlTextWriter writer)
  26. {
  27. this.EnsureChildControls();
  28. string html = "<p align=’center’>" + this.m_someText + "<br>" + DateTime.Now.ToShortTimeString()
  29. + "<br>" + DateTime.Now.ToLongDateString() + "</p>";
  30. writer.Write(html);
  31. }
  32. }
  33. }

Listing 1 – sample web part

Figure 1 – Web Part Gallery (note new .webpart extension)

Figure 2 – Select new Web Part (note new .webpart extension)

Figure 3 – New Web Part in Gallery

Figure 4 – Tesing new Web Part

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

PointBridge Blogs

More from this Author

Follow Us