kamagra how much to take

Building A Simple SharePoint 2007 / WSS v3 WebPart

First Steps In Creating A Simple SharePoint 2007 (MOSS) / WSSv3 WebPart

There are several differences that exist between the ways that WebParts were written in the SharePoint 2003 framework as opposed to the SharePoint 2007 (MOSS) framework that dramatically increases the functionality that is available to the developer. As written about in previous articles, there has been a dramatic shift in how WebParts are structured within the 2003 framework and the 2007 framework.

What Has Changed?

The way that WebParts are programmed in WSS 3.0 has changed drastically, however the benefits of programming WebPart instead of standalone .NET application remains the same. One of the most important things that is covered with the introduction of WSS 3.0 is the legacy style of creating WebParts (although both inherit from the WebPart class, in 3.0 this class is located in System.Web.UI.WebControls.WebParts namespace, and in WSS 2.0 this base class was located in the Microsoft.SharePoint.Pages namespace) is still maintained, and although the environment may have changed, programmatic efforts will not have been wasted. Since the new WebPart framework is based on the ASP.NET 2.0 framework, in order to maintain functionality and rendering capability there has been changes to the Microsoft.SharePoint.dll, referenced heavily when creating new WebPart projects.

WebPartManager Control

The most important concept to grasp in relation to developing ASP.NET 2.0 WebParts is the WebPartManager control (Note: this is in relation to ASP.NET 2.0 WebPart development, we will touch on the WSS 3.0 control responsibility for rendering shortly). This control serves a very important purpose in relation to the new technology of WebParts, mostly tailored around the serialization and de-serialization of WebPart related data since this data is transported from the database.

Starting to Create the WebPart

The first step in creating a new WebPart is to open the Visual Studio 2005 client (you can also use notepad if you wish to compile with the CSC command). You are then going to create a new project. When the project selection screen comes up, choose a class library, and then in language selection box choose either C# .NET or Visual Basic .NET, depending on yours or your client preferences. Name your project Hello World. When building SharePoint 2007 WebParts, and since it is common for the WebParts to exist within the GAC, you must modify your assemblyinfo.cs (or.vb) file in order to build the project with a signed assembly. This can also be done through the Visual Studio 2005 command prompt using the sn.exe command.

Open the class file that is located in your Visual Studio project. In this file, you will see at the top using statements (for the remainder of this article the examples will be provided in C#, but for brevity it will be imports statements or VB.NET). Through the use of adding references to this portion, will gain access to different options to inherit portions of code that would otherwise require several re-writes of code that is already placed into the framework.

Adding the Necessary References

The first reference that you will see in the default class library is the System. We will have to add other using statements in order to properly build other WebParts. Firstly, ensure the following references are made:

  • System
  • System.Web.UI
  • System.Web.UI.WebControls.WebParts

This should look like the following when added to your C# class file, located at the top before any other code (again, this should read imports …. if you are using VB.NET):

  1. using System.Web;
  2.  
  3. using System.Web.UI;
  4.  
  5. System.Web.UI.WebControls.WebParts;

Following, as explained before, we will have to change some of the inheritance so that our WebPart class will inherit out of the System.Web.UI.WebControls.WebParts namespace. Our class is called HelloWebPart, therefore we will have to create this inheritance by placing the following syntax.

Exposing SharePoint Specific Assets

For exposing certain SharePoint specific information and objects, there will have to be other references added to the project file. For the context of a Hello World WebPart, we are just facing text out of a WebPart, which doesn’t require access to the SharePoint object model, since in aggregate this is considered just an ASP.NET 2.0 WebPart. If for example, we wanted to query data out of a SharePoint list, we would have to add a reference to the Microsoft.SharePoint.dll, and then write some custom code to query the actual list. In order to setup access to the lists, you would have to first get the content of the current site as demonstrated below:

  1. SPWeb HelloWorldTestSite = SPContext.Current.Web;

Then establish a reference to the list that we wish to query:

  1. SPList Hellos = HelloWorldTestSite.Lists[HelloList];

In order to get all the items, there would have to be an enumeration set that would query through all of the objects of the list following:

  1. foreach (SPlistitem Hellos in HelloList);
  2.  
  3. //Place your query logic here

Afterwards, you could massage the data as necessary and configure your display programmatically depending on your project requirements.

The Actual Hello World Code

Afterwards, to get our WebPart to render out some simple text, we will have to create some code that will parse out our message. It should look like the following when complete:

  1. using System.Web;
  2.  
  3. using System.Web.UI;
  4.  
  5. using System.Web.UI.WebControls.WebParts;
  6.  
  7. namespace helloworld {
  8.  
  9. public class helloworld : WebPart {
  10.  
  11. protected override void RenderContents(HtmlTextWriter writer) {
  12.  
  13. writer.Write (Hello World):
  14.  
  15. }
  16.  
  17. }
  18.  
  19. }

By placing the above code, you will get a simple SharePoint WebPart that when deployed will place the text Hello World within the WebPart.

Once built, you will get a simple assembly (or binary depending on semantics used within your organization), that can deployed to a SharePoint server pending some further modifications.

Deploying the WebPart for Use With Your Portal

There are two main locations where you can place a WebPart so that you can expose it to your users within your portal. The two places where a WebPart can be placed are:

  • GAC (Global Assembly Cache)
  • /bin directory of your SharePoint instance

For some organizations, placing a WebPart assembly within the GAC is not acceptable, since assemblies placed in this directory are run with full trust, and therefore not subject to several aspects of CAS (Code Access Security), so for the sake of this article we will deploy the WebPart to the local bin of the SharePoint site.

By default, your site collection will place relevant files for your SharePoint instance into your inetpub/wwwroot directory, where there is also a bin folder located, therefore the deployment path is inetput/wwwroot/bin for our SharePoint WebPart.

We can modify the path for where our WebPart is going to be deployed by going to the properties of the project (right click on the top level solution file from within Visual Studio.NET 2005 and select properties from the fly-out), where there are a great many other options outside of the deployment path, including setting the strong key that we are going to use. Under the build shuttle, under the output path, you can change the path to your SharePoint bin directory by placing the path C:\inetput\wwwroot\bin, where the final build will be placed.

Following, and similar to the architecture of SharePoint 2003, all WebPart have to have a safe control entry within the portal web.config in order to run correctly. This is an excellent feature since if there is a problematic WebPart that exist within your environment it is simple to switch it off in order to take it out of the entire environment until a remedy is implemented making it safe again for you to run.

Getting the WebPart to Appear On The Page

The steps in order to get the WebPart on your arbitrary SharePoint page are pretty straightforward. Navigate to the site actions menu on your site and go to the WebPart gallery. Select New which will allow you to add the newly deployed WebPart by selecting the checkbox next to your newly deployed project. Lastly, select the Populate Gallery option in order to deploy the WebPart for use within the portal.

Following, when you edit a page, you will be able to select the WebPart from the WebPart selection dialog through a variety of mechanisms including SharePoint designer and the GUI.

Share

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>