Unit Testing

Sitecore Unit Testing is not a Success or is IT ?

On November 28, 2013 I came across this question on StackOverflow – Sitecore Unit Testing is not a success? Luckily I could answer it and provide information to the user dpwulp94 but on the first read I was astonished that we as developers do give up unless there is some motivation or solution for the problems we face. My effort for this blog post is to encourage people to break through the barriers of limitations provided by the initial version of any application. Make the effective changes not only in frameworks but also in the attitude and approach of the common problems faced by Sitecore community developers.

Sitecore Unit Testing is not a success or is it ? dilbert-quality “Unit Testing is always useful for anything that is written in C# (or should I say in OO Language)” is what I said on the post but now the front end frameworks are also rapidly changing for the ability to write tests around it. dpwulp94 was implying that after a good amount of research he was planning to give up (“Should we give up or can we get it working and in what way?”), but giving up for the time being and revisiting it with a fresh approach is the key with testing. All I could suggest again is watching

  1. Another interesting video by codeflood.
  2. Automated Testing with Sitecore V2 video by Alistair Deneys from Hedgehog

and read Alistair Deneys’ blog post Unit Testing in Sitecore is not Scary There are also more Unit Testing videos mentioned in Unit Testing Sitecore using NUnit, serializing content blog post if you want more information on how to do Unit Testing in Sitecore.  Coming back to the StackOverflow answer, I also mentioned that “I can gauge the blockage that you are facing. But have a read of Testing Strategies – Opinionated Advice by Kevin Obee.” Sometimes while doing Unit Testing we loose the focus of being pragmatic and just follow the Unit Testing Principles. Confessing in the answer that “I faced that and establishing the balance would be the key. In no way I am suggesting that you might have gone in this direction, but just suggesting that sometimes taking a step back, reflecting on what we ACTUALLY want to TEST and restarting helps a lot.” After reading through the answer a positive comment by dpwulp94 was “The article of Unit Testing in Sitecore is not scary seems great. But it would be nice if there are some people on StackOverFlow who encountered the same problem, writen above. And (of course) how that was being solved” To this I would completely agree and would like open source isolation testing framework for Sitecore like Sitecore.FakeDb gain more light. If this interests you check out this video by Pavel Veller from BrainJocks Keep open source projects coming and let’s make Sitecore Testing better by the day. Happy Sitecoring !

Unit Testing Sitecore using NUnit, serializing content

A while ago I came across this question on StackOverflow – Unit Testing Sitecore using NUnit, serializing content

I was lucky to successfully answer the question and provide information to the user dpwulp94 and also hope that this answer provided was helpful to other seekers as well.

My effort for this blog post is to put the information together in one place and update it periodically.

Unit Testing Sitecore using NUnit, serializing content

Anyone seeking Unit Testing in Sitecore should watch the below videos and there are plenty of others as well.

Interesting videos on Youtube

  1. Sitecore Unit Testing video by gravypower.
  2. Another interesting video by codeflood.
  3. Most knowledgeable for me was this video by Alistair Deneys from Hedgehog
  4. Advanced Glass Walkthrough video by Mike Edwards from Hedgehog
  5. Automated Testing with Sitecore V2 video by Alistair Deneys from Hedgehog

Other interesting blogs are

  1. Igloo – Unit Testing for Sitecore
  2. iStern – Unit testing with Sitecore Item by Thomas Stern
  3. Mocking Sitecore with MS Fakes Part I and Part II by Thomas Stern
  4. And lastly my preferred Coffee => Coder => Code again by Alistair Deneys
  5. Knifecore’s blog also helped me a lot when I was trying to write tests.
  6. Unit Testing with Sitecore by Reservoir Devs

The topic of Serializing Sitecore objects was also mentioned

We also want to mock the Sitecore data by serializing the Sitecore objects. I have been looking for, but could not find a good tutorial to do this. How can I do this?

Kern Nightingale has got a perfect presentation which will give you insight to how to use it for testing as well.

Would also recommend Sitecore Glimpse by Kevin Obee, the project’s one of the purpose is “provide build scripts that automate the tasks of compiling, testing and packaging the project artifacts to make it easier to open the project up for collaborative development” which he mentions on his blog.

To conclude hope this gives bit more perspective on Unit Testing and Serialization with Sitecore.

Happy Sitecoring!



Shriroop Parikh is my name and I work as a Sitecore consultant for global digital agency. At work I build and contribute in creating Sitecore experiences for various companies using the Sitecore Experience Platform (XP). Working with Sitecore since 2010 and along the way I have gained experience on architecture – content, software and hardware; cloud based platforms & services – AWS, Azure and Rackspace, back-end website development using ASP.NET / ASP.NET MVC, SQL Server and MongoDB. Working with javascript frameworks like Knockout and optimising web pages by tweaking front-end, has also been part of the journey.

About this blog

This is an effort to post my exploration and learning of Sitecore as I go along on my journey. The inspiration came after attending Sitecore Symposium 2012 in Amsterdam during September 2012.

Get in touch

You can reach out to me on Twitter handle @_shriroop_ or through LinkedIn.

How to import XML documents into Sitecore?

A while ago I came across a rather interesting problem. What if a requirement is made by a client that they would need hundreds and thousands of XML documents imported in Sitecore. How can we achieve this?

So after researching a lot on the topic on various other Sitecore MVP blogs, I didn’t get a convincing solution or let’s say I didn’t look hard enough. After much research and reading documents on Sitecore Developer Network (SDN), I was able to find answer to this interesting question.

Import XML Files into Sitecore

Here it is and would like to share it with the Sitecore community.

  • By creating a File Crawler based on Sitecore.Search. The Sitecore.Search API acts as a wrapper for Lucene.Net. It provides flexible integration of Sitecore with Lucene and a set of .NET friendly wrappers around the Lucene classes related to search.
  • Creating a File Crawler is explained very nicely and in detail in the Sitecore Search and Indexing document (refer pages 39 to 44) on SDN.
  • “Sitecore.Search Crawler is a component in Sitecore that scans a specific storage system such as a database or file system, extracting information and storing it in a search index, making it available to Sitecore Search. It performs several roles – Indexer, Crawler and Monitor”. – Sitecore Search and Indexing document
  • Note: The above solution is only applicable forSitecore CMS 6.6 and above.
    • One thing to bear in mind is that if one can, have the indexing done in regular intervals rather than constantly running in the background during the import. This even furthers the speed of import. Updating the setting <setting name=”Indexing.UpdateInterval” value=”00:50:00″ /> to say 50 minutes and using that window to import the files.

Another aspect is to try and import the files into the web database than master database. Web database is lot faster, purely because by default it’s not connected to History Engine, whereas master database is connected.

But at the same time, as Kern Herskind Nightingale points out to me, if one is importing items into web database they could be wiped by a publish. Also turning off indexing while the upload of data is going on will be a wise step.

I am aware that this solution is applicable for Sitecore 6.6 and above, and now there are other versions of Sitecore like 7.0, 7.1, 7.2, 7.5 and Sitecore 8 coming out soon for everyone, but there are still clients out there who are still using Sitecore 6.6.

Happy Sitecoring!

Use Name Value List field type, it’s there for a reason

Couple of months ago, I came across a solution that was put in place in Sitecore to have Name – Value pairs or Key – Value pairs for calculation purpose. But the data template’s field names were used as Key and the content editor would enter in the Value. This seemed wrong at that time but I could not think of an apt change to make that solution better and simpler. Recently, while learning about Sitecore fields and field types on my SND course I came across the field type – Name Value List, Straight away I thought, the solution that I had seen a couple of months back could benefit from this field type and could be done a lot better.

The solution that was put into place was just seemed bit odd and have shown it below.

1. A template with a list of fields was created which were the Names, like if one is trying to create a Name – Value or a Key – Value Paired list.

2. The image shows the template created.

Sitecore Data Template

Sitecore Data Template

3. The final template was then inherited from the above template. Showing the inheritance in the below image.

Sitecore Data Template Inheritance

Sitecore Data Template Inheritance

4. The item created from this template was filled out my the content editor. Filled values shown below.

Item created using Template

Item created using Template

By now we get the picture of what is going on and how much maintenance overhead this can be.

Applying the knowledge that I learnt at the SND course, seemed the right way to correct this odd solution. The data template should be modified by changing the field type to Name Value List.

Correct Template

An Item based on this template should be created. Below image shows how the content data should be stored in a Name Value List field type.

Name Value List in Sitecore

Correct way to implement Name Value List in Sitecore

The raw value of Name Value List is stored in a url string format – value_1=20.5&value_2=21.5&value_3=22.5&value_4=23.5&value_5=24.5.

There is no Sitecore web control for this field type.

Lastly the use of Sitecore.Web.WebUtil class comes in handy.

private void FetchNameValueList(Item item)
            string _urlParamsToParse = item["NameValueListFieldName"];
            NameValueCollection nameValueCollection = Sitecore.Web.WebUtil.ParseUrlParameters(_urlParamsToParse);

            foreach(var nv in nameValueCollection)  
                // Do Stuff

Enumerate through the keys to retrieve the values from the field.

Hope this post gives a better understanding of Name Value List field type and helps implement better and simple solutions.

Thanks to Jason who taught me the recent SND course at Sitecore that inspired me.

Finding specific renderings using sitecore query

Recently I was helping a content editor by moving items in the content tree. We successfully move almost all items apart from a couple. These were advert items which referenced on various pages within the renderings using data source. Hence referenced as path and not the Sitecore Item Guid. Some moving them would break the links and we didn’t know on which pages these were referenced.

We did a search using Sitecore Rocks but the way these were referenced and the current version of Rocks didn’t support path searching. We were bit clueless. One of my fellow developers suggested writing a bit of Sitecore Query to get what we were after.

We wrote something like

select @@id, @@name, @__Renderings from /sitecore/content/home//*[contains(@__Renderings,’ds=”/sitecore/content/home/caring/apply for job”‘)];

This helped us find a specific type of renderings which referenced using data-source and they were placed within specific sublayouts.