Monday, July 27, 2009

Create a sharepoint webpart

Steps:
1. Create a simple webusercontrol . Put necessary code and controls in it.(For ex, datagrid and code to connect database.) Do not maintain separate code file (ascx.cs) for web user control. Put code in same ascx file only.
2. Put the webusercontrol (.ascx file) in layout folder of sharepoint site where you want to deploy the webpart.
3. Go to VS2008. (Please install VSSWSE.exe before that.)
4. Create new Project -> Select webpart from Visual C# -> sharepoint
5. Add following code :
protected override void CreateChildControls()
{
base.CreateChildControls();
// TODO: add custom rendering code here.
Control objControl = Page.LoadControl("/_layouts/WebUserControl.ascx");
this.Controls.Add(objControl);
}
6. Build and deploy the solution.
7. Go to the sharepoint site -> Site Actions -> EditPage ? Add Web Part.
8. Select your webpart from the list.

No comments: