Umbraco Lucene Date Range fun

March 22nd, 2013 No comments

You know days when you think everything is fine, then you hit a snag and it takes the rest of the day? Yup. That happened today. The issue this time was with an Umbraco implementation I am working on at the moment.

With searching using indexes in Umbraco it’s all rather buggy. DateTimes are stored as the format: yyyyMMddTHH:mm:ss in Lucene when Umbraco indexes. The issue with this is that it stores in Lucene as a string. So when you do a query in Lucene for a date range. It’ll give you some pretty gnarly results. Lucene expects the format: yyyyMMddHHmmss000

The best thing to do is to convert the string before putting in the index. Umbraco has a nifty Interface you can use for this called IApplicationEventHandler. This is automatically hooked up when Umbraco finds it in your assembly.

You can then use the following method to add the custom string in:

public void OnApplicationStarting(UmbracoApplication httpApplication, ApplicationContext applicationContext)
{
    var siteIndexer = ExamineManager.Instance.IndexProviderCollection["IndexerName"];
    siteIndexer.GatheringNodeData += GatheringSiteIndexDataHandler;
}

void GatheringTagIndexDataHandler(object sender, IndexingNodeDataEventArgs e)
{
    var node = new Node(e.NodeId);
    e.Fields.Add("yourdatefield", "your new date string");
}

If you want to override a field already in Lucene, you can just remove it first by using:
e.Fields.Remove("yourfieldname")

Good luck!

Setting up Sitecore on Amazon RDS

November 27th, 2012 1 comment

This week I have had a bit of a nightmare setting up a client’s site using Amazon’s Relational Database Service (RDS). The issue arises because you can’t just restore a .bak file on RDS as there is no access to the service’s file system. I read a blog post here: http://blog.xcentium.com/2012/06/how-to-install-sitecore-on-aws-using-amazon-rds/. This didn’t really fix any issues and caused errors. I tried scripting the database and it’s data which kind of worked, but caused some data to not work properly, mainly the membership provider.

I looked into Red Gate’s SQL Packager and realised it creates a .net executeable of the full database. Originally I rejected this thinking it would need access to the RDS file system again. How wrong I was. You can just run it and connect to the RDS instance and away it goes. If you’re working on RDS at all with Sitecore, this is a really useful tool.

Replaceable token issue with config ConnectionString transforms

September 25th, 2012 No comments

I ran into a little conundrum the other day trying to make a .net C# project build and deploy. I was making a build using MSBuild which also transforms config files. This was all working, but in the config’s ConnectionStrings element, the transform was coming out with:
<add name="Connection" connectionString="$(ReplacableToken_ProjectName-Web.config Connection String_0)" providerName="System.Data.SqlClient" />

Now, I thought that $(ReplacableToken_ part would be properly transformed for the relevant environment like:
<add name="Connection" connectionString="[LiveDB connectionstring]" providerName="System.Data.SqlClient" />

The issue is that MSBuild actually does this on deploy, not on build. It’s a feature. On build, it just creates a placeholder. You can, however, make MSBuild switch the connectionstrings on build by adding the following line of xml into your .csproj file inside the first PropertyGroup:

<AutoParameterizationWebConfigConnectionStrings>
False
</AutoParameterizationWebConfigConnectionStrings>

I’ve only found this for Web config connectionstrings so far, the others seem to just work immediately.

Categories: asp.net, C#, Deploy Tags: , ,

SQL Migration to Azure

April 17th, 2012 No comments

I’ve been playing about with Azure at work recently to test it out and see if it will work as we need for clients. For the most part it has been blindingly easy to set up. But one thing I had a few issues with was migrating my SQL data to SQL Azure (on the Azure platform there are 2 database types, rather helpfully named Windows Azure – which is a noSQL database and SQL Azure – which is based on SQL server).

As we have developed a site using Umbraco, we wanted to package the database and just restore it on Azure. You can connect to SQL Azure by SQL Server Management Studio (if you do, be sure to install SSMS 2008R2 as 2008 will not work properly) but you can’t backup and restore an Azure DB using this method. Enter the SQL Azure Migration tool, which is very simple to use. You connect to your SQL server, choose your database, then connect to your SQL Azure instance and select which DB you want to restore to. Easy life. One gotcha though: You can only restore SQL server 2008 R2 database. If you try an older DB you will get errors, so try and upgrade before even considering Azure.

Returning a default with generics

March 28th, 2012 No comments

Today I was working on a generic method which gets a querystring and converts it. If the querystring exists, it would convert the type and return it. However, I’d also need to return a default if the querystring doesn’t exist. As it was a generic method, I couldn’t rely on just returning a null as if the type was an integer, this wouldn’t work (assuming not a nullable integer).

I disovered this nifty little method though:
return default(T);

Lovely, it will then respond appropriately, if an integer, it would return a 0, if a string, a null.

Categories: asp.net, C# Tags: , ,

TestCaseSource in Nunit 2.5

February 29th, 2012 No comments

Recently, one my team mates discovered the TestCaseSource attribute in Nunit 2.5. I have been using the TestCase attribute for a little while. It enables you to tes multiple inputs with one test:

[TestCase(12,3,4)]
[TestCase(12,2,6)]
[TestCase(12,4,3)]
public void DivideTest(int n, int d, int q)
{
  Assert.AreEqual( q, n / d );
}

This is useful for various reasons, mainly that you don’t have to write a separate test for each input. TestCaseSource is a different way of doing this by allowing the user to build up an IEnumerable of test cases and just add that to the test.


[Test, TestCaseSource("DivideCases")]
public void DivideTest(int n, int d, int q)
{
  Assert.AreEqual( q, n / d );
}

static object[] DivideCases =
{
  new object[] { 12, 3, 4 },
  new object[] { 12, 2, 6 },
  new object[] { 12, 4, 3 }
};

This is helpful as you won’t have a load of testcase attributes at the top of each test, and you could reuse this data. Lovely!

Upgrading Sitecore causes Global.asax to not work

February 29th, 2012 1 comment

AAAAAHHHH! This fix, I hope, will save you a lot of time. Recently we have been upgrading some clients to the latest Sitecore. Generally that works fine, however, on re-compile, then running the application, I get a nasty StructureMap Exception Code: 202 (we use this lovely little nugget for IoC). Panic ensues. You check everything, it’s all in place. You check the Global.asax in Visual Studio (where we kick off Structuremap and any application wide methods), it has all the code there. And there’s the problem. Visual Studio opens global.asax.cs by default, which is correct really, code should really only be in there. However, if you open the global.asax file in your favourite text editor, you see the issue. Sitecore replaces the global.asax file with a default, which has default, empty code and methods in it! It looks like:

<%@Application Language='C#' Inherits="Sitecore.Web.Application" %>
<script runat="server">
public void Application_Start() {
}

public void Application_End() {
}

public void Application_Error(object sender, EventArgs args) {
}
</script>

What this means is, although Visual Studio sees all your code in global.asax.cs, that code is not running because it isn’t even referenced by the global.asax in the first place, it should look something like this instead:
<%@ Application Codebehind="Global.asax.cs" Inherits="YourSiteAssembly.Site.Global" Language="C#" %>

Infuriating? Very! Just keep an eye on that global.asax file! I hope this saves you some time and a lot of pain.

Sitecore desktop freezes

February 21st, 2012 2 comments

While working on a site recently, I noticed that trying to get to the Desktop version of Sitecore, it would get stuck just in a request state, nothing happening. This was always after logging in, the Sitecore login page itself worked fine. It would also not log any issues in the log files. The thing that fixed it was to make C:WindowsTemp a writable folder for the IIS user. Almost immediately it worked. Phew!

I hope this helps :) .

What’s that duck for?

February 12th, 2012 No comments

I’ve written about this before in my last, not so focused blog, but I’ll write it again. There are a few simple non-programming programmer rules I like to adhere to. These would be things like KISS (Keep It Simple Stupid) etc. Another lesser known one of these is Rubber Duck Debugging.

The duck on my desk

When you hit a conundrum while coding, it is very easy to sit and suffer, draw diagrams etc. Many (including myself) tend to internalise this and not speak it. Usually, if you then ask another programmer, by the time you have explained the problem you have worked it out because you have externalised the issue.

Rubber duck debugging gets round this (sort of). You get a rubber duck (you should always have one on your desk) and when you hit a conundrum, you pick the duck up and you explain the issue you are having. By doing this, you externalise the problem and it can really help. It feels daft, but it can really cure the issues of Cognitive Dissonance and make you feel much better.

Of course, asking someone else helps even more, but if they’re not about/you feel stupid asking because it feels like a simple issue, this is a great back up.

And that, ladies and gentlemen, is why I have a rubber duck on my desk.

Supersized Jquery library

February 12th, 2012 1 comment

I keep saying this: if .net and C# (or just back end technologies), then HTML, Javascript and CSS (front end technologies) are my mistress. I love to dabble with these lovely pieces of simple but great technologies.

One of my latest things is to take a library off Github (let’s not forget Bitbucket too!) and start playing with it. Recently, I needed a way of easily animating a background on a page and by chance stumbled across the Mishkin’s site: http://mishkins.co.uk/. Apart from ogling the food, the background transitions are amazing, even if you have low resolution images. I decided to peruse the source code to find they use a Jquery library called Supersized, a library which allows backgrounds to be loaded and displayed like a slideshow with lovely effects. It even handles stretching the image as you need and various other options.

Have a go. I love the way the web has progressed.

Switch to our mobile site