Thursday, May 24, 2012

Steps to Create a Custom Webpart Pragammetically

Step 1:
Start->program-->Visual Studio 2008-->go to New--click New Project-->select visual c#-->select Class Library-->Double Click on Class1.cs file-->copy the namespace name and paste it to the class name also.
Step 2:
Add All coresponding reference to the your webpart like..

  Microsoft.Sharepoint.Search.dll      etc....
Next Add a Namespace
 using System.Web.UI.WebControls;
 using System.Web.UI.WebControls.WebParts;
 using System.Web.UI.HtmlControls;
 using AjaxControlToolkit;
 using Microsoft.SharePoint;
 using Microsoft.SharePoint.WebControls;
 using Microsoft.SharePoint.WebPartPages;
We can inherit the webpart at the first stage for example:

    public class PatnerLogin : System.Web.UI.WebControls.WebParts.WebPart
  Here we can Initialize the controls...
    Label lblStatus = new Label();
           Label lblUsername = new Label();
           Label lblPassword = new Label();
           TextBox txtUsername = new TextBox();
           TextBox txtPassword = new TextBox();
           Button btnLogin = new Button();
Step 3:
we can write the OnInit method  for example:
        protected override void OnInit(EventArgs e)

  Here we can call the method...
The purpose of this method we need to call the InitializeComponent method
Step 4:
Next we can write the InitializeComponent method for example:
        private void InitiliseComponenet()
  Here we can Declare the controls...
  for example:
     lblStatus.ID = "lblStatus";
                  lblUsername.ID = "lblUsername";
                  lblUsername.Text = "User Name";
                  txtUsername.ID = "txtUsername";
                   lblPassword.ID = "lblPassword";
                   lblPassword.Text = "Password";
                   txtPassword.ID = "txtPassword";
Step 5:
Next we can write the CreateChildControls for Example:

        protected override void CreateChildControls()

  Here we can create instands of the controls ...
  for Example:
    tr = new TableRow();
                 tc = new TableCell();
                 tc1 = new TableCell();
                 tc.BackColor = ColorTranslator.FromHtml("#8CC3FF");
                 tc.ForeColor = ColorTranslator.FromHtml("#ffffff");
                 tc.Font.Bold = true;
                 tc.Font.Size = FontUnit.Medium;
                 tc.ColumnSpan = 2;
                 tc.HorizontalAlign = HorizontalAlign.Center;
                 tc.Text = "LOGIN";              
Step 5:
Build The Solution with Release mode (Debug-mode will be Release)

Go to Root Folder : C:\Inetpub\wwwroot\wss\VirtualDirectories\MyFOlder
Mainly we can make a change to two part namely

(Webpart created Location )
1-bin (go to release folder -copy the filename.dll and paste to the root folder (MyFolder))
2-Web.config file(Here we can add the Safe Control )
for example:
<Safe Control Namespace="PartnerLogin" TagName="PartnerLogin" Safe="True"  >
Step 7:
Go to Portal site:
Go to Site Action-->Site Setting --> Webpart(Under Gallaries)-->select webpart -->PubulateGallary
If it is not the means (webpart) you can reset to IIS (Go to Run -->Type iisreset) and then Enter. It will automatically reload it.


No comments:

Post a Comment