Tuesday, May 27, 2008

Adding user control to SharePoint 2007 (MOSS/WSS) Web Part

1. Open Visul Studio 2008/2005, Add New Project> Select Empty Template name "Test.Development"

2. Add a Webpart by selcting the Webpart template Name "TestUCWebpart "

a. Create a object of User Control

UserControl _control;

b. Add following code in public class

this.ExportMode = WebPartExportMode.All;

c. Add following code in Create child control
_control = (UserControl)Page.LoadControl(@"~/_controltemplates/Mycompany/UC.ascx");
Controls.Add(_control);

d. Add following code in Render
base.RenderContents(writer);


Following is the code
*****************************WebPart code***************************************

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 Test.Development{
[Guid("4154895c-4160-4db5-adf2-849d9848a97f")]

public class TestUCWebpart : System.Web.UI.WebControls.WebParts.WebPart
{

UserControl _control;


public TestUCWebpart()
{
this.ExportMode = WebPartExportMode.All;
}

protected override void CreateChildControls() {
_control = (UserControl)Page.LoadControl(@"~/_controltemplates/Mycompany/UC.ascx"); Controls.Add(_control);
}

protected override void RenderContents(HtmlTextWriter writer)
{
base.RenderContents(writer);
}
}
}

******************************End of Webpart code**************************************


3. Add New project as ASP.NET Webapplication Name "UCWebApplication"

a. Add One Web control (.ascx) "UC.ascx"

b. Write Code desing and code

c. Add following on the top in .ascx file

//Webpart Assembly name
//Modify ascx
//Register

4. Assign Strong Key

5. Build

Following is the code

***************.ascx***********************












FL
CA
GA
TN
KY



Label1...........







**************end of ascx*******************

***************.cs***********************

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace UCWebApplication
{
public partial class UCNew : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string szState = ddlState.SelectedValue;
lblResults.Text= "Hello" + txtFirstName.Text + txtLastName.Text + txtCity.Text + szState ;

}

protected void btnReset_Click(object sender, EventArgs e)
{
lblResults.Text= "Hello" ;
txtCity.Text = "";
txtFirstName.Text = "";
txtLastName.Text = "";
ddlState.SelectedIndex = 0;
}
}

}
*************end of .cs*******************


6. Copy AScx and ascx.cs in to controltemplates//

7. copy .dll of control webapplication to C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin

8. Go to C:\Inetpub\wwwroot\wss\VirtualDirectories\80 and edit Web.config

9. Add both the assemblies in to safecontrol list



10. Add both the assemblies in asseiblies list
a.
b.

11. Make Trust

12. Webpart is ready now

1 comment:

Unknown said...

Hi,Popular image editing software applications include Adobe PhotoShop and Macromedia Fireworks with Web Design Cochin. These software applications do require some training before a new user can use them effectively.Image editing software applications allow a Web designer to create the graphics for a Web site. Thanks.....