Thursday, November 12, 2009

WebPart Creation & Deployment

Custom Web Part Creation and Deployment

Creating Web User Control in VS2005 or VS2008.
Step 1: Create new project (Web application Project)
Note: Do not use new Web Site when creating project.
Step 2: Name the Project with good name. Since this becomes our DLL.
Step 3: Create Web User Control, with complete UI and Logic.
Step 4: Build the project. Which will create a DLL.

Create Web Part Project Library:
This project is our Web part Library. Usually we will create only one Library project for all the web parts we develop.
Step 1: Open new dev VS2005. Create New Library Project.
Step 2: Name the project with Good name. (This becomes Dll)
Step 3: Add a reference system.web to the project. This contains all the related name space for web part to work in share point.
Step 4: Add a Class. Name it. This becomes the WebPart.
Step 5: Import below name space.
using System.Web.UI.WebControls.WebParts;
Step 6: Inherit WebPart Class.

namespace BasicWebParts
{
public class HelloWorld:WebPart
{

}
}
Step 6: Override Create control method .And modify the code to reflect the user control file name.



Example class code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;

namespace BasicWebParts
{
public class UserControlWebPart:WebPart
{
Control cntrl;
protected override void CreateChildControls()
{
this.cntrl = this.Page.LoadControl(@"~/usercontrols/ucDisplay.ascx");
this.Controls.Add(this.cntrl);
}
}
}

Change only the ucDisplay.ascx only to your user control.

Step 7: (Important)
Add the below line to AssemblyInfo file located under Properties folder .
[assembly: System.Security.AllowPartiallyTrustedCallers]
Better Add it at the end.

Information: This related to security issue while inporting the Webpart by sharepoint.

Step 8: Creating snk file.
1.Open Application Properties as shown below Image.



2. Click on signing  Choose Strong name key  as shown in below.



3. Enter Project Name as snk file name and uncheck the Protect my key file with password.
4. Click ok. And save the changes. snk file got created.

Step 9: Build the Project. Get the Dll.
Step 10: Extract public Key token.
Open visual studio 2005 command prompt.
Enter sn –T “Path of the dll”
Example: sn -T “C:\Project1\bin\debug\Test.dll”
Note the Public Key Token: it will be like example “fa29fd297c3b296b”
Step 11:
Add Safe Control Tag in Web.config of the site. To find web.config go to IIS server virtual directory and explore the same.
By Default Location is like : C:\inetpub\wwwroot\wss\virtualdirectories\siteport
Open Web.config . Copy paste one safe control tag from their and modify the Assembly PublicKey token, Namespace.
Example:


Step 12: Deploy dlls and supporting files.
1. Deploying User Control s
2. Create a folder “usercontrol s” in the site root .
Assume 1001 is the site port we created. Create the directory like below
C:\inetpub\wwwroot\wss\virtualdirectories\1001\usercontrols.
Step 13: Place ASCX controls to this folder.
Step 14: Place WebpartDlls, WebUserControl dlls created from above steps into bin folder.

Adding web part to the site:

Step 1: Open site - Click Site Actions - Site Settings - Webparts  Webpart gallery will open
Step 2: Click New . New ly created webpart are displayed. Check the web part to add into current site.
Step 3: Add web part from web part pane to any webpart page.

Note:
rest of the activities whatever functionality/design we can change as it Usercontrols update to reference folder.

Copyright © 2009 Angel