Click here to Skip to main content
13,898,048 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


24 bookmarked
Posted 11 Nov 2009
Licenced CPOL

SharePoint - user control as a Webpart

, 11 Nov 2009
Rate this:
Please Sign up or sign in to vote.
SharePoint - user control as a Webpart


In my first article of SharePoint webpart

(Developing and Deploying Custom Web Parts for SharePoint Portal 2007) CustomWebpartSharepoint.aspx

I have discuss how to make the custom web part, in that case you have to make the class library project and do all work at code behind, even you have to write the code to render the controls. This really a very cumbersome and time taking task. But not need to worry at all, SharePoint has introduce one new way to make the webparts, which is very easy, in that you just need to make user control in and deploy that usercontrol in SharePoint, so my this article will show you all steps of development and deployment of usercontrol.

B. Developing

1. To get started with creating Usercontrol in Microsoft Visual Studio 2005, Open the IDE and create a new C# project, Select web application as Project Type. Name It as UserControl_WebPart.

2. Add the usercontrol (ascx) and web page (aspx) in the project and provide the name for usercontrol – ‘UsercontrolWebPart’, now do what you want to do with, I have just putted the Login control in my example. After that test your usercontrol by running it with the web page.

3. Add the reference of Mirosoft.sharepoint.DLL to the project.

4. Add new class into the project named – UCwebpart.cs, which basically deploy the Usercontrol into the SharePoint.

5. Following is the description of the code.

a. A a. add following these two Namespaces.

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebPartPages;

b. Inherit the class from the WebPart class.

public class UCwebpart : WebPart  

c. Provide the following two variables, which provide the address of the usercontrol.

private UsercontrolWebPart usercontrol;
private string error = "";
private string Pathofusercontrol = @"/";

d. Add the method ‘CreateChildControls’, which load the usercontrol in the sharepoint.

protected override void CreateChildControls()
               string tooldir = this.Page.Request.MapPath(Pathofusercontrol);
               string[] textArray1 = null;
                     textArray1 = Directory.GetFiles(tooldir);
               catch (Exception exception1)
                    error += string.Format(exception1.ToString());
                usercontrol = (UsercontrolWebPart)this.Page.LoadControl(Pathofusercontrol + @"UsercontrolWebPart.ascx");
            catch (Exception CreateChildControls_Exception)
                error += "CreateChildControls_Exception 2: " + CreateChildControls_Exception.Message + " - Path is" + Pathofusercontrol.ToString() + "--End";

            }//end catch

e. Add the methid ‘RenderContents‘, which render the contents of the usercontrol.

protected override void RenderContents(HtmlTextWriter writer)
             catch (Exception RenderContents_Exception)
                error += RenderContents_Exception.Message;
                if (error.Length > 0)

6. Give your assembly a strong name. Just go into the Project Properties and then go to the "Signing" tab (see Figure 1) to do this. Alternatively, you can use the sn.exe command line tool to generate strong names for you.



7. Add new one xml file to the project and provide the following contents to it.

<?xml version="1.0" encoding="utf-8" ?>

<webPart xmlns=""> 
		<type name="UserControl_WebPart.UCwebpart, UserControl_WebPart, Version=,Culture=neutral, 
		PublicKeyToken=#################" /> 
		<importErrorMessage>Cannot import this Web Part.</importErrorMessage> 
			<property name="Title" type="string">Web Part</property> 
			<property name="Description" type="string"> 
			<property name="ChromeType">TitleOnly</property> 
			<property name="ChromeState"><city w:st="on" /><place w:st="on" />Normal</place /></city /></property> 
			<property name="ItemLimit" type="int">15</property> 
			<property name="ItemStyle" type="string">Default</property> 

NOTE: change the value public key token, which you will find when deploy the dll into the GAC folder. 

The final contents are here of the project.

C. Deployment

1. Deploy the assembly to the Assembly Folder (GAC) (requires the assembly to be strong named). Drag and Drop the assembly (dll) file, Named UserControl_WebPart.dll from source folder to the Gac Folder (C:\WINDOWS\ASSEMBLY).

2.The Assembly (dll), UserControl_WebPart.dll will be copied in to GAC folder.

3. Now take the public key token for the UserControl_WebPart.dll assembly (dll), select the file first and then click mouse right button, and select the properties.

4. The properties will open and then select the public key token, copy it, and paste the key into any text file.

5. Drag Drop the UsercontrolWebPart.ascx User Control.


6. Open the web.config file of the SharePoint application from the following path.


7. Add the following attribute, in the last of <sharepoint><safecontrols> element.

<SafeControl Assembly="UserControl_WebPart, Version=, Culture=neutral, 

<p style="TEXT-ALIGN: justify">PublicKeyToken=93b5b0fffa462560" Namespace="UserControl_WebPart" TypeName="*" 


<p style="TEXT-ALIGN: justify">Safe="True"/> 


8. Now replace the public key token, copied from GAC, with the existing key highlighted in yellow color.

<SafeControl Assembly="UserControl_WebPart, Version=, Culture=neutral, 

<p style="TEXT-ALIGN: justify">PublicKeyToken=93b5b0fffa462560" Namespace="UserControl_WebPart" TypeName="*" 


<p style="TEXT-ALIGN: justify">Safe="True"/> 


9. Run the SharePoint application and open the Home Page and go to SiteAction->SiteSetings, then click the on the Modify all site Settings link.

10.Click on the Web Parts link under the Galleries section.

11. After opening the web part gallery click on new button, highlighted in the following figure.

12. Click on the upload button.

13. A new screen window will appear, where you have to provide the XML file, which you have created in the project before, and yes please change the value of attribute PublicKeyToken in the xml file.

14. After clicking on ok button of the previous screen, again new window will appears, where you can provide the value of webpart.

15. After clicking on ok button, you will go back to the webpart gallery, where you can check that your web part name appears with small new icon.

16. Click on SiteNavigation->EditPage.

17. Now click on “Add Web Part” link, as shown in the following figure.


18. Then a new window will appear from where you can add your web part.


19. Mark the check box of your web part and click on add button, after you will go back to you page, where you will find your web part.



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


About the Author

Technical Lead
India India
Gaurav Mehta is from Delhi, India. He has to his credit, a Bachelor’s Degree in commerce and a Master's in Computer and Software Applications. He started his career with C and C++ and then moved on to Microsoft Technologies. He has been in this industry for more than 6.5 years. Gaurav is primarily involved in requirement analysis; designing and developing web and win apps.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 4 Pin
Manoj Ahlawat5-Jul-10 20:46
memberManoj Ahlawat5-Jul-10 20:46 

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

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

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190306.1 | Last Updated 11 Nov 2009
Article Copyright 2009 by Gaurav-Mehta
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid