collapse
expand

An inside look at web development.

Using an HttpHandler for AJAX requests on the server in DotNetNuke

By Hristo Evtimov
February 7, 2012
Share this
Email to a friend Email to a friend

During the last four years as a developer at BlueBolt Solutions, I occasionally need to create a DotNetNuke module that would need to load some data though an AJAX call to the server, for example, if a module requires internal search or a module that allows users to load comments for a specific comment thread. There are different approaches to perform this AJAX request and I wanted to share my observations thus far.

If your module is simple enough, you might be able to get away with the Update Panel that DotNetNuke usually wraps around your module. I personally find that to require too many resources with all the calls to different Page_Loads, event handlers, and the enormous amount of HTML that could get generated on the server and returned to the browser. A much faster approach is to call an HttpHandler through JavaScript and write out a JSON (JavaScript Object Notation) object inside your handler. The handler does not have any unnecessary event calls and it returns the smallest amount of data to the browser, packaged as an easy-to-use JSON object.

This produces a great increase in speed, but a few challenges do arise. First of all, you need to use JavaScript to process the JSON and display the data as wanted. Secondly the HttpHandler itself does not really inherit from DotNetNuke’s PortalModuleBase and thus it does not have access to PortalSettings or the UserInfo objects. Therefore, if you need to load data for the current portal only, or only data relative to the current user, then the HttpHandler needs to know what the current portal id and/or user id is.


Getting the current user info:

public class HandlerForSomething : IHttpHandler
    {
 
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            
            UserInfo user = DotNetNuke.Entities.Users.UserController.GetCurrentUserInfo();

After getting the user we can perform different checks on that user. Whether (s)he is in a certain role or even whether (s)he is logged in:

if (user.IsInRole("Administrators") || user.IsSuperUser)
{
//Load some data here…
}


Getting the current portal info:

PortalSettings ps = (PortalSettings)context.Items["PortalSettings"];
if (ps != null)
{
//Here we can use ps.PortalId to load data for the current portal only
}


The PortalSettings object is stored in HTTPContext before your handler is processed.

Using an HttpHandler is a fast way to asynchronously load some data in your module but it does require a good amount of JavaScript work to parse the returned data and might not be applicable for  all scenarios.

Comments (0)

 
Learn More

Connect with us!

Search this Blog
Enter Keywords
Copyright 2012 BlueBolt, LLC