Sunday, June 27, 2010

Creating and Deploying Customwebpart using Usercontrol in Sharepoint

3 steps To Create Customwebpart with usercontrol

1)Creating Usercontrol
2)Creating Webpart
3)Deploying the webpart

In this Scenario i creating webaprt to show current user in sharepoint portal.

To create custom webpart we should have sharepoint environment,visual studio 2005 or 2008 in our PC.

1)Creating UserControl:

Start-->Programs-->Visual studio 2005 or 2008

FIle-->New-->Website

Right Click on the website-->Add New Item

Select webuser control



Click Add

Right click on the website-->Add Reference

Go to Browse option -->select below path



C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI

IN that select Microsoft.SharePoint.dll

Click OK

Go to UserControl.ASCX.CS File

Add Namespace using Microsoft.SharePoint;

Write code page load or new method based on your requirement .

In my scenario i writing in page load to get current user .

SPWeb spWeb = new SPSite("http://jagadeeshv:9999/CMS/").OpenWeb();

//Here i am hard coding to open sharepoint weburl locally. by using this i can check local webpage to check that user control.you can user bellow statement to get crrent weburl in any type of sharepoint site

SPWeb web = new SPSite(SPContext.Current.Web.Url.ToString()).OpenWeb();
string user = SPContext.Current.Web.CurrentUser.ToString(); //To get current user
Go to Default.aspx form and add Register tag to check that usercontrol locally .
Build and run this applcaition.If you find no errors you can go for webpart to add this usercontrol in sharepoint.
Go to -->C:\Inetpub\wwwroot\wss\VirtualDirectories -->Find our application port no-->
Open that port -->
Create New folder name as Usercontrols.



Copy Usercontrol.aspx and usercontrols.ascx.cs file in our website paste into this usercontrol Folder.

Creating Custom webpart.

Start-->Programs-->Visual studio 2005 or 2008

File-->New-->Project -->Slect Class library Project

Given same name for class(.cs) to avoid the confusion



Add reference -->browse--> got to this path as like above image C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI

add microsoft.sharepoint.dll

Add Namespaces inherite webpart class write code as show bellow-->

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace samplewebparttocheckcurrentuser
{
public class samplewebparttocheckcurrentuser:WebPart
{

UserControl UserControl; \\Creating using control
protected override void Render(HtmlTextWriter writer)
{
UserControl.RenderControl(writer); (\\rendenring that usercontrol into shrepoint webpage)
}
protected override void CreateChildControls()
{

\\ Giving virtual path to add usercontrol into this webpart.Actuall this path
referces C:\Inetpub\wwwroot\wss\VirtualDirectories\9999\Usercontrols\WebUserControl.ascx file.

UserControl = (UserControl)Page.LoadControl(@"/Usercontrols/WebUserControl.ascx");
Controls.Add(UserControl);

}


}
}

Right Click on that project -->Select Properties-->

Go signing option--> select sign in assembly check box-->

select dropdown-->Click new -->Give key name like (sample)here no need any extensions

deselct Protect my key file with a password.



Build the project .It shouldn't contain any errors.

Go to -->project path -->



Go to bin-->Debug-->Find dll with project name.

Deploying Webpart:

Go to C:\WINDOWS\assembly drag or copy that dll into assmbly folder.

after copy right click on dll -->go to properties-->copy name and public key token.


Go to-->web.config using this path C:\Inetpub\wwwroot\wss\VirtualDirectories\9999

open web.config file -->
add as a safe control.
go to safecontrol section -->









Here give assembly and namespace both are same because we ahve given class name and project name are same.

give the copied dll name in two places(assembly and namesapce) and give public key token

Save web.config file.

Go to Sharepoint sie

Site Actions-->Site settings-->

Under the gallery section-->Find webparts

Click on that webparts-->Wepart gallery will open

In the webpart gallery-->New-->in the new webparts -->

find your webpart with the dll name-->select that webpart -->



select overwrite if already exists option. and click on populate gallry.

Now got to sahrepoint webpart-->Edit page-->

Add webpart-->In the miscellaneous section your webpart will appear-->

select that webpart click on ok-->

that webpart will display with current user name like below -->

No comments:

Post a Comment

Content Editor Webpart not showing up in SharePoint Online ------------------------------------------------------------------------------...