Angus Logan

MCMS/SPS/.NET/SQL/Microsoft Australia

Using Ajax.NET under SharePoint (doesn't play by default)

What’s this – An original post? – No way ; )

---

I’m having a play with Ajax.NET because I have some somewhat unique requirements from an application which is delivered under SharePoint.

The requirement is that a process may take 1 hour to complete and the process progress is being stored in a database which must be queried.

So what my plans to do are create a Web Part which looks in the DB for these processes and refresh and image and the UI to display progress – but all without a page refresh.

Ajax.NET is the perfect solution.

When I put the CSharpSample under /_layouts/ and make the initial call:

Ajax.Utility.RegisterTypeForAjax(typeof(WebUserControl1));

I get a cast exception.

Message: Specified cast is not valid

StackTrace: at Ajax.Utility.RegisterCommonAjax() at Ajax.Utility.RegisterTypeForAjax(Type t) at CSharpSample.WebUserControl1.Page_Load(Object sender, EventArgs e) in c:\program files\common files\microsoft shared\web server extensions\60\template\layouts\csharpsample\webusercontrol1.ascx.cs:line 20

The way around this is – don’t make the RegisterTypeForAjax  call!

So you need to manually include the Ajax.NET client side references (bolded below)

<%@ ControlLanguage="c#"AutoEventWireup="false"Codebehind="WebUserControl1.ascx.cs"Inherits="CSharpSample.WebUserControl1"TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>

 

<!-- Manually Includ the Ajax JavaScript Files - Start -->

<scripttype="text/javascript"src="/_layouts/CSharpSample/csharpwrapper/common.ashx"></script>

<scripttype="text/javascript"src="/_layouts/CSharpSample/csharpwrapper/CSharpSample.WebUserControl1,CSharpSample.ashx"></script>

<!-- Manually Includ the Ajax JavaScript Files - End -->

 

<scriptlanguage="javascript">

function GetTime()

{

      // Make the client side to server side call

      var res = WebUserControl1.GetPercentageComplete();

     

      // Get the value and set the width of the image      

      document.getElementById('progress1').width=res.value;

 

      // TODO: Handle the 100% complete response (hide the table)   

 

      // Repeat the call every 1 second

      setTimeout('GetTime()', 1000);

}

</script>

<tablewidth="100"style="border: solid 1px black;" cellpadding="0" cellspacing="0">

      <tr>

            <td><imgsrc="/_layouts/CSharpSample/images/blackpixel.gif"height="15"width="0"id="progress1"></td>

      </tr>

</table>

The code behind then looks like:

[Ajax.AjaxMethod]

public int GetPercentageComplete()

{

      // Go get the Percentage complete (right now just seconds)

      return(System.DateTime.Now.Second);

}

 

Other than that – Ajax.NET looks awesome.

 

Now to work out how to package it all into a single web part and not under /_layouts/

Comments

TrackBack said:

# July 10, 2005 6:23 AM

TrackBack said:

# July 10, 2005 11:06 PM

TrackBack said:

# July 11, 2005 3:52 AM

anguslogan said:

Hy, to put the ascx in Sharepoint, you use SmarPart ? I'm having this error :

Error: unable to load ~/_layouts/telemoveis/LeadTime.ascx
Details: It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS. (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\60\template\layouts\web.config line 9)
# July 11, 2005 10:41 PM

anguslogan said:

Hi Angus,
I am having a very similar problem and I am hoping you could help me. I need some help very urgently.

I am trying to use Ajax.net (5.7) on a sharepoint website. My webparts were not working when I put the httphandler in the web.config in the IIS root. But on doing some googling, I found that the solution to this was to add the handler to the layouts/web.config, I tried it and it worked just fine. Now, let me explain the setup I have :

I have my Ajax enabled control in a custom control library.
I use a separate class in the same namespace as my custom control to store all the Ajax funtions.
I then use the custom control in my webpart.

I use my webpart on a subsite, instead of the root dir of the WSS server.


Instead of using the RegisterTypeForAjax call, I tried to put the script tags manually as you suggested. "Reports" is the subsite I have. *** Please note that my control library dll and my webpart dll still reside in the bin directory of the IIS Root. ***

When I try to generate the webpart, I see the script tags generated as :

< script type="text/javascript" src="/Reports/ajax/CustomControls.CAjaxFunctions,CustomControls.ashx">< / script >
< script type="text/javascript" src="/Reports/ajax/common.ashx"> < / script >

I call my function as :

CustomControls.CAjaxFunctions.GetListOptions(key, datatagName, GetOptionLists_CallBack);

I get an error message "'CustomControls' is undefined". Is this because the function is not in the webpart, but rather in the control library ? Could you tell me how you have your setup in sharepoint ? I would really appreciate any help you could give. I need some help here urgently. Thank you.
# December 10, 2005 1:41 AM

Aleksey said:

Интересная подборка ссылок по использованию AJAX в SharePoint. Ссылок очень много, сам еще пересмотрел

# May 13, 2007 1:26 PM