Category Archives: C#

How To Build a JSON Web Service to Send Mail in ASP.Net

I was pleasantly surprised to see the traffic analytics on a post I wrote a while ago on Using jQuery to Send Email with Web Services. However, I found some who were still requiring assistance writing the actual .NET web service. Here you go.

In Visual Studio, when you create a web service (ASMX), the default format is SOAP. You will need add a couple of attributes to the code to make it return results in a true JSON format.

  1. First requirement is that you do a POST, but the AJAX call already takes care of that.
  2. Your class is going to need the

    attribute so that you can call the web service from javascript.

  3. Your method responsible for sending mail will need this attribute:
    [ScriptMethod(UseHttpGet = false,ResponseFormat = ResponseFormat.Json)]

Continue reading

Using jQuery to Send Email with Web Services

Note: to see the code for the .NET web service to be used with jQuery code below, see new post here:
How To Build a JSON Web Service to Send Mail in ASP.Net

Just to make sure we are all up to speed, a quick concept in extreme layman’s terms

– jQuery is a front-end thing, meaning it gets your browser to do stuff, like move stuff around and change stuff after it’s already been loaded from the server.

– Sending email is a back-end thing, meaning it requires talking to the server to send stuff.

Now that we are all up to speed, including my 5-month-old daughter, I think it’s clear that jQuery alone can’t do this, at least not without help.  How does jQuery get help from the server? It uses an AJAX Post method to talk to a server, in our case, a web service, to send something and then get something back in return, like a result, which could be ignored by not specifying it in the AJAX Post call.

For this exercise, because I prefer .NET to PHP, I will be using a very simple .NET web service to send out the email, but this task can be accomplished by any server that accepts HTTP Post commands, like a CGI script.
Let’s review the steps that we will take to accomplish this task:

  1. Validate form data
  2. Send data to web service
  3. Interpret & display results from web service

Continue reading

Sitecore’s CustomCache – A simple implementation

Sitecore comes stocked with a custom caching mechanism in its Sitecore.Caching namespace. Unfortunately, there is not much documentation on it, but this post will hopefully shed some light on this mysterious, yet useful object known as CustomCache.

To implement, you will need 3 things:

  1. Your own CustomCache class
  2. A CacheManager class
  3. The code that will be using the cache

Your own Custom Cache class

Because Sitecore.Caching.CustomCache is an abstract class, you cannot directly instantiate it, but you can inherit from it.

Continue reading

The Power of LINQ – Sorting Lists with a single line of code

This post, just like the code example, is short and to the point, because that’s how LINQ is – short, to the point, and powerful.

Let’s say you have an object called Country. There will be a List<> populated with it that is going to be used in a drop-down box. It has the following properties:

  • CountryName (string)
  • DropDownValue (string)
  • SortOrder (int)

The business logic for the list is to order it by SortOrder, and then by CountryName. You ready for the code?

Continue reading

Behavior-Driven Development (BDD) – The What, Why, and How

The standard development method they teach you in Software Engineering 101 is

  1. Get your requirements
  2. Come up with a top-bottom architectural design
  3. Code up modules one by one
  4. Test (Unit/System/Regression)
  5. Delivery/Deployment

It’s a great approach and the solution you deliver can be built without compilation errors and work fine… or so you think.  You deliver the solution and all of the sudden, the client points out that there were requirements missed and/or incomplete.  My favorite taught-myself-in-24-hours-developer reaction to this is:

“But… it… compiles… therefore… it works…  it has to!”

With Behavior-Driven Development (BDD), and one of the reasons this approach is favored by QA teams all over the world is this approach ensures and, actually, requires that you work against the user requirements when working on the design.
Continue reading

Tutorial: Refresher on Sorting Generics in C# – IComparable & IComparer

I thought I’d post a quick refresher for sorting Lists<> and generics since it will be used in the post about working with Sitecore Fast Query.

There are a lot of ways of sorting items you get back from Sitecore, including even implementing a bubble sort in the code-behind aspx.cs file.  That may work for a quick prototyping exercise; however, when working with enterprise class systems, why not leverage the tools C# gave you to create elegant and robust code by creating line-of-business model objects and using a C# List<T> that already comes with sort functionality (provided you define the rules on how to compare your objects)?


If you want to your objects to be sortable, you have to make sure they implement the IComparable method, otherwise when you try to do a List<T>.Sort() on them, you will get an exception.  To implement the IComparable class, you will need to implement the CompareTo() method, which essentially describes how to compare two objects.  If you compare your objects alphabetically, you can take advantage of the String.CompareTo() method here.  Take a look at the code example below using a Movie object.

public class Movie : IComparable
public int YearReleased { get; set; }
public string MovieTitle { get; set; }

//need to implement this as a result
//of declaring the IComparable interface
int CompareTo(Movie otherMovie)
return MovieTitle.CompareTo(otherMovie.Title);


When you implement the IComparable interface and the CompareTo method, that is the default sort method.  When you want to create additional properties on which to sort, this is where the IComparer interface comes in.   For every method on which you will want to sort, you will have to create a class that implements the IComparer interface.  So, if you wanted to create two sorts – ascending and descending – on the year the movie was released, you will need two classes that implement the IComparer interface.  With this particular example, we can create these classes as nested.

public class Movie : IComparable //main class
public int YearReleased { get; set; }
public string MovieTitle { get; set; }

public class SortByYearReleasedAscending : IComparer
int Compare(Movie a, Movie b)
var compareResult = a.YearReleased - b.YearReleased;

return ( (compareResult == 0) ? 0 : compareResult / Math.Abs(compareResult) );


public class SortByYearReleasedDescending : IComparer

In case I lost you in the Compare() method, I am all about C# shortcuts (thanks to ReSharper) and didn’t feel like writing this out:

if (a.year > b.year)
return 1;
if (a.year < b.year)
return -1;
return 0;

Since the year is an integer, we can just take the difference. If the value on the left is bigger, the result will be positive, otherwise negative. At that point, we will need to return 1, -1, or if it’s the same, the result is 0. To get our result down to a 1 (positive or negative), we simply divide by the absolute value of the result, but not before we check if it’s 0 to bypass the divide by zero error.

And that’s all there is to it. To piece it all together, if you have a List<movieList>, you can do a sort on it using movieList.Sort() which will use the default IComparable.CompareTo method, or you can specify how you want it sorted by passing in the IComparer component, like movieList.Sort(new movieList.SortByYearReleasedAscending())

Questions or comments – drop me a line.