How to add a custom user control to a SharePoint page

by Melyssa Bell September 18, 2008
One way to get “home grown” .Net development in a SharePoint site is to use custom user controls (.ascx). Following are the general steps involved with making user controls function on a SharePoint site:
 

1. Start a new Web Site in Visual Studio.

2. Add a new user control page to the project - you may either include the code inline or as code-behind.

3. Create the user control and place all code between the script tags on the page (inline) or in the code-behind file.

4. For inline code, references to other assemblies should be placed outside the script tags and take the format:

  • <%@ Import Namespace=”System” %>

5. Custom assemblies (class libraries, etc.) should be placed in the bin directory of the Web application hosting the user control.

6. Save/compile the .ascx file (and .cs file if code-behind), then copy and paste it/them into the following location: C:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/CONTROLTEMPLATES/

7. Create a new folder in the features directory and give it the same name as your user control: C:/Program Files/Common Files/Microsoft Shared/web server extensions/12/TEMPLATE/FEATURES/

8. Create two new files inside this new folder. Name one feature.xml and the other elements.xml.

9. Open feature.xml for editing. Add the following xml to the file (replace the red bold text with the user control information):

  • <?xml version=”1.0″ encoding=”utf-8″ ?>
    <Feature Id=”5DD8CC0A-0EF5-4c14-8052-573E14021D7A
      Title=”User Control Title
      Description=”User control description.
      Version=”1.0.0.0″
      Scope=”WebApplication“  xmlns=”http://schemas.microsoft.com/sharepoint/“>
      <ElementManifests>
        <ElementManifest Location=”elements.xml” />
      </ElementManifests>
    </Feature>

10. Open elements.xml for editing. Add the following xml to the file (replace the red bold text with the user control information):

  • <?xml version=”1.0″ encoding=”utf-8″ ?>
    <Elements xmlns=”http://schemas.microsoft.com/sharepoint/“>
      <Control
        Id=”aUniqueId
        Sequence=”100″ ControlSrc=”~/_controltemplates/Filename.ascx”>
      </Control>
    </Elements>

11. Install the feature. Run the following command from the console (you must be in the “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\” directory first; replace the red bold text with the user control information):

  • stsadm -o installfeature -filename FolderName\feature.xml

12. Activate the feature. Run the following command from the console (you must be in the “C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\” directory first; replace the red bold text with the user control and scope information):

  • stsadm -o activatefeature -filename FolderName\feature.xml -url http://servername

13. Start SharePoint Designer. Open your Web site and the page on which you want to add the user control.

14. Add the following line of code to the PlaceHolderMain content place holder (replace the red bold text with the user control information):

  • <SharePoint:DelegateControl runat=”server” ControlId=”theUniqueId“></SharePoint:DelegateControl>

15. Save the file and open in browser.

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Search Blog

WELCOME TO OUR BLOG

Fusionovation is an industry blog written by Smooth Fusion team members that serves to inform marketers of cutting-edge digital topics and trends.

Smooth Fusion specializes in helping marketers with the planning and implementation of digital campaigns and ongoing programs. We are a trusted partner to marketing agencies and corporate marketers worldwide, delivering our unique blend (or fusion) of technical expertise and understanding of the needs of marketers.

Our team is made up of consultants, project managers, software engineers, developers, designers, and quality assurance specialists. And what we all have in common is experience using technology to empower marketers. Learn more about Smooth Fusion.