ShareThis

Cross Domain Access to HTTP Handler using jQuery Ajax

We can access Http handlers directly from a regular AJAX call, if they are in the same domain. However it is not the case if handler is hosted in a different domain. One solution is to use JSONP (the “padding” around the pure JSON).

JSONP pass the response from the server in to a user specified function, return the response as JSON, but also wrap the response in the requested call back.

In order to use JSONP, server has to support JSONP. The client tells the server the callback function for the response. The server then return the response as JSON, but also wraps the response in this callback function.

HttpHandler sending the response in the format expected by JSONP.

public class AtomHandler : IHttpHandler
{
  ///<summary>
  /// Enables processing of HTTP Web requests by a custom HttpHandler that implements the System.Web.IHttpHandler interface.
  ///</summary>
  /// <param name="context"> A System.Web.HttpContext object that provides references to the intrinsic server objects (for example, Request, Response, Session, and Server) used to service HTTP requests .< /param>
  public voidProcessRequest( HttpContext context)
  {
   string jsonString= "{'Title':'What Will Brand Engagement Look Like in 2020?','Summary':'When it comes to brand engagement, 5 years will seem like a lifetime.','UpdatedDate':'4/23/2015 4:27:46 AM +00:00','FeedType':'MBD - Marketing'}" ;
 
   string jsonp=context.Request["callback"];
 
   if (! String.IsNullOrEmpty (jsonp))
   {
    jsonString = jsonp + "("+jsonString+")";
   }
 
   context.Response.ContentType = "application/json";
   context.Response.Write(jsonString);
  }
 
  ///<summary>
  /// Gets a value indicating whether another request can use this instance.
  ///</summary>
  public bool IsReusable
  {
   get
   {
    return false;
   }
  }
}


jQuery JSONP request to get data from handler.

var loadAtomData=function() {
  try {
    $.ajax({
      url: "http://<server>/AtomTest/AtomHandler.ashx",               
      type: "GET",
      dataType: "jsonp",
      jsonp: "callback",
      contentType: "application/json; charset=utf-8" ,
 
      success: function (responseData, txtStatus, jqXHR) {
        console.log( "The response is" , responseData);
      },
      error: function (responseData, txtStatus, errThrown) {
        console.warn(responseData, txtStatus, errThrown);
        alert( 'JSONP failed - ' + txtStatus);
      }
    });
  } catch(e) {
    alert(e)
  }
}
 
$(document).ready( function() {
loadAtomData();
});

Web browsers let scripts from a webpage to access data from a second page, if both have the same origin (URI scheme, hostname and port). JSONP works since browsers do not enforce the same-origin policy on <script> tags.

How to Create a Custom View for SharePoint 2010 Survey

As you all know creating a custom view for SharePoint in-built survey list is not there by default. But this is a required feature in some cases. For example if you want to filter survey responses by an answer to a particular question, then having a view with that question is handy. 

So here is a workaround:

01. First of all find the survey ID. We can do that by navigating to survey settings and Find query string parameter “List” from the browser URL.

http://<server>/_layouts/survedit.aspx?List=%7B630DCEEC%2D45DD%2D4C35%2D9EBF%2D7B2C607333AE%7D

02. You won’t find ‘Create View” option in the ribbon or anywhere, but can access that page by directly accessing from the URL. So visit “Create View” page using the URL:

http://<server>/_layouts/ViewType.aspx?List=<List ID from previous step>

03. In order to create a new View, click on “All Responses” link from “Start from an existing view” section.



04. In the Create View page, provide a “View Name”, select Columns and set any other settings. Click OK.

That is it! View can be accessed from the URL: http://<server>/Lists/<survey name>/<view name>.aspx

“Detailed” is the custom view that I created and shown in the above screenshot.

xrm.CreateQuery() - Invalid 'where' Condition



“Invalid ‘where’ condition. An entity member is invoking an invalid property or method.”

I came across above error, while I was working on a LINQ query to retrieve a set of records from a CRM 2013 custom entity. It is a CRM 2013 online environment and I was using XrmServiceContext to retrieve data.


Even though it appears to be that I have specified an invalid property, I found out below line is causing the issue.

Where(d => d.nav_expirationdate.HasValue && ...

It turns out that LINQ to CRM implementation expects two values in each condition and CRM Property must be on the left hand side. Otherwise where clause will simply crash.

Code that will Crash with Invalid 'where' condition exception:

this.xrm.CreateQuery<nav_myrequest>().Where(d => d.nav_expirationdate.HasValue && ...

Code that will Work without an error:

this.xrm.CreateQuery<nav_myrequest>().Where(d => d.nav_expirationdate != null && ...