Archive for October, 2011

Microsoft surely rocks…and here is the reason!

A good post here by “Good Coffee Good Code” guy explains the complete process and provides a sample solution here. Download and check it out!

In Pivot Viewer v2, sorting of multivalued facets is not taken care of automatically (i.e. if you are binding items in the non-cxml way). I took the sample provided at the end Pivot Viewer’s documentation here and changed the Employee class to:

public class Employee
	public Employee(string firstname, string lastname, DateTime? birthDate, string department, string office, string title, string officePhone, string personalPhone)
		FirstName = firstname;
		LastName = lastname;
		Birthdate = birthDate;
		Department = new List<string> { department, "D1", "D2", "D3", "D4", "D5" };
		Office = office;
		Title = title;
		OfficePhone = officePhone;
		PersonalPhone = personalPhone;

	public string FirstName { get; set; }
	public string LastName { get; set; }
	public DateTime? Birthdate { get; set; }
	public List<string> Department { get; set; }
	public string Office { get; set; }
	public string Title { get; set; }
	public string OfficePhone { get; set; }
	public string PersonalPhone { get; set; }

i.e. the “Department” variable is now of List<string> type. Then, I added “Department” as a Filterable Pivot Property in the xaml like:

<sdk:PivotViewerStringProperty Id="Department" Options="CanFilter" DisplayName="Department" Binding="{Binding Department}" />

The result was when I tried to sort on this property, it threw an error:

“Failed to compare two elements in an array. At least one object must implement IComparable”

As the error explains, the error comes because the List object doesn’t have an implementation for IComparable due to which the sorting is not supported. It kind of makes sense too since a generic sorting criteria of how a List object is compared with another cant be decided. To make this work, I created a custom class that inherits the List object and has an implementation for the IComparable interface. For the sorting comparison, I did it according to the number of items in an object compared with the other. I know its not going to be correct in most of cases, but hey…its just a feasibility shown here, you can sophisticate the sorting mechanism as much as you like! 🙂 The custom class goes like:

/// <summary>
/// Custom class to support multivalued facets sorting
/// </summary>
public class StringFacets : List<string>, IComparable
	#region Implementation of IComparable

	/// <summary>
	/// Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.
	/// </summary>
	/// <returns>
	/// A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has these meanings: Value Meaning Less than zero This instance precedes <paramref name="obj"/> in the sort order. Zero This instance occurs in the same position in the sort order as <paramref name="obj"/>.Greater than zero This instance follows <paramref name="obj"/> in the sort order.
	/// </returns>
	/// <param name="obj">An object to compare with this instance. </param><exception cref="T:System.ArgumentException"><paramref name="obj"/> is not the same type as this instance. </exception>
	public int CompareTo(object obj)
		var stringFacet = obj as StringFacets;
		if (stringFacet == null)
			return -1;

		if (stringFacet.Count < this.Count)
			return -1;
		if (stringFacet.Count == this.Count)
			return 0;
		if (stringFacet.Count > this.Count)
			return 1;

		return -1;



And the class is to be used as follows in the Employee class:

public StringFacets Department { get; set; }


Now, when you will try to sort the items based on the  “Department” category, it would work like a charm. The code is available at

If you would like to share any improvement, its going to be awesome!

Pivot Viewer v2 (released as a part of SL 5 RC) is one of smartest improvements done by MS to its own products. However, there are still somethings which are not very clearly available. For instance, sorting multivalued facets, custom actions, etc. In this series of blogs, I am going to explain to you how one can do the tasks which were quite easy in the old version of Pivot Viewer but not so straight forward in this version and then later I’ll take you through how you can harness the power of this newer version of the super awesome product. The series will evolve as follows:

Stay tuned!

Open Xml on Azure

I was once deploying a project (that involves Open Xml operations) on Azure. The project would run fine locally on my machine and but the code just wouldn’t run. After lots of scratching, I discovered the 2 problematic situations faced most widely that leads to this problems. The root cause here is that Azure doesn’t support the OpenXml operation directly since the required dlls are not available on Azure. The solution to the problem is changing the configuration of your project references, so whenever you get stuck in this problem, just check out which of the following situations apply to you and make the necessary changes to make your code work.

Scenario 1: The excel operations are performed in your main web project (i.e. the project being deployed on the cloud):

  1. Expand the project’s “References” folder
  2. Select the “DocumentFormat.OpenXml” and “WindowsBase” dlls
  3. Right click on them and then select “Properties”
  4. Set “Copy Local = True
Scenario 2: The Open Xml operations are being done inside a dll and then this custom dll is being referenced in your web project. If this is the case, do the following:
  1. Add References to “DocumentFormat.OpenXml” and “WindowsBase” dll in your main web project
  2. In your web project, expand the “References” folder
  3. Select your custom dll and the “DocumentFormat.OpenXml” and “WindowsBase” dlls
  4. Right click on them and then select “Properties”
  5. Set “Copy Local = True
Rebuild your project and deploy again, and this time it’ll work!

Silverlight 5 Toolkit is here!

Good news for all those facing problems with SL5 RC projects and the SL4 Toolkit issues! The Toolkit for SL5 RC projects is available here.

Happy coding! 🙂