in

SharePoint Blogs

The Best Place for SharePoint-related Blogs

Tobias Zimmergren's thoughts on development - MOVED!

THIS BLOG HAS MOVED!!!
NEW URL: www.zimmergren.net
Please update your RSS and bookmarks!

Any comments here will not be answered, please comment on http://www.zimmergren.net instead!
All the same posts are there

MOSS 2007: Using AJAX UserControls in SharePoint

Author: Tobias Zimmergren
Url:
http://www.sharepointblogs.com/zimmer

This post merely shows you how you can use the AJAX functionality in SharePoint by using UserControls.

What we need to be able to follow the steps in this article

In the following sections I will describe how you an use the SmartPart by Jan Tielens to host your custom usercontrols in SharePoint. The SmartPart is widely used by a number of people. However, there's also the approach of writing your own wrapper if you want full control of your usercontrol wrapper. Liam Cleary has blogged about doing so here.

If you've checked the aforementioned requirements, let's get started

First off, we'll install the AJAX Control Toolkit

Add the AJAX Control Toolkit to your SharePoint Server

Download the AJAX Control Toolkit from here and extract all the files into any folder. Search for the AjaxControlToolkit.dll file and make sure you copy it to the same webapplication's /bin directory as you configured for AJAX use. This means that you simply copy the AjaxControlToolkit.dll file to e.g. C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin if that's the webapplication you've configured AJAX for.

It might look something like this:

We're ready for some development, create a new project
  1. Launch Visual Studio
  2. Create a new Web Site with ASP.NET AJAX Support


    Note: The only reason that I create an entire Web Site project is so I can test my UserControls and debug easily on my own development machine before I actually deploy them.
  3. Create a new Web User Control by adding a new .ascx file to your project (I usually delete the .cs file and the reference to the CodeBehind for easy deployment if there's no sensitive code in order to limit the UserControl to one file with embedded code)
  4. It could now look something like this

Add the AJAX Control Toolkit to your Visual Studio toolbox
  1. Right click in the Toolbox and choose "Add Tab" and choose a name for the new section. (My choice was "AJAX Control Toolkit")
  2. Right click inside the newly created tab, and click "Choose Items..."
  3. Press "Browse" and navigate to your AjaxControlToolkit.dll which you've got somewhere on your machine by now and then click "Open"
  4. You now have a Toolbox filled with the AJAX Control Toolkit controls to use within your project.

Add a reference to the AJAX Control Toolkit in your UserControl
  1. Switch to designview of your UserControl
  2. Dragging any chosen AJAX Control or Control Extender onto the UserControl will automatically add the reference to the correct Namespace for AJAX in your UserControl. You might want to change the TagName to something more useful than cc1 as it's named automatically.

     
Create a simple Hello World UserControl
  1. Drop a TextBox in the UserControl
  2. Drop a "CalendarExtender" in the UserControl - It's found in the AJAX Control Toolkit toolbox 
  3. Set the CalendarExtender's TargetControlID property to the ID of the TextBox control.
    The code might look something like this:

    <%@ Control Language="C#" AutoEventWireup="true" %>
    <%@ Register Assembly="AjaxControlToolkit"
                 Namespace="AjaxControlToolkit"
                 TagPrefix="ajaxToolkit" %>
  4. <script runat="server">
        // If there were any server side code, it would go here since I don't use a code behind or pre-compiled assembly to host the code
    </script>

    Pick a date by clicking in the textbox!<br />
    <asp:TextBox ID="TextBox1" runat="server"/>
    <ajaxToolkit:CalendarExtender ID="CalendarExtender1" runat="server"
    TargetControlID="TextBox1" />

    The UserControl might look something like this when you add it to a page and run:


  5. If you're happy with this very simple Hello World UserControl which uses the AJAX Control Toolkit to extend a Calendar popup, move on to the next section :)
Deploy the UserControl to SharePoint
  1. Copy your newly created UserControl (in my case, it's called ZimmerAJAX.ascx)
  2. Paste the UserControl to C:\Inetpub\wwwroot\wss\VirtualDirectories\80\UserControls (assuming you're deploying on the standard port 80 site)
    Note: The UserControls directory might not exist at this point, then you'll have to create it manually. The reason that this folder needs to exist is that the SmartPart which will host our UserControl is looking for controls placed inside that folder, unless you're creating your own UserControl Wrapper WebPart, as Liam Cleary explains here

When you've copied the file to the UserControls directory, the SmartPart will automatically be able to find it.

Add the SmartPart to your site
  1. Navigate to the site where you want to host your UserControl
  2. Click "Site Actions" - "Edit Page"
  3. Click on one of the Web Part placeholders ("Add a Web Part")
  4. Look for the SmartPart and add it to your page.

Note: If you havn't added a ScriptManager to either the Masterpage or the default.aspx page, you'll need to use the SmartPart with AJAX instead. In my case I've already configured my masterpage to include a ScriptManager.
Configure the SmartPart to use your AJAX-based UserControl
  1. Click on "Edit" - "Modify Shared Web Part"
  2. Choose your UserControl from the DropDown at the top of the settings, and click Ok or Apply to apply your changes!


  3. Click "Exit Edit Mode" to exit the edit mode of the page, and view your UserControl
     

That's a wrap!

Now your AJAX-enabled UserControl is hosted within a wrapper webpart (SmartPart) and have full AJAX Extensions support.

Thanks for tuning in. See you soon!

Cheers!

Comments

 

Links (1/10/2007) « Steve Pietrek’s SharePoint Stuff said:

Pingback from  Links (1/10/2007) &laquo; Steve Pietrek&#8217;s SharePoint Stuff

January 10, 2008 7:19 PM
 

Peter Lindblom said:

Nice step by step instructions. I like the fact that you're creating a Web Site-project instead of just the .ascx file to get the debugging, testing etc. in place. I got my AJAX stuff to work following your last post, and now I've implemented a usercontrol to test. It's awesome :)

Have a good day, see you soon.

January 11, 2008 1:59 AM
 

SharePoint, SharePoint and stuff said:

Novell Userverwaltung mit SharePoint kombinieren Microsoft Office SharePoint Server 2007 and Novell eDirectory

January 11, 2008 8:09 AM
 

Mirrored Blogs said:

Novell Userverwaltung mit SharePoint kombinieren Microsoft Office SharePoint Server 2007 and Novell eDirectory

January 11, 2008 9:06 AM
 

Marcel said:

nice impelementatione of the usercontrolle with ajax. i got it working now and used smartpart aswell and tried making my own webpart for wrapper but did not succeeded. but i like how you use the smartpart to host usercontrolles and insert into them some AJAX. some examples would be awesome

January 11, 2008 1:31 PM
 

SHAREPOINTBlogs.com Mirror said:

Author: Tobias Zimmergren Url: www.sharepointblogs.com/zimmer If you&#39;ve read my last two blogposts

January 12, 2008 5:28 PM
 

Christoffer Åström said:

Nice work!

I would like to see a dynamically created usercontrol with ajax support... Keep up the good work!

January 13, 2008 5:10 AM
 

George said:

Nice post.

I have a problem to use "AuditCompleteExtender", which needs a webservice and defined in ScriptManager, any idea?

May 12, 2008 10:00 AM
 

Brajendu Kumar Das said:

I've been trying to deploy AJAX code to Sharepoint for some time, and ran into a few problems.  Eventually, I gave up on the SmartPart because no matter what I did, I couldn't get an XML object data source to read correctly.  However, since I started my solution search here, I thought I'd post some work arounds for some of the common errors I've seen.

I was getting an error about registering scripts before prerender.  The solutions I found online were to make sure that the AJAX toolkit was up to date, and that anywhere onprerender was overridden, make sure you call base.onprerender().  This didn't solve my problem.  Getting rid of all the UpdateProgress controls did solve the problem.

There's a sporadically reported error that doesn't seem to have a documented solution:

This control does not allow connection strings with the following keywords: ‘Integrated Security’, ‘Trusted_Connection’.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Microsoft.SharePoint.WebPartPages.DataSourceControlDisabledException: This control does not allow connection strings with the following keywords: ‘Integrated Security’, ‘Trusted_Connection’.

I was able to get rid of this by putting my VS 2005 built ASPX pages in a separate virtual directory (running as an application), and editing the web.config for my new application.  Add the following section to the <system.web><pages> section:

<tagMapping>

  <remove tagType="System.Web.UI.WebControls.SqlDataSource, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

</tagMapping>

I'm not sure if this would work for the smartpart, but since I had to use a separate web application anyway, it works for me.  Essentially, it tells SharePoint to get out of the way when accessing data sources.

Pls Suggest....!

June 18, 2008 7:46 AM
 

Blog del CIIN said:

Una de las cuestiones que nos han planteado recientemente en el CIIN es el grado de madurez de la tecnología

July 5, 2008 9:39 AM
 

WSS 3.0 & MOSS…¿qué pasa con el soporte de AJAX? « Pasi??n por la tecnolog??a… said:

Pingback from  WSS 3.0 &amp; MOSS&#8230;&iquest;qu&eacute; pasa con el soporte de AJAX? &laquo; Pasi??n por la tecnolog??a&#8230;

July 5, 2008 9:42 AM
 

hnm.mallesh@gmail.com said:

System.InvalidOperationException: Script controls may not be registered before PreRender.

Im getting abov error can any body help me pleaseeeeeeeeeee

July 17, 2008 5:15 AM
 

Tobias Zimmergren said:

Hi guys,

for answers to your comments, please post the comments in my new blog: www.zimmergren.net instead of here.

This is an unmonitored blog.

Cheers

July 22, 2008 4:41 AM
 

Srinivasan K said:

Good article.

Keep doing...

September 26, 2008 6:04 AM

Leave a Comment

(required )  
(optional )
(required )  
Add

Need SharePoint Training? Attend a SharePoint Bootcamp!

Posts (c) their respective authors. Everything else (c) 2007 SharePoint Experts