Helloworld webpart in SharePoint
This is my first experience of creating and deploying web part in SharePoint. For creating any SharePoint application in visual studio 2008 you need to install Visual Studio 2008 extensions for Windows SharePoint Services 3.0. You can download this from here. After installing this extension you are getting project type SharePoint in your visual studio.
For creating new webpart you need select webpart template in sharepoint project type. I am giving helloworld name to my solution. Once I create project it ask for trusted level of web part. Depends upon accessibility model you need to give trusted level. If webpart accessible to all sharepoint sites then you need to give Full Trust (deploy to GAC). If webpart made for specific site then you need to give Partial trust (Deploy to bin). When you are creating partial trusted level then you need to add custom code access policy for webpart. For this you can make following entry in your site web.config file.
I had selected full trusted web part. Now I am getting following files under hellowworld folder while I create web part for sharepoint.
Helloworld.cs: All code behind logic include in this file. All webpart life cycle event are include in this file.
Helloworld.webpart: This file contains webpart’s defination. The type element must contain a name attribute which must be the full name or Guid of web part class and importErrorMessage contain an error message that is displayed if the import of the web part fails.
Helloworld.xml: this file defines what webpart contained within feature and where to deploy them. The Url attribute pointing to '_catalogs/wp' means that it will put all the defined web parts in the Web Part Gallery. The Path attribute declares where to find the web part file definition relative to the feature.xml file. The Url defines what the web part file definition will be called in the Web Part Gallery.
Now I am creating a label called lblMessage and adding some text in it. Now I am add this control at runtime into the web part. The control is rendered when CreateChildControls is fired.
Now I built the solution. Everything is fine now. This is how web part created. In next section I will tell how to deploy web part.
| Source Code: HelloWorld web part |
| Date:- 21 October, 2009 |