Just like everyone else, I'd heard the buzz about AJAX but had never really tried it out; both from a user perspective, and programmatically! I do a lot of programming for my work, and one of the environments I'm spending a lot of time developing in is SharePoint, and in particular, web parts. Having created quite a few web parts, one of the things I became quite aware of is that performance can be an issue. When there are lots of web parts on a Team site, any user interaction with a web part will cause all of them to have to be refreshed, which can be slow if the web parts are complex (like ours are). So....the answer to me seemed to be AJAX! OK, I was using it as an excuse to try out AJAX, but in the end, it was the right decision to solve my problems. Sometimes, it's better to be lucky than good :)
Being a true programmer at heart (i.e. lazy:)), I decided there must be an easy way to do this! I looked through a whole bunch of AJAX libraries and frameworks out there, but in the end, came across Ajax.NET Professional, and really liked it. It seemed to be robust, complete, lightweight, well used, and well supported. So, next step, how to get it working with my SharePoint Web Part? Well, that was where it got interesting. Although there is great documentation with Ajax.NET, there was nothing specific to SharePoint. Eventually, I found some hints in the Google Ajax.NET forum which then led to me finding a blog entry by Maxim Tarassenko that was enough to get me to a working solution.
This article (I hope!) will serve as a single reference point in what to do to enable Ajax.NET Pro within SharePoint so that web parts can be created that can use AJAX. What I will not be doing is trying to explain how to create web parts or go into detailed examples of Ajax.NET Pro usage! The aim is to make every step clear and easy, even for a SharePoint novice, so if any steps are not clear, let me know and I'll try and explain them better!
Installing Ajax.NET Pro
The example project Web Part was created using version 22.214.171.124. Once this, or the current latest release, has been downloaded and the contents extracted to a directory (you'll need to have it handy for referencing when creating code, so put it somewhere permanent), the following steps will configure SharePoint:
- Install AjaxPro.dll into the GAC. I prefer to use the GAC because it makes security easier, and there is only ever one place the DLL is installed to!
- Add the AJAX HttpHandler to the web.config file for each SharePoint Portal site that you want to AJAX enable.
- Add the /ajaxpro path to the list of excluded paths for each portal site.
Installing the Ajax.Net Pro DLL in the GAC
I prefer to use the GAC because it makes security easier, and there is only ever one place the DLL is installed to! There are several ways to do this, but the simplest, to me, is to drag-and-drop the AjaxPro.dll into the c:\windows\Assemblies directory.
Adding the HttpHandler
This step must be done manually by editing the appropriate web.config file for each SharePoint Portal site that Ajax.NET is to be enabled for. In the web.config file, locate the
<httpHandlers> section - it will probably have the two default entries as follows:
<add verb="*" path="/_vti_bin/*.aspx"
<add verb="*" path="*.aspx"
Microsoft.SharePoint, Version=126.96.36.199, Culture=neutral,
Add the following entry to this section:
<add verb="*" path="ajaxpro/*.ashx"
type="AjaxPro.AjaxHandlerFactory, AjaxPro, Version=188.8.131.52,
Culture=neutral, PublicKeyToken=4735ae9824c7d3ec" />
Excluding the /ajaxpro path
This can be done in one of two ways; either through the (graphical) SharePoint Central Administrator, or by using the (non-graphical) stsadm command-line tool.
Using SharePoint Central Administrator
- Click Start, point to All Programs, point to Administrative Tools, and then click SharePoint Central Administration.
- On the Central Administration page, under Portal Site and Virtual Server Configuration, click Configure virtual server settings from the Virtual Server List page.
- On the Virtual Server List page, select the virtual server you want to configure.
- On the Virtual Server Settings page, under Virtual Server Management, click Define managed paths.
- In the Add a New Path section, in the Path box, type /ajaxpro.
- Select Excluded path for the Type.
- Click OK.
- Open a Windows command prompt.
- cd C:\Program Files\Common Files\Microsoft Shared\web server extensions\60\BIN (on a side note, I add this to my PATH environment because I use stsadm all the time).
- stsadm -o addpath -url http://your_server_portal_name/ajaxpro -type exclusion.
The Web Part Code
The first thing you have to do with Ajax.NET Pro is to register your types (class or classes). This should be done once, so a good place is on page load. The command to register the type is:
UserInfo with the name of your type.
The last "problem" was how to initiate a client side call through Ajax.NET Pro when the web part is first loaded without the user having to do anything! This bit is what took the longest for me to get working:) At first, I thought I could add something to the web parts page load code (same place I put the type registration code), but that does not work. What I really needed to do was add it to the actual page
AjaxUserInfoInit when the page (web part) is first loaded. In here, I then execute a call, or calls, back to the web part DLL to execute my initial code.
A Word About the Example
The example is very silly and trivial in that all it does is to display a message when the web part is first rendered, letting the user know something is being done (just like Microsoft Outlook Web Access):
This "something" is a couple of Ajax.NET calls that will return data that is displayed in the web part:
Hopefully, this will get the creative juices flowing, and you'll be able to change your web parts into AJAX extravaganza's:) Well, that's my intention for my customers! I'd like to think that by using AJAX, I will enhance the user's experience by improving the feel and speed of SharePoint - less page refreshes is a good thing.