Saturday, February 13, 2010

How To Develop And Deploy Basic Sharepoint Webpart Step By Step



Web Parts are the building blocks of pages in SharePoint sites. Users of SharePoint sites can make use of those building blocks to determine what should be displayed on a specific page in a particular SharePoint site. When you install SharePoint, you can make use of some out-of-the-box Web Parts straight away. Depending on whether you have Windows SharePoint Services (WSS) or Microsoft Office SharePoint Server (MOSS) as your SharePoint installation, you'll have more or less. Additionally, every SharePoint list and document library will have a Web Part counterpart that can display the contents of the corresponding list or document library.

But, the out-of-the-box Web Parts are not the only ones that you can use! Developers can build their own Web Parts as well and deploy them to the SharePoint server. End users won't notice the difference between the custom Web Parts and the out-of-the-box Web Parts, so Web Parts are a great way to extend SharePoint.

So let’s go through the basic steps to create your own Web Parts in various ways.


Step 1)
 A Web Part in code is just a normal .NET class, nothing more, nothing less. In Visual Studio, you can make use of the Class Library project template to write code for the Web Part class. This code will be compiled into a .NET assembly, in this case a DLL that is exactly what you need. When Visual Studio is started, create a new project and select the Class Library template as shown in below figure. The name of the new project is important, so think carefully when you choose a project name. Your project name should be unique on the server on which you would like to deploy your Web Parts. Additionally, the project name will be used later when the Web Parts are deployed. Also, be aware that names in .NET are case-sensitive!




A common practice to ensure unique names in .NET environments is using namespaces. If the project name is, for example, MVP.Book.WebParts, by default, all the code will be sitting in the namespace with exactly the same name. Every Class Library project in Visual Studio can contain any number of Web Part classes, so think of the project as the container of your Web Parts.

Step 2) when the new project is created, there will be one class already available: Class1.cs. In general, Class1 is not a good name for a Web Part, so rename the class to HelloWorld. The special thing about a Web Part class is the fact that the class inherits from a specific base WebPart class. This base WebPart class is available in the System.Web assembly as shown in below figure. By default, the Class Library project doesn't contain a reference to this assembly, so you should add it yourself. Right-click the project node in the Solutions Explorer window and choose Add Reference. Next, select theSystem.Web assembly from the list.


Now the HelloWorld class can inherit from the WebPart that is available in the System.Web.UI.WebControls.WebParts namespace class.

All Web Part classes must be scoped public. A Web Part class that is not marked as public can't be used in SharePoint Web Part pages. In C#, when a new Class Library project is created, the default class Class1 is automatically set to the public scope. Unfortunately, classes that are added to the project are not scoped public automatically; the developer must add the public keyword manually!

Step 3) A Web Part developer must only focus on what should happen inside the Web Part. The contents displayed inside the Web Part should be generated in the Render method of the Web Part class. This method is already implemented in the base WebPart class, so it should be overridden in the HelloWorld WebPart class. This is the default implementation:




The only parameter of the Render method is the writer parameter, which is of the typeHtmlTextWriter. You should use this parameter to write HTML that will be rendered inside the Web Part. It's important to write valid HTML with the writer parameter, because invalid HTML is accepted as well, so it could break the Web Part page. The HelloWorld Web Part should display fixed text, which is formatted as a title:


It's not recommended to write HTML tags in plain strings as displayed in the previous example. Later in this article, I discuss the proper way to generate HTML. The purpose of this example is to show the real basics.

For now, the code is sufficient. It is a Web Part that displays fixed text.
The complete contents of the Class1.cs looks like this:




In this example, the Web Part code generated HTML by using a string. It is quite obvious that generating HTML for more complex user interfaces can be complicated. This section examines using controls in Web Parts to simplify the generation of HTML code in Web Parts:

The .NET Framework has lots of Web controls that can be used in Web Parts, including Button,Textbox, Label, and so on. Most of those user controls are residing in theSystem.Web.UI.WebControls namespace. The advantage of these ASP.NET Web controls is that they can generate HTML for themselves.








Step 4) Give your assembly a strong name. Just go into the Project Properties and then go to the "Signing" tab as shown in below figure to do this. After giving a key name click on OK , also give password for your assembly if you want more security otherwise clear checkbox that mention "Protect my key file with a password.

Build the project and on successful built you are ready to Deploy the Web Part to the Portal Site.

Deployment:

Step 5) Deploy the assembly to the Assembly Folder (GAC) (requires the assembly to be stron named). Drag and Drop the assembly (dll) file, 
named BasicSimpleWebpart.dll from source folder to the Gac Folder (C:\WINDOWS\ASSEMBLY). The Assembly (dll), BasicSimpleWebpart.dll will be copied in to GAC folder as shown in following image.



Step 6) Now take the public key token for the BasicSimpleWebpart.dll  assembly (dll), select the assembly file from GAC and then click mouse right button, and select the properties. The properties will open and then select the public key token, copy it, and paste the key into any text file.

Step 7) Open the web.config file of the SharePoint application from the following path.

C:\Inetpub\wwwroot\wss\VirtualDirectories\80
Add the following attribute, in the last of element , by replacing the properties of BasicSimpleWebpart.dll as per required in following code of line.

Step 8) Now Copy and paste the same BasicSimpleWebpart.dll assembly (dll) to bin folder of the sharepoint application, the path is as follows.

C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin.

Step 9) Run the SharePoint application and open the Home Page and go to SiteAction->SiteSetings, then click the on the Modify all site Settings link.

Click on the Web Parts link under the Galleries section.



Step 10) After opening the web part gallery click on new button, highlighted in the following figure.


Select the checkbox of BasicSimpleWebpart.dll web part, and click the button Populate Gallery.




Now web part gallery will open again with the name of our new web part, highlighted in the following figure.


Step 11) Now open the site where this web part needs to be deployed.And Click on SiteAction->EditPage.


 Step 12) Now click on “Add Web Part” link, 
 A New window will be open “Add Web Part to Main”, and then go to ‘Miscellaneous’ section and select the checkbox of the HelloWorld web part, then click ‘add’ button as 
shown in the following figure.




Now “Exit Edit Mode” comes under the “SiteSection”. The Web part now appears on the screen.



Enjoy Sharepoint Webpart :)
Dipti Chhatrapati