Blog migrating

Hi everybody! I’m moving to a new domain: http://gergelykoncz.com. I’ll transfer the worthy content to the new domain slowly but this blog won’t be refreshed any more. The main reason behind moving is that I’d like to focus more on software design and put much less emphasis on Microsoft certifications. Also, I’d like to publish content under my real name. So see you on the new site!

 

70-483 Programming in C#

Last week I sat 70-483, Programming in C#. It was beta period so it was (at least supposed to be) free. The exam experience had a little glitch: I signed up in one test site (not my usual one, they were full). Luckily I had the extra precaution to call them before I’d go there (but after booking the exam). It turned out that even though I could book the exam online and they stated they had seats to that day (which was deadline of the beta period) nobody will be in the center because of summer vacations. So I had to pay an extra $25 as a transfer fee to be able to go to another test site.

OK, now about the exam: it was easy. And it was really about the C# language, not the .NET framework. So there were questions on syntax, too.

 I’m working with C# for four years now so there weren’t too many surprises so it took about an hour to finish the questions, review them and leave.  You can view the exam topics here.

XsltListViewWebPart with custom XSL parameter

Recently I had the requirement in the title: we built a custom webpart based on the built-in XsltListViewWebPart. Microsoft wisely follows the best-practice pattern composition over inheritance, making the said webpart sealed (yes, it was sarcasm) so we generate one on the fly and we needed to pass some parameters to these generated XsltListViewWebParts and be able to access and work with these parameters in the XSL template.

The solution isn’t particularly hard but it took me a lot of googling, so I thought it might worth sharing it here. It has the following steps:

  1. Add your custom parameter to the XsltListViewWebPart’s ParameterBindings property.
  2. In the XSL file create an XSL parameter with the name of your custom parameter.
  3. Select the value of the XSL parameter in the XSL file to be able to work with it.

The first part was easy, especially with the help of Stefan Stanev’s post which covers the ParameterBindings property in great detail. There’s only one gotcha here: the type of the ParameterBindings property is string. I wasn’t become too depressed by this fact, simply concatenating your custom value will do the job:

myWebPart.ParameterBindings += @”<ParameterBinding Name=””myParameter”” DefaultValue=””Hello!”” />”;

 

The second part is easy too: just add an XSL parameter to your XSL file, like:

<xsl:param name=”myParameter” />

After you’ve done this, you’ll be able to access the value of the parameter (in our case, “Hello!”) using the following XSL query in your XSL file:

<xsl:value-of select=”$myParameter” />

Well, that’s all. Hope it helps someone out.

 

SharePoint Genesis

I’m preparing some posts on dealing with lists in a very cool way, but the whole concept is revolving around SharePoint Genesis, an open source tool I don’t think everyone is familiar with, so this post is a brief intro on the topic (and not an advertising material).

We’re working on a pretty large SharePoint project which involves fiddling a lot with technical lists and content types. I haven’t got a massive SharePoint experience (five moths so far) but I found that I (and every other developer I work with) have serious issues with these things put together by hand.

Once I had to deal with the fact that a content type was generated differently on my development server, on our integration server, on the client’s development server and even on the production server. And yes, it was put together by the very same person, in a few days’ difference. Try to get SPMetal work with that one.

Of course there’s a solution to this problem – and it is open source and downloadable from spgenesis.codeplex.com. You could have guessed that it’s called SharePoint Genesis. In a nutshell it’s a list-generating framework for SharePoint. Using it we were able to wrestle complete control of every list used in the project from consultants. It gives you type-safety and can be easily used similarly as an ORM tool. Fields, content-types, lists, event receivers – it provides everything needed to store and retrieve data from SP.

Even better you can easily wrap it around an existing list and using the repository pattern it’s easy to access the underlying data from code. Be sure to examine it and experience with it before you make a decision. The main drawback is the lack of documentation (and for me it was very strange to work with inner classes as if they were properties (which they really should have been)), but you can always read the source.

CAML Query Quirk

Recently I had the requirement to be able to retrieve items from a SharePoint list by using CAML queries in a custom webpart. I know the preferred way is to use LINQ to SharePoint, but the particular problem couldn’t be solved by that (and management clearly wanted CAML).

The problem

We store user preferences in an SQL Server table, and in this case, we had negative preferences, things the user doesn’t want to see. The task was to filter a list by a lookup field where the values doesn’t fall in the user preferences. I quickly thought the following:

<Query>
 <Where>
  <And>
   <Neq>
    <FieldRef Name=’MyField’ LookupId=’TRUE’/>
    <Value Type=’Lookup’>1</Value>
   </Neq>
   <Neq>
    <FieldRef Name=’MyField’ LookupId=’TRUE’/>
    <Value Type=’Lookup’>2</Value>
   </Neq>
  </And>
 </Where>
</Query>

Of course the actual query was generated by some C# code, nothing hardcoded.

And it worked nice – I left it as it is. One or two days later the code started to break down – no results arrived from the list. It took a while to figure out that the only thing changed was that someone added a third option to the preferences. After fiddling around with for a while it became apparent that the query is working with two values, but not with three (or for the record, anything higher than two).

After some hours of looking around I found a someone commenting on this behavior – CAML queries work only with two (or one) elements on a given level. So the following is fine:

<Query>
 <Where>
  <And>
   <Neq>
    <FieldRef Name=’MyField’ LookupId=’TRUE’/>
    <Value Type=’Lookup’>0</Value>
   </Neq>
  </And>
  <And>
   <Neq>
    <FieldRef Name=’MyField’ LookupId=’TRUE’/>
    <Value Type=’Lookup’>1</Value>
   </Neq>
   <Neq>
    <FieldRef Name=’MyField’ LookupId=’TRUE’/>
    <Value Type=’Lookup’>2</Value>
   </Neq>
  </And>
 </Where>
</Query>

But should you add another And tag in the Where element it would break again, so you’d had to break it up like the Neq tags.

Solution

I thought a lot about how to solve this problem recursively – but found a much more practical method instead: I switched the filter from specifying values to exclude into stating what should be included. I used the In element, which has a Values element, where you can specify multiple (yes, more than two, if you wish) values to search for.

Conclusion

I’m astonished that this issue persisted in generations of SharePoint but maybe we should just as well opt for using LINQ to SharePoint and forget how things work closer to the metal (I’m joking of course).

Safari Books Online

I’m not the salesman type, and it’s pretty hard to impress me (OK, it’s not that hard but I hope you get the point) but now I’ll make an exception and advertise a service a little (I’m not interested financially).

I bought many books before – from eternal classics, like Pro ASP.NET 3.5 in C# 2008 and Introducing Silverlight 3. You know, the types of books you read every year or two. OK, I have some more permanent books, like the GOF Design Patterns, for the account.

I did this for a while, and piled up a bunch of books which after the one and only reading (and using a little as reference) typically being used to collect the dust from the room (and contributing to the weight you have to drag around if you are moving).

I always liked gadgets and I thought that since I ordered these books from Amazon anyway I might as well cut back on the shipping costs and bought a kindle (to be fair, it costs almost exactly the same in Hungary, since I have to pay 25% VAT on ebooks, and regular books pay only 5%, figure it out).

I bought books for my Kindle, took them everywhere and read them happily – but prices were still high, which resulted in one or two (mostly one) books a month. I figured that there has to be another way (not the illegal one). I imagined an online service like a library on technical (software development) books I’m interested in. It turns out I was right, a quick google search on the above topic brought up exactly the same question on Stackoverflow – and the answer was Safari Books Online.

I fell in love with it – you pay some (laughable) fee and read for it whatever you like (I’m yet to come across a book I searched and unavailable through the service). OK, you have to be online. Since I live my life in constant internet access it wasn’t a real constraint for me.

So if you are like me and would like to access your learning material cheap and legal then don’t hesitate to try it out. Now they have some nice plans which further reduce the price.

Happy learning!

Blog reactivated

A lot have happened since my last post (it was almost half a year ago). The most remarkable change was my job switch. I found my previous job to be a little too limiting, plus I got an offer I couldn’t resist. So currently I’m dealing (fighting would be a more appropriate word in my case) with SharePoint 2010 and its countless nuisances.

Googling fruitlessly for hours and looking up tautology in the miserable, so-called documentation of SharePoint made me realize that there must be other people out there trying to bend this thing to their will, so I thought I might just as well share my solutions, maybe someone, somewhere will find the useful (and of course, for future reference for me).

So from now on, I’ll make a few posts on SharePoint and its numerous WTF moments.

It might seem that the job switch doesn’t turn out good after all, but fortunately I’m working in a place where they actually take care of the code, and things like TDD, enterprise architecture and best practices are followed (a rare thing in the Hungarian IT industry, I suppose), so there’s a lot of room for me to improve.

Currently there’s no MS learning plan, and my Azure studying ended with my job shift. Nowadays I’m interested more in design approaches rather than specific implementation details. I’m entertaining myself with studying agile methodologies and extreme programming. I’m reading a bunch of books in the topic, so maybe I’ll post a few reviews as well.