Hi all,I am back again with some interesting stuff on webpart development in asp.net 2.0 and SharePoint both.
The time since I had started to work on sharepoint tools and
technology, I had always came across the requirement of building a
webpart in the sharepoint site for implementing new stuffs [like email,
calculations, displaying images etc.].
After a good long googling and R&D I came up with some discoveries,
concepts, conculsion, tricks and tacts which I am going to share with u
all.I feel u will get those things with less pain.
Ok here I start with basic defination of webpart:
Web Parts in Windows SharePoint Services provide developers with a way to create user interface elements that support both customization and personalization. A site owner or a site member with the appropriate permissions can customize Web Part Pages using a browser or Microsoft Office SharePoint Designer 2007 by adding, reconfiguring, and removing Web Parts.
The term customization implies that changes are seen by all site members. Individual users can further personalize Web Part Pages by adding, reconfiguring, and removing Web Parts. The term personalization implies that these changes will be seen only by the user that made them. Developing custom Web Parts provides an easy and powerful way to extend Windows SharePoint Services sites.
Following are some ways in which you can use custom Web Parts:
* Creating custom properties you can display and modify in the user interface.
* Improving performance and scalability. A compiled custom Web Part runs faster than a script.
* Implementing proprietary code without disclosing the source code.
* Securing and controlling access to content within the Web Part. Built-in Web Parts allow any users with appropriate permissions to change content and alter Web Part functionality. With a custom Web Part, you can determine the content or properties to display to users, regardless of their permissions.
* Making your Web Part connectable, allowing Web Parts to provide or access data from other connectable Web Parts.
* Interacting with the object models that are exposed in Windows SharePoint Services. For example, you can create a custom Web Part to save documents to a Windows SharePoint Services document library.
* Controlling the cache for the Web Part by using built-in cache tools. For example, you can use these tools to specify when to read, write, or invalidate the Web Part cache.
* Benefiting from a rich development environment with debugging features that are provided by tools such as Microsoft Visual Studio 2005.
* Creating a base class for other Web Parts to extend. For example, to create a collection of Web Parts with similar features and functionality, create a custom base class from which multiple Web Parts can inherit. This reduces the overall cost of developing and testing subsequent Web Parts.
* Controlling the implementation of the Web Part. For example, you can write a custom server-side Web Part that connects to a back-end database, or you can create a Web Part that is compatible with a broader range of Web browsers.Now as the webpart framework is build up on .Net 2.0 so its possible to built up sharepoint webpart using the same.
As per my research and extensive R&D I came up with conculsion that we can develope a sharepoint webpart in following way:- Building sharepoint webpart using asp.net webpart namespace but for that we need
to few more things in that code so that sharepoint can Identify it. - By consuming or referring the sharepoint namespace while building a webpart.
And the techniquics to built a webpart are as follows:- Using Visual Studio .NET ClassLibrary template.
- Using Visual Studio .NET WebControlLibrary template.
- Using Visual Studio .NET SharePoint Template for specifically building webparts.
Note: The above templates can be found as follows:-
Open Visual Studio .NET--> File -->New -->Project -->Under Visual C# tree check Windows Option -->in left window u will find 2 options viz:
1] ClassLibrary template; 2] WebControlLibrary.
where as for the SharePoint Template:-
Open Visual Studio .NET--> File -->New -->Project -->Under Visual C# tree check SharePoint Option -->in left window u will find webpart template.
From the above techniquics here I will pen down the WebControlLibrary option [There are slight differences].
Creating a Web Control Project
To begin, you must first create a Web Control project in Visual Studio to contain code and supporting files for the Web Part.
To create a Web control project to contain the Web Part
Start Visual Studio 2005.
On the File menu, point to New, and then click Project.
The New Project dialog box appears.
In the Project Type pane, in the Visual C# node, select Windows.
In the Templates pane, click Web Control Library.
Rename your project from WebControlLibrary1 to MyFirstWebPart.
Setting Up Your Project
Before you can start working with the code for a Web Part, you must make the following changes to your project settings:
Note:
You must make these changes for any Web Part that you create, so you might want to keep this information available for future use.
- Add the necessary references to create a SharePoint Web Part.
- Remove unnecessary code from the project template.
- Set the version number of the Web Part assembly.
- Sign the Web Part assembly with a strong name.
Adding Necessary References to Create a SharePoint Web Part
A Web Part that is used on a SharePoint page must inherit from Microsoft.SharePoint.WebPartPages.WebPart. It also uses the
System.Xml.Serialization namespace to declare the attributes of the Web Part. Therefore, you must add references to those libraries, as follows.
To add references to the project
On the Project menu, click Add Reference.
In the Add Reference dialog box, on the .NET tab, select Microsoft.SharePoint.
Note:
You can also open the Add Reference dialog box in Solution Explorer by right-clicking References and selecting Add Reference.
Click OK.
Note:
The previous steps assume that you are building the Web Part on a computer where Office SharePoint Server 2007 is installed.
You can also find Microsoft.SharePoint.dll at drive:\Program Files\Common Files\Microsoft Shared\Web server extensions\12\ISAPI.
Repeat steps 1 to 3 to add a reference to System.Xml.
Removing Unnecessary Code from the Project Template
The WebCustomControl1.cs file contains code that is created by default when you create the Web Control project.
To remove unnecessary code from the project template
Rename the WebCustomControl1.cs file to MyFirstWebPart.cs.
In the MyFirstWebPart.cs class, replace all of the code that is inserted in the file by default with the following:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
// custom added
using System.Xml.Serialization;
using Microsoft.SharePoint.WebPartPages;
namespace MyFirstWebPart
{
[XmlRoot(Namespace = "MyFirstWebPart")]
public class MyFirstWebPart : WebPart
{
protected override void RenderWebPart(HtmlTextWriter output)
{
base.RenderWebPart(output);
}
}
}
Setting the Version Number for the Project
By default, the AssemblyVersion property of your project is set to increment each time you recompile your Web Part. A Web Part Page identifies a Web Part with the version number that is specified in the web.config file. With the AssemblyVersion property set to increment, if you recompile your Web Part after importing it into a Web Part Page, the Web Part Framework looks for the version number that you specified in the web.config file. If the version number does not match, an error occurs. To prevent the version number of your Web Part from incrementing each time you recompile, you must set the version number in AssemblyInfo.cs.
To set the version number
In Solution Explorer, in the Properties node, double-click AssemblyInfo.cs.
Edit the line [assembly: AssemblyVersion("1.0.*")], so that it reads [assembly: AssemblyVersion("1.0.0.0")].
Save the project.
Signing the Assembly with a Strong Name
Web Parts are designed to be distributed over the Internet or an intranet. For security reasons, when you create a custom Web Part, you must sign it with a strong name to ensure that the Web Part can be trusted by your users.A strong name consists of the assembly's identity, a public key, a digital signature, and so on. You can use the strong name tool (sn.exe) to manage keys, generate signatures, and verify signatures. This tool is installed with the Microsoft .NET Framework software development kit (SDK) when you install Visual Studio 2005.
To assign a strong name to your assembly
In Solution Explorer, right-click the MyFirstWebPart project, and then select Properties.
On the Signing tab, select the Sign the assembly check box.
In the Choose a strong name key file list, select
Enter keypair as the file name, and clear the Protect my key file with a password check box.
Save the project.