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:
- Copy the compiled dll into the _app_bin folder of the target portal e.g. C:InetPubwwwrootwssvirtualdirectories80_app_bin
- 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" />
- In the Site Settings section of the Portal site select Webparts in the Galleries section (see Figure 2)
- From the tools menu in select New
- 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.
- 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.
-
using System;
-
using System.Collections.Generic;
-
using System.Text;
-
using System.Web;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
using System.Web.UI.WebControls.WebParts;
-
-
namespace WP20Test2
-
{
-
-
public class WPTuesdayTest : WebPart
-
{
-
-
private string m_someText = = "Good Morning PointBridge!!";
-
-
[Personalizable(PersonalizationScope.Shared)]
-
public string SomeTextProperty
-
{
-
get
-
{
-
return this.m_someText;
-
}
-
set
-
{
-
this.m_someText = value;
-
}
-
}
-
-
protected override void RenderContents(HtmlTextWriter writer)
-
{
-
this.EnsureChildControls();
-
string html = "<p align=’center’>" + this.m_someText + "<br>" + DateTime.Now.ToShortTimeString()
-
+ "<br>" + DateTime.Now.ToLongDateString() + "</p>";
-
writer.Write(html);
-
}
-
}
-
}
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