Welcome to MSDN Blogs Sign in | Join | Help

Integrating ASP.NET AJAX with SharePoint

[Cross-posting from Mike Ammerlaan's blog. I was going to include this as part of my upcoming "Recommended Reading for March" post, but since it's such an important and frequently asked about topic and because the info is coming from the "horse's mouth," I thought that it deserves to be cross-posted here in full. <Lawrence />]

Microsoft ASP.NET AJAX 1.0: A Background

Microsoft ASP. NET AJAX 1.0 lets developers build Web 2.0 sites using the latest Ajax techniques. 

ASP.NET AJAX extends ASP.NET 2.0 and makes several new tools and techniques available to help you build applications more quickly:

  • Extensions to JavaScript.  ASP.NET AJAX extends the JavaScript library to bring standard object oriented concepts to JavaScript.  It brings a formal type declaration system, with support for inheritance.  It also provides a significant number of out of the box types, including types such as Sys.Net.WebRequest for working with web services.  Finally, it helps to abstract some cross-browser issues such as XML element traversal.  This makes it much easier to create robust JavaScript libraries and frameworks which are commonly needed by rich internet applications.
  • ASP.NET Control Extenders.  Extenders are additional ASP.NET controls which can extend the functionality of existing controls with additional Ajax capabilities.  A common example is an extender which allows existing textbox controls to have autocomplete functionality with no modification to the extended control.   (The autocomplete extender is included with the ASP.NET AJAX Control Toolkit.)
  • UpdatePanels.  UpdatePanels allow your existing ASP.NET controls and web parts to achieve the fluid, no-postback updates of Ajax-based applications with minimal re-coding of your control or part.  Quite simply, controls within the UpdatePanel control which ordinarily would post back to update their data will now be routed through an Ajax-style callback, resulting in a silent update back to the server.  This makes your application “postback” much less, making interaction with your control more seamless.

With Microsoft ASP.NET AJAX 1.0, you can build more dynamic applications that come closer to the rich style of interruption-free interaction you may see in standard client applications.

Microsoft ASP.NET AJAX 1.0 and SharePoint

Windows SharePoint Services version 3 builds much more directly on top of ASP.NET 2.0; therefore, many of the capabilities of ASP.NET AJAX work directly with SharePoint. 

However, in a few cases there are some compatibility issues between ASP.NET AJAX and SharePoint which are anticipated to be addressed in the first service pack of Windows SharePoint Services.  For this reason, until that first Service Pack of Windows SharePoint Services, we are unable to offer formal support through Microsoft Product Support Services for usage of ASP.NET AJAX within a SharePoint Web site.  This post is intended to provide developers with some guidelines for evaluating ASP.NET AJAX and WSS3.0. 

Specifically, there are some limitations on usages of the UpdatePanel in your web parts and controls.  Some approaches are described below to address these limitations, but these are workarounds and as such may cause other issues in your application.
Here are some common scenarios in SharePoint you should be able to achieve with Microsoft ASP.NET AJAX 1.0:

  1. Building a more powerful, re-usable JavaScript libraries you can use in your web controls and parts
  2. Enabling your web services to render via JSON, resulting in easier usage in JavaScript/Ajax Applications
  3. Building a web part that takes advantage of Extender technology to provide richer interaction styles, such as autocomplete on a textbox.
  4. Using an UpdatePanel in your web part or control for more fluid, no postback interaction. (this will require some workarounds, however.)

Adding Microsoft ASP.NET AJAX Technology to SharePoint Pages

To extend your SharePoint site with Microsoft ASP.NET AJAX 1.0, you’ll need to perform a few steps. 

  • First, you will need to download and install ASP.NET AJAX on servers in your farm.
  • Second, you need to extend web.config with some settings to enable ASP.NET AJAX technology. 
  • Third, you will need to add the ASP.NET AJAX Script Manager into your master page to enable scenarios such as Extenders or UpdatePanels.

Installing ASP.NET AJAX on servers in your farm

You will want to install the full "ASP.NET 2.0 AJAX Extensions 1.0" from ajax.asp.net.

Extending SharePoint web.config files with Microsoft ASP.NET AJAX 1.0

Extending SharePoint web.config files with ASP.NET AJAX requires that you interleave some Ajax registration entries in-line with WSS registration entries.  To do this you will need to edit your SharePoint web.config file, typically in a directory like c:\inetpub\wwwroot\wss\virtualdirectories\80.

1. Add a <sectionGroup>element to the <configSections>tag:

<configSections>
    <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
      <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
          <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/>
        <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35">
          <section name="jsonSerialization" type="System.Web.Configuration.ScriptingJsonSerializationSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="Everywhere" />
          <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
          <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication" />
        </sectionGroup>
      </sectionGroup>
    </sectionGroup>
</configSections> 

2. Add a <controls> section as a child of the <system.web>/<pages> tag:

<pages>
      <controls>
        <add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      </controls>
</pages>

3. Add the following tag to the <assemblies> tag, within <compilation>:

<assemblies>
       <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</assemblies>

4. Add some new registrations to the end of the <httpHandlers> section:

 <httpHandlers>
      <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/>
</httpHandlers>

5. Add a new registration to the HttpModules section, beneath any existing registrations:

<httpModules>
      <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules>

6. Add a SafeControl entry for the System.Web.UI namespace from Microsoft Ajax Extensions, within the <SharePoint>/<SafeControls>section:

<SafeControls>
      <SafeControl Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TypeName="*" Safe="True" />
</SafeControls>

7. Finally, add the following configuration tags at the bottom of web.config, near the bottom before the end <configuration> tag:

<system.web.extensions>
    <scripting>
      <webServices>
      <!-- Uncomment this line to enable the authentication service. Include requireSSL="true" if appropriate. -->
      <!--
        <authenticationService enabled="true" requireSSL = "true|false"/>
      -->
      <!-- Uncomment these lines to enable the profile service. To allow profile properties to be retrieved and modified in ASP.NET AJAX applications, you need to add each property name to the readAccessProperties and writeAccessProperties attributes. -->
      <!--
      <profileService enabled="true"
                      readAccessProperties="propertyname1,propertyname2"
                      writeAccessProperties="propertyname1,propertyname2" />
      -->
      </webServices>
      <!--
      <scriptResourceHandler enableCompression="true" enableCaching="true" />
      -->
    </scripting>
  </system.web.extensions>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <modules>
      <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    </modules>
    <handlers>
      <remove name="WebServiceHandlerFactory-Integrated" />
      <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode"
           type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </handlers>
</system.webServer>

Adding a ScriptManager into a SharePoint MasterPage

Many components of ASP.NET AJAX require the inclusion of a .NET ScriptManager control within a page. 

Although it may be possible in some cases to dynamically insert a script manager from within a control, in many cases the control may not be able to insert the script manager early enough in the page lifecycle depending on how the control is used, making this tricky to get right.  Also, the control implementer will need to ensure that multiple instances of their control (or other controls) do not result in the addition of multiple script managers within the page. For these reasons, dynamic insertion of a ScriptManager control from another control is not recommended.

To statically embed a script manager into a page, it is recommended that you add the ScriptManager into the master page of a site.

To do this, open up the master page for your site.  Typically, this will be located at <site url>/_catalogs/masterpage.  You can edit this file by opening it in an editor such as Microsoft SharePoint Designer, or directly in Notepad by opening your master page library via DAV (typically \\server\<pathtosite>\_catalogs\masterpage.)

Add the following into the markup of your page.  A recommended location is right beneath the WebPartManager registration  (search for  <WebPartPages:SPWebPartManager id="m" runat="Server" />): 

    <asp:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager>

Using UpdatePanels within SharePoint

UpdatePanels are a very useful addition to ASP.NET AJAX, and represent the simplest way to convert existing, standard ASP.NET controls and parts to take advantage of Ajax techniques.  However, there are some changes within Windows SharePoint Services which may get in the way of working with ASP.NET AJAX.

Windows SharePoint Services JavaScript has a “form onSubmit wrapper” which is used to override the default form action.  This work is put in place to ensure that certain types of URLs, which may contain double byte characters, will fully work across most postback and asynchronous callback scenarios.  However, if your scenarios do not involve double byte character URLs, you may successful disable this workaround and gain the ability to use ASP.NET AJAX UpdatePanels.

To do this, you may need to register a client startup script which disables this workaround, in addition to resetting the default form action:

    <script type='text/javascript'>_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;</script>

This script may be directly embedded in the page, or could be emitted by a control that uses the UpdatePanel.  The following is an example of a very simple ASP.NET Web Part which uses UpdatePanel capabilities:

using System;
using System.Collections;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;

namespace MS.Samples
{
    public class AjaxUpdatePanelPart : WebPart
    {
        private Label label;
        private TextBox textBox;

        protected override void  CreateChildControls()
        {
      base.CreateChildControls();

            this.EnsureUpdatePanelFixups();

            UpdatePanel up = new UpdatePanel();

            up.ID = "UpdatePanel1";
            up.ChildrenAsTriggers = true;
            up.UpdateMode = UpdatePanelUpdateMode.Conditional;

            this.Controls.Add(up);

            this.textBox = new TextBox();
            this.textBox.ID = "TextBox";
            up.ContentTemplateContainer.Controls.Add(this.textBox);

            this.label = new Label();
            this.label.Text = "Enter your name.";
            up.ContentTemplateContainer.Controls.Add(this.label);

            Button button = new Button();
            button.Text = "Say Hello";
            button.Click += new EventHandler(HandleButtonClick);
            up.ContentTemplateContainer.Controls.Add(button);
        }

        private void HandleButtonClick(object sender, EventArgs eventArgs)
        {
            this.label.Text = "Hello " + this.textBox.Text;
        }

        private void EnsureUpdatePanelFixups()
        {
            if (this.Page.Form != null)
            {
                string formOnSubmitAtt = this.Page.Form.Attributes["onsubmit"];
                if (formOnSubmitAtt == "return _spFormOnSubmitWrapper();")
                {
                    this.Page.Form.Attributes["onsubmit"] = "_spFormOnSubmitWrapper();";
                }
            }

            ScriptManager.RegisterStartupScript(this, typeof(AjaxUpdatePanelPart), "UpdatePanelFixup", "_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;", true);
        }
    }
}

Output Caching and ASP.NET AJAX

ASP.NET AJAX infrastructure is not compatible with output caching features.  This output caching infrastructure is a featured component of managed content pages such as those supported by web content management features in Microsoft Office SharePoint Server.  For this reason, many scenarios which involve output cached features may not be able to take advantage of components like UpdatePanel.  However, you will be able to successfully use other ASP.NET AJAX features, such as the JavaScript library, combined with your output cached pages.

Compatibility with output caching is targeted for a future release of ASP.NET AJAX infrastructure.

Conclusion

Microsoft ASP.NET AJAX 1.0 provides great building blocks for building rich Ajax-enabled applications.  Combined with the power of the SharePoint platform, and knowing some of the integration limitations, you can build powerful Web2.0 applications that bring together the best of both of these technologies.

 

Mike Ammerlaan, Program Manager

Published Friday, March 02, 2007 11:51 PM by LLiu
Filed under: , ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# SharePoint 整合 ASP.NET AJAX 1.0

SharePoint v3 雖然有了 blogs、wiki 等 web 2.0 的功能,一直讓人期待的是什麼時才能擁有 ajax 的支援。年初 ASP.NET AJAX 1.0 extension 正式發表,

Friday, March 30, 2007 3:57 AM by Application.NET

# re: Integrating ASP.NET AJAX with SharePoint

Is it possible to integrate other javascript frameworks with SharePoint such as  Mootools, Prototype etc?  Thanks.

Wednesday, June 13, 2007 1:14 AM by Sheldon

# re: Integrating ASP.NET AJAX with SharePoint

My "SmartPart with Ajax" is coming up with the error when i try to add one to my sharepoint page:

--------------

Unable to add select web part(s).

SmartPartAjax: A Web Part or Web Form Control on this page cannot be displayed or imported. The type is not registered as safe.

--------------

what do i need to do to correct this?

thank you

Amit

Friday, June 29, 2007 5:38 AM by Amit Sawhney

# SharePoint 整合 ASP.NET AJAX 1.0

SharePoint v3 雖然有了 blogs、wiki 等 web 2.0 的功能,一直讓人期待的是什麼時才能擁有 ajax 的支援。年初 ASP.NET AJAX 1.0 extension 正式發表

Thursday, July 19, 2007 10:31 PM by 外部部落格

# re: Integrating ASP.NET AJAX with SharePoint

Brilliant and easy to follow article!!!  

Thanks for sharing!

Monday, August 13, 2007 9:22 AM by RayBez

# ASP.NET AJAX Support in WSS 3.0 and MOSS 2007 SP1

I was doing some searches for SP1 and noticed a number of references with anticipation for SP1 awaiting

Friday, November 30, 2007 3:10 AM by Joel Oleson's Blog SharePoint Land

# re: Integrating ASP.NET AJAX with SharePoint

You missed scriptmanager from webpart code. You have to insert scriptmanager in webpart code , because if you miss then webpart will come back error message.

Friday, November 30, 2007 3:50 AM by Attila

# ASP.NET AJAX Support in WSS 3.0 and MOSS 2007 SP1

I was doing some searches for SP1 and noticed a number of references with anticipation for SP1 awaiting

Friday, November 30, 2007 4:04 AM by Noticias externas

# re: Integrating ASP.NET AJAX with SharePoint

After creating a web user control and implementing using the Smartpart with AJAX, everything was great until I "Published" the sharepoint page.  The user control has a timer control on it updating date and time.  Even with the web part properties up it was updating.  After closing the properties, it was updating.  When I published the page it stopped updating the date and time.  Any ideas?

Tuesday, December 18, 2007 3:59 PM by Michael

# Free SharePoint Business Data Catalog Column

I have decided to release the custom BDC column that I created to the public domain. This custom column

Thursday, December 20, 2007 10:38 PM by Aaron Robertson-Hodders SharePoint Blog

# re: Integrating ASP.NET AJAX with SharePoint

hi thanks for this article it was very useful to me.

Wednesday, January 16, 2008 11:11 PM by Thiru Kumaran

# re: Integrating ASP.NET AJAX with SharePoint

Hi,

I tried this, but my UpdatePanel does not show any data if the event that is bound to the asynch trigger is raised. It works fine in my development-site, but as soon as I start using my webpart in WSS it doesn't show any data.

Attaching the debugger tells me that all events are raised and handled correctly, but the browser does not seem to know how to handle the response.

Any suggestions??

Thanx,

Bart Coppens

Monday, January 28, 2008 6:33 AM by Bart

# re: Integrating ASP.NET AJAX with SharePoint

Does this work with Ajax 3.5 as well?

Tuesday, April 29, 2008 11:58 AM by Vicki DIllon

# re: Integrating ASP.NET AJAX with SharePoint

Has anyone made this work with Publishing->Publishing Portal (site template)? This works with Publishing->Collaboration Portal (site template).  

Webpart on publishing site, post back fires through an update panel (code executes too) but it won’t render control correct, plus it adds three square boxes to page title.

Any help??

Environment

Windows 2003 32bit, MOSS 2007 + SP1 +.Net 3.5

VS 2008 + SharePoint extension

Wednesday, June 18, 2008 5:12 PM by Shine

# re: Integrating ASP.NET AJAX with SharePoint

After changing master page from blueband.master to default.master, updatepanel worked for Publishing Portal  too...

One way to make this work

1) install .Net 3.5 framework

2) Create webpart using Visual studio 2008 + SharePoint extension. That is all…(http://msdn.microsoft.com/en-us/library/bb861877.aspx)

No web.config changes required for simple update panel to work. Only one entry need to be on web.config   is safecontrol entry for webaprt.

I really want to make webpart partial rending work with blueband.master, we used similar master page for our current site.

Thursday, June 19, 2008 10:10 AM by Shine

# re: Integrating ASP.NET AJAX with SharePoint

Shine i had the same issue and i have resolve it,

the problem is that the webpart manager TAG:

<WebPartPages:SPWebPartManager runat="server"/>

has been placed before the Form Tag

to resolve this issue

just cut it and paste after the Form Tag.

<Orginal... >

<body class="body" onload="_spBodyOnLoadWrapper();">

<WebPartPages:SPWebPartManager runat="server"/>

<form runat="server" onsubmit="return _spFormOnSubmitWrapper();">

....

....

******************************************

******************************************

< Resolved Code.. >

<body class="body" onload="javascript:if (typeof(_spBodyOnLoadWrapper) != 'undefined') _spBodyOnLoadWrapper();">

<form runat="server" onsubmit="return _spFormOnSubmitWrapper();">

<asp:ScriptManager runat="server" ID="ScriptManager1"></asp:ScriptManager>

<WebPartPages:SPWebPartManager runat="server"/>

<table cellpadding="0" cellspacing="0" class="master">

<tr>

....

....

Tuesday, July 08, 2008 7:15 AM by Tarek Jajeh

# re: Integrating ASP.NET AJAX with SharePoint

A did everything as is described. Add Script manager to my test page on sharepoint and tried to add SilverLight Media Viewer and I get this message:

SilverLight BluePrint for Sharepoint: The Media Viewer Web Part: Cannot import SL.SharePoint.MediaViewer Web Part.

What could be wrong, I did not find any solution on google...

I am using ajax web parts on my test page in connetion with SmartPart, but this does not work...

Wednesday, July 09, 2008 3:18 AM by Joe

# Getting Telerik RADEditor for MOSS installed - Part 1

Getting Telerik RADEditor for MOSS installed - Part 1 Getting Telerik&#39;s RADEditor installed in our

Friday, July 18, 2008 6:43 PM by Sean's blog

# re: Integrating ASP.NET AJAX with SharePoint

hi,

I have integrated the page but when i call the script manager and the update pannel it shows an error message as type is not registered safe PLEASE HELP MEEEEEEEEEEEEEEEEEEEEEEEE

Wednesday, July 23, 2008 5:55 AM by helen

# Timer control is not working in MOSS 2007

hi Michael

   I do have the same problem, my webpart is not refreshing after publishing the page.

How you resolved it?

Thanks.

Tuesday, August 05, 2008 4:27 AM by Roshan

# PostBack is executed with AJAX installed!!

Hi,

  Here we've been trying (for a week 'til now)to activate AJAX in our sharepoint website, but we still see the postback in the page..., why is this? I did everything you say to activate ajax...

  Is it possible that there is an error in step 1...?

  Thanks for sharing!

Tuesday, August 05, 2008 3:49 PM by Acaspita

# re: Integrating ASP.NET AJAX with SharePoint

Hi,

I followed the steps what you mention on above . but when i placing scriptmanager in masterpage it is giving error.

error: scriptmanger is not permitted in internet explorer 6.0.

how to solve the problem. i need to use ajax in share point portal..

Monday, September 22, 2008 6:49 AM by subbarao.bvr

# SharePoint 2007 母版页添加

按照SharePointTeamBolg的文章,文章中建议大家将

Monday, January 19, 2009 12:56 AM by 努力学习的小熊

Leave a Comment

(required) 
required 
(required) 
 
Page view tracker