Click here to Skip to main content
Click here to Skip to main content

Custom UserControls/WebParts in SharePoint/MOSS 2007

, 22 Apr 2009 CPOL
Rate this:
Please Sign up or sign in to vote.
This article describes the way to use custom webpart/usercontrol in SharePoint/MOSS.

Introduction

When I started navigating SharePoint, I found it quite difficult to understand in terms of the underlying concept. A lot of resources are there throughout the Internet to start with any of the topics. But as always, I believe in making things simple to learn and understand. Here I am going to explain the process of implementing a Custom user control developed in Visual Studio 2005/2008 in SharePoint/MOSS 2007.

Prerequisite

  • Basic familiarity with WSS/MOSS
  • Experience of using WebUserControls and class libraries in .NET
  • MOSS Server Installation
  • A Web Application in MOSS to test our Project
  • Visual Studio 2008 or Visual Studio 2005 having Microsoft.SharePoint DLL Smile | :)

Hope all my readers satisfy these prerequisites, if not then also I would try to make it work for you.

Problem Scope

I want to use a Simple WebUserControl in MOSS site. We can call it a Custom Web Part.

This web user control is made in Visual Studio 2008 Web Site using C#. You can make this control as complex as you need because it really doesn't affect my process of implementing it on MOSS web application. Smile | :)

Description

Open Visual Studio, go to C# Project and Add a Blank Solution named “MOSSWeb”.

cd1.JPG

Now add a new WebApplication named “MossWebDemo” to the Solution. Create a WebUserControl named “uclDemo.ascx”.

Now again from the Solution Explorer add a new Project of type “ClassLibrary”, name it “MossImplementation”. Rename the class named Class1.cs to “HostDemo.cs”.

Your Solution Explorer should look like the following:

cd2.JPG

Let us focus on uclDemo.ascx as of now. Code here with complete freedom and forget this control is to be used in MOSS. What I mean is you can code anything, like a Database operation etc. I will consider a database table and will fetch records on a GridView. The database is not the default MOSS database. Create your own database. And make a table “Employee” with your desired fields, put some records in it.

Let's store Database connection string in Web.config of the application as follows:

<appSettings>
<add key="ConStr" value="Database=YourDatabaseName;
	Server=ServerName;User=UserName;Password=Password;"/>
</appSettings>

I will not go into the details of writing the code to populate a GridView from a database table, I hope it's basic for the targeted audience. For details of that, you can download the code. Let's assume that our uclDemo.ascx is ready. Drop it on a Web Page and test it. After testing, go to your Moss Server on which you want to implement this control.

Open C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES.

This is 12 Hive where in ControlTemplates folder, controls are physically placed.

Here you can directly drop uclDemo.ascx and uclDemo.ascx.cs, or you can create a folder and then drop these in that folder. Best practices advice to create a separate folder. So let's create a folder named “MyControls” and drop the controls here.

C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\
	TEMPLATE\CONTROLTEMPLATES\MyControls

Let's go to class library project, open the class named HostDemo.cs and copy the following code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; 
namespace MossImplementation
{
  [DefaultProperty("Text")]
  [ToolboxData("<{0}:MossImplementation runat="server"></{0}:MossImplementation>")]
  public class HostDemo : System.Web.UI.WebControls.WebParts.WebPart
  {
    private Control _MyUserControl;
    protected override void CreateChildControls()
    {
    _MyUserControl = 
	this.Page.LoadControl(@"~/_controltemplates/MyControls/uclDemo.ascx");
         this.Controls.Add(_MyUserControl);
    }
  }
}

Before moving further, let's have a quick look over the code.

This class is being inherited from System.Web.UI.WebControls.WebParts.WebPart.

_MyUserControl is a control object which will hold our custom control from 12 Hive.

We will override the CreateChildControls() event and will add our custom control to the current page. That's it, isn't it simple !! Smile | :) .

Our DLL is ready, just build it. Add a strong name to the DLL, after rebuild copy it to the Server GAC. Oh.... I think I may be going very fast for starters. Ok, let's do it.

Don't forget to add a reference to System.Web as we are using Control object in our Class Library. Build the application now.

Register your DLL from the bin folder of MossImplementation project to Server GAC, physical location of which is C:\WINDOWS\assembly. You can either drag the DLL here or use the VS command prompt as follows:

Gacutil /i (PathofYourDLL)
Like : gacuitl /i 
	E:\CodeProject\MOSSWeb\MossImplementation\bin\Debug\MossImplementation.dll

Look for message “Assembly Successfully Added to GAC”.

Now look for the MOSS web application on which you want to use this WebControl. Let's suppose it is http://myserver:1000.

Go to C:\Inetpub\wwwroot\wss\VirtualDirectories and look for your port folder i.e., 1000. Open web.config of that folder. It's the web.config of your MOSS application so be very careful. Don't make any mistake in the update.

Here we need to make two updates.

  1. I want to make the MossImplementation DLL as a safe control, so that the web application can use it. Go to GAC location, locate your MossImplementation DLL. Right click properties, and get the following properties of that and add entry to the <SafeContols> of the Web.Config. For example:
    <SafeControl Assembly="MossImplementation, Version=1.0.0.0, Culture=neutral, 
    	PublicKeyToken=593570f0e9c6db21" Namespace="MossImplementation" 
    	TypeName="*" Safe="True" />
  2. If you are using Connection String from Web.Config in uclDemo.ascx then add that string to the <appSettings> of this Web.Config. Save your web.config.

Now you are ready, open your MossWeb Application on the Web Browser.

Go to Site Settings, Web Part Gallery.

Click New ? look for your uclDemo.ascx and upload it.

Now go to any page of your site. Site settings, edit page ? Add WebPart and add your uclDemo WebPart. Hope it will work as per your expectation. In case of any query, please get back to me.

Happy coding......

History

  • 23rd April, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Kamal Singh Kharayat
Technical Lead Government of Dubai
United Arab Emirates United Arab Emirates
Hi, I own Bachelor and Master in Computer Application. As a habit of learning I always use to learn new technologies. I own keen interest in self learning, specially in Microsoft .net technologies. In sports, I like to play Soccer. I own 6 year of Software Development experience in .net technologies and including 3 year in WSS and MOSS 2007.
Also I own experience of more than 1 years as a Technical Trainer for .net technologies in various IT Companies.
Currently I am working an independent IT Consultant.
We are open for assignments in SharePoint & Microsoft .NET technologies. Contact us for SharePoint Corporate Training.
 
"Improvement is the only thing which makes me happy...."
e-mail Addresses: kamalkharayat@gmail.com
skype id: stek_ks
http://kamalsinghtechriders.blogspot.com/
Follow on   Twitter   Google+

Comments and Discussions

 
Generalone issue PinmemberGaurav-Mehta3-Jul-09 5:39 
GeneralRe: one issue PinmemberKamal Singh Kharayat22-Jul-09 22:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.141015.1 | Last Updated 23 Apr 2009
Article Copyright 2009 by Kamal Singh Kharayat
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid