Wednesday, October 27, 2010

How to create Custom webpart in sharepoint

Createproject

To Get Started with creating Custom Web Part for MOSS 2007 in Microsoft Visual Studio 2008, Open the IDE and create a new C# project, Select Class Library as Project Type. Name It.

Add references to System.Web, Microsoft.Sharepoint.dll






Extend the MyNewWebPart Class from the WebPart Class ( System.Web.UI.WebControls.WebParts.WebPart), and add the code as shown below. The CreateChildren Control would create and add controls to current class. In the below coding I have only added a WebControl.Calender Object. The RenderControl Method is an override for the WebPart Base Class and calls the RenderChildren Method, this causes the Children Controls to be rendered on the Particular HtmlTextWriter passed as a parameter to the method.




Now, strongly name it, Build the dll, and use sn-t or reflector to find the public key token.



After that follow the below steps to deploy

1) Now you have to deploy to your website, so locate the Sharepoint WebSite you wish to deploy this webpart in. This can be done via the IIS Manager under administrative tools. The default ones would be virtual directories that look like GUIDs under "C:\INETPUB\WWROOT\WSS\".

2) Place the dll under the bin directory in the virtual directory.

3) Under the same virtual directory, find the web.config, and add the following to the SafeControls section -

<SafeControl Assembly="ClassLibrary2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=77dfsdf234933833"Namespace="ClassLibrary2" TypeName="*" Safe="True" AllowRemoteDesigner="True" />

Insted of above step you can also directly deploy it in the Assembly if you are an Administrator, and add the SafeControl in webconfig file.

4) Then browse to the WebSite, and go to Site Actions -> Site Settings.


5) On the site settings page under Galleries column click on the Web Parts




6) Click on "New" in the toolbar,as shown below -




7) Find the ClassLibrary2.MyNewWebPart



8) Now switch to 'Edit Mode' of the page where you want to add the calender control,then click on Add a webpart, you should see it in the list that pops up. Add the MyNewWebPart, and under Miscellaneous.

9) Click 'Exit Edit Mode' you will find the output as below.



Source :kethare.in

No comments:

Post a Comment