Tuesday, June 23, 2009

Creating and Using Web User Controls in SharePoint Server Projects

Creating and Using Web User Controls in SharePoint Server Projects

Step 1:- Create Sub folder Under Control Templates folder of the SPHive Location and Name it as Techno. See the image below .
Step 2:- Create a web user Control without Code behind file, Name it as WebUserControl.ascx. Design as per your requirement and write your business Logic using Inline Coding.
I coded my page as below.
Step 3:- Now save the WebUserControl.ascx in Techno Folder as below image.



Step 4:- Add the WebUserControl.ascx as Safe Control in the web.config file of the SharePoint Web application.
Step 5:- Now develop the web Part using Visual Studio extensions for wss 1.1/1.2.
For example Name it as WebUserCntrl And Add New item as WebPart name it as VenkatWebUserControlDemo.
Step 6:- Instantiate Control object at class level.
Step 7:- Override the Virtual methods such as OnPrerender (), CreateChildControls () methods.
Step 8:- In CreateChildControls () method load the Control as below.
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace WebUserCntrl
{
[Guid("3f1050ea-4d01-4029-b1fe-c0499750f6fe")]
public class VenkatWebUserControlDemo : System.Web.UI.WebControls.WebParts.WebPart
{
Control _WebUControl;
protected override void OnPreRender(EventArgs e)
{
this.Title = "Web User Control";
this.Description = "Demo for web user control";
}

protected override void CreateChildControls()
{
_WebUControl =this.Page.LoadControl("~/_controltemplates/Techno/WebUserControl1.ascx");
this.Controls.Add(_WebUControl);
}
}
}

Step 9: - now Click on Project--> WebUserCntrl Properties as below.

Step 1 0:- Click Debug and Provide URL of the SharePoint Site under Start Browser with URL option as below.



Step 11:- Press F5
Step 12:- now the UserControl web part is Successfully added to SharePoint Site.
Now you can find out the web part under miscellaneous section.
Step 13:- open the site http://spdevserver:30000/sites/Infosys
Go to SiteActions-->EditPage-->Add a web part-->Select the Web part with the name VenkatWebUserControlDemo Under miscellaneous section.

@@Enjoy Maadi.... by venkat

No comments:

Post a Comment