getting things together.

Seems like I always have excuses for not writing, still i urge to get stuff written, archived and stored in some safe place. Lately I have been struggling with some dns servers after move of domain. But everything should be sorted now as it seems.

Silly stuff like thinking that if you move a domain to a new host, they will register it with their own name servers. This seem to not always be the case, and I had to re-register it myself on the name servers that my new host provide. Not sure if it was my old host ( who made it hard or my new host (binero) that didn’t care much about it.

Side effect was that no matter what dns/alias i registered/changed for this domain, it had no effect what so ever and continued to point to an IP on the old host.

Anyhow. Stuff moved, set up in new environment and ready to go again. Now I just need to decide if to create my own site for blogging, or continue using WordPress since I’m lazy. The problem is, all cool stuff i know how to do is in .net. Php is something i left behind a very long time ago. Still. If you work with web, I feel you should do your own stuff. So most likely, Instead of writing, I will code something new that maybe some time gets finished, or I’ll just continue whine about how i never get stuff done ^^.


by chance i was dragged into an Umbraco project at work, I did work with Umbraco before, but it was like 5 years ago and I was not that impressed. The thing that bothers me most is that you build your site through a web interface and not with code first.

But things change and some stuff become availible, or atleast starts to get availible. I’ve tried out the Umbraco 7.4.3 and I must say that i start to feel some potential in it again. So much that I will most likely build a personal site around it.

I hope to get better at writing stuff down as well. I’m going on parental leave for 3 months soon, maybe that will let me sort stuff out when it comes to documentation =)

EPiServer 301-handling

Got some room to breath today, so decided to split a simple 301-handling I did some weeks ago into it’s own project and “put it in the git”. Also made a nuget for it if anyone wants to try it out.

I like nugets, if done correctly they make it very easy and clean to try code if you want to, and I’m trying to make it a natural part of when i package stuff for safekeeping.

So what does this thing do then?

Basic 301-handling for EPiServer 7.5+. Support for Domain-redirects. Based on XML-files to be as simple as possible doing nothing more and nothing less.

If domain-redirects exists, that request will be routed first with persistent relative path.

Routes will catch what EPiServer does not want and send it to controller for checking if it’s a 301, if it is, a permanent redirect will trigger to new place. If not, a 404-exception will be thrown falling back on website configured error-handling.

Drop bin-file in your bin-directory. All is IInitializable modules and will hook onto EPiServer.

Optional configuration, add to appsettings

These are also the default paths if no appsettings exist.
XML should look as follows

I trim surrounding /, as well as ignore lower/uppercase on compare. So not that important.

So what about errorhandling?

To accompany this module I usually redirect to a page in EPiServer. Webconfig as

Then I set a page in EPiServer to answer on /error address. The page someone tried to access can be found in TempData[“originalUrl”].

SVG Fallback with DisplayTemplates in EPiServer Mvc 7.5

The requests about doing stuff with Svg starts to increase and it was time to think about how to handle it. I had probobly 5 different complicated ways to do it and since I’m effective (or lazy) i like it to be easy for me to handle.

So what I landed in was actually a pretty simple concept giving the DisplayTemplates some more responsibility. Think I’m pretty ok with this solution. There are some quirks for the EPiServer Editmode where I have some problems with it handling svg as images. But no time for that today. Doing simple failsafe for that place.

So what I do in my DisplayTemplate is to check if there is any more viewData that i care about in this state. Here i decided to use the cssClass on rendering aswell and now check for an svgModel property. If an svg property is present, I render that as main imagetag and I add the original as a fallback. (Here is also the Editmode workaround atm.)

Having this displaytemplate for my ContentReferences with UiHint.Image, i can now pass and extra ContentReference into it as so properties look like this.

and calling it looks like this

That is about as easy as it gets I think. Then I complete it with a js to take care of the fallback if we don’t have support for svg. Using Modernizr.

EPiServer 7.5 handling extra Routedata using Partial Routing

Querystrings are evil and does not make pretty links. We can agree to this yes? So what you normally do is put up routes that can handle all those quirky bastards and put em where they belong.

As an example I’m going to use a newslistpage with pagination. (I’m lazy so i’ll only write relative urls) /newslist. If i want to target a specific page in the pagination, i would normally set up a route to handle it so i could write /newslist/2 instead of the ugly /newslist/?page=2.

But if you are working with a Cms like EPiServer, the paths are not really known all the time and routing changes. This can be a tricky thing and I’ve seen a lot of people just ignoring it and carry on with querystrings since it’s the easy thing to do.

There is a very easy way to deal with this in EPiServer 7+ and it’s called Partial Routing. I was reading some posts about it on and thought, hey i can use that for simpler things aswell. So here is the implementation to use it for your own “extra” route data.

So what it does it add this extra routing to all pages of type NewsListPage. When resolving it, the routing checks if there are any parts of the routing left unhandled. In my case I’m expecting a possible paginationreference, so i try parse it to an int to see if its data i should bother with.

If it fits, I then add that extra data to the RouteData collection. This will make it easily accessible in the Controller.

So to recap, if there are parts left in the routing after hitting the NewsListPage, we handle it in the Partial Router, add it to the routedata collection and then we can fetch it like it was a querystring/normal route in the controller.

To initialize and attach this Partial Routing to EPiServer, we need to load it. I like to keep it pretty clean so I have a class handling it.

And that should be it. No excuse to use querystrings for simple tasks like this ^^

EPiServer 7.5 Mvc Parsing ContentAreas

So this other day I had to get all values from a Page in EPiServer (7.5+) to send to another service. I’ve done it before in earlier versions and thought there would be no problem. However the new ContentArea breaks the pattern a bit and left me confused. Looping through the pageproperties, you could do a switch on the PropertyDataType enum to see what we were dealing with, but not in case of ContentArea it seems.

The funny thing is that there is no ContentArea in that enum and the ContentArea itself says, by Type, that it’s a LongString? So I try do a check on OriginalType and compare to PropertyContentArea, successfully I can identify the ContentArea property. It’s never fun having to compare types though..

What i need to do now is parse everything that is in that contentarea and get the properties for those items aswell. Don’t know how often you do stuff like this but it’s a rather special case and something that is also pretty tight cached, so should be no worries.

The GetExternalUrl() that i use are extensions to get pretty links, which i want from everything that is a PageReference or a ContentReference. This of course varies what you are using em for. But should give you the basic idea.

LinqToTwitter Server OAuth Mvc c#4+

Recently I had to update some libs in a Project we are working on and one example was LinqToTwitter, which is a library I’m very found of, well documented and supported.

Still there are a few steps that documentation does not cover if you are a bit narrow in your knowledge of c# like me. One of the things I did not have the chance to work as much with before is Mvc and especially making stuff async.

LinqToTwitter library got full support for async right through, and also more or less forces you to implement it async aswell in the version i got from Nuget (3.0.4+).

It’s not a major issue since i do the Server OAuth by opening an extra view with Javascript, but still controller needs to be async for it to be happy. This was fairly simple to accomplish in the end (when i learned what to look for). So this is a basic async Mvc controller, note the inheritance on the base.

The thing with keyword “await” is that the following code will not be executed until response is given, but instead of locking the Thread waiting for the response it goes on and handle other stuff meanwhile.

I can see how this is a great thing when it comes to posting stuff, reading posts etc where you are not depending on what the response is. But I’m not so sure it is a good idea when it comes to Authentication. Fine if it’s about logging in and stuff, but when requesting OAuth permissions with tokens and stuff, i rather know the result before i move on. Like in this case we have settings depending on if the result was a success or not.

If you are trying to make a controller async but got trouble returning awaitable results from other libraries, for example if you have a big project and not all parts are awaitable, you can pretty easy put em into a queue anyway, making them awaitable.

AngularJs sharing values between controllers

Been a while since i last posted but thought it was time to start sharing code again. So I’m doing a lot of AngularJs these days and sometimes you get the need to share values between controllers, or let’s say 2 controllers wants information from the same place, but you don’t wan’t to do 2 separate requests.

We can do this by creating a service for the controllers to talk with. My Service needs to get the value from another server, so I’m doing that with a request.

What you might find odd or stupid or whatever, is that I’m requesting Json with a Get, but since it’s only a number you can access, i deem it safe and allow it from the controller. The reason is that i also use this service to check if all layers of the app respond, so to make it easy.

So what you can see here is a method named “query” that let me ask for an updated value, and we have a variable named skierCount that holds our value. To start its just “-“.

Then my 2 controllers that share this service.

We have the base controller named “systemStatisticCtrl” that also have the responsibility to update the value of skierCount every minute and set the shared variable.

After we updated the value, we broadcast on the rootscope that event “updatedSystemStatistics” triggered. Both controllers listens to this event and knows that they should update their variable from the service.

As you can see the slave controller (“systemStatisticCopyCtrl”), brilliant name i know, only has that purpose, initially set that variable from service and then be on standby for eventtrigger that it changed.

the view for the app

Export DataTable to Excel

So this is part two for me where I’m doing a custom Export from XForms in EPiServer. Check prev post if you don’t know how to get from XForm to DataTable.

So ye this is pretty simple and you can find the information about it on a lot of places, problem is I had some special problems with the export and line breaks You see the XForms i need to export are from start send emails. So i need to preserve the line breaks from the emails. If you export the data as it is, you will most likely get a new row in Excel for every line break in your html. And that kinda sucks when you export lots of emails.

So what i do is i save the Plain Text part from my Emails and I replace the Line breaks with ‘|’ before saving them to XForm. Saving to XForm automatically removes all normal line breaks (“rn”) as you use in Plain Text formatted email. Therefor i replace it with the single ‘|’ char before saving (This is a char i don’t normally use in the mails).

So to start we set the header:

As you can see i do a Replace() on “|” into a
before entering the value. This is cause i saved my line breaks as that char to preserve it in the XForms.

You are free to add whatever Css you feel like to this table when rendering it and Excel will do its best to show it. The magic however lies in the style attribute I set at the top:

br { mso-data-placement: same-cell; }

This prevents the line breaks i add (
) to render as new rows in Excel and do a normal line break within the same cell instead, making our body easy to read. remove it and you will see the difference 🙂

Load XForm as DataTable

Sometimes I run into small peculiar problems that clients want solved, for instance this other day i had a request that demanded me to collect XForm data and handle it elsewhere. It’s not that complicated but if you are not used to parsing XML it might be a bit tricky. Anyways, I’ve split the functions a bit in my repository so first I load the right XForm on init.

After the init I can then run my function for converting the selected XForm into a DataTable. It’s done so i should be able to iterate through all XForms if i feel like it and create a DataSet with all the Xforms as Tables.

So what I do is get all the elements from the XForm. Since I navigate the nodes i need to convert them from XNode to XElement for easier access to properties. Then i check if the column exists in the DataTable, if it does not then i create it and add the values to my valueList. I do this since I need to insert all column values at the same row, hence the cast to array after.

So ye thats about how simple i now have my XForm as a DataTable instead to do fun stuff with.