Wednesday, November 6, 2013

Sitefinity ajax requests

On a project at work we used Sitefinity from Telerik as a CMS System. I can say that it is a very good and especially end-user friendly CMS System because it let's them drag & drop so called widgets on the page and this is straight forward for almost everyone.

We are using the mixed mode (aspx and mvc) but I developed all my widgets in MVC.
For one widget I had the request to show statistical data in a chart and the request to the datasource (an external web server) takes about 2-3 seconds. I didn't want to let the user feel that the whole page is not responding just because of this data request and so I decided to to an ajax request in the background.
With jQuery this is an easy task, but I was not able to return the json data to the client. I returned a JsonResult on the Controller but Sitefinity always injected somehow the master layout of the page and therefore it was invalid json when it was received by the client.

Fortunately I found a solution to this problem:

public ActionResult GetAjaxData()
{
    Response.ClearHeaders();
    Response.AddHeader("Content-type""text/json");
    Response.AddHeader("Content-type""application/json");
    Response.ClearContent();
    //here you obtain your list of objects
    List myResult = new List(); 
    Response.Write(myResult.ToJson());
    Response.End();
 
    return new EmptyResult();
}

4 comments:

Unknown said...

Very nice solution, Manfred. So now, you don't have to actually write a new route as I was doing before. I like your solution.

Voltix said...

Sitefinity Winnipeg

web lol said...

kul page

Anonymous said...

SITEFINITY IS ONE BIG PILE OF SHIT!