CAML queries of Lookup columns support query by ID

When you run a CAML query, you have to specifiy a value you’re searching for:

query.Query = "<Where><Eq><FieldRef Name='MyField' /><Value Type='Text'>WhatILookFor</Value></Eq></Where>";

Well, that’s fine, but what about lookup columns? They actually have 2 elements – the text, and an ID.

E.g. You might have a lookup to a ‘Country’ list, and “United Kingdom” might be ID 45.

If I wanted to find items on my list that referenced the “United Kingdom” value, my query would be:

query.Query = "<Where><Eq><FieldRef Name='MyField' /><Value Type='Lookup'>United Kingdom</Value></Eq></Where>";

However, this doesn’t make much sense. The ID is our primary key to the ‘country’ list. Why are we querying by value? I mean, if the United Kingdom had a revolution and suddenly became, say, the ‘United Republic’, all of my CAML queries would break.

(That might be the least of my worries, but you get my point).

So, we’d like to query by ID. Well, you can – I’ve mentioned it before, and the MSDN documents neglect to mention it themselves. There is a property on the FieldRef of LookupId. Set that to True, and you can query by ID.

query.Query = "<Where><Eq><FieldRef Name='MyField' LookupId='TRUE' /><Value Type='Lookup'>43</Value></Eq></Where>";

Edit: One of my colleagues pointed out that in SharePoint 2010, if you update the text of a referenced item, it is updated within the items that reference it. This is different to SharePoint 2007, where the referencing items retained the original text value. He’s right – but I’d still prefer to query by ID as it’s indexed, unique, and simple.

CAML queries of Lookup columns support query by ID

Don't Reuse SPQuery objects!

For God’s sake, don’t reuse SPQuery objects!

Now, with that out of the way, let me explain. I’ve inherited some code. In that code, it’s performing a couple of queries on a List, using an SPQuery object. It’s querying a boolean column, and only one item in the list should be true. It’s a long story why, but that’s the way it is. The list is something like this:

The code needs to query this list for true items (which should only be one) and false items (which you can see above, is more than one). However, these queries didn’t seem to be working. Continue reading “Don't Reuse SPQuery objects!”

Don't Reuse SPQuery objects!

Practical SPDisposeCheck – Namespaces, Delegates and RootWebs

SPDisposeCheck is a great utility for checking the disposal patterns you’re using in your SharePoint solution. Believe me, even experienced practitioners make mistakes; object disposal in SharePoint is hard.

Now, every time someone says that SPDisposeCheck is great, someone posts back that it isn’t perfect. They’re right; it won’t pick up every invalid disposal pattern – but it’s pretty good. Given the choice of it or no check, it’s a clear winner.

I’m not going to describe it much – there are good articles already out there about the problem, and how to use SPDisposeCheck. It’s also well worth installing the latest version, which integrates with Visual Studio. That’s brilliant – you can run a test over your assemblies with each build.

However, recently I took over someone else’s code; SPDisposeCheck proved invaluable, but quirky too. This post will examine some of the practical issues that I found with using it.

Continue reading “Practical SPDisposeCheck – Namespaces, Delegates and RootWebs”

Practical SPDisposeCheck – Namespaces, Delegates and RootWebs

Assembly Versioning in SharePoint

Note: Although quite a lot of this post is about using SVN data in versioning, the idea of using the AssemblyFileVersion works with other systems too

Knowing what version of your code is running is important. Last year I was working on a product where the build process actually put the SVN revision number into a constant in a file – which was then compiled into the assembly – and then the product displayed that version on various configuration pages. This was cool, ‘cos it made it very easy to tie the version of a customer’s code that had a problem to a revision in SubVersion. It proved really bloody useful – the number of times that the ‘latest version, which had definitely been installed’ hadn’t been was … surprising. Thus, I promised that I would do similar in my solutions. However, putting the revision number into a file full of constants seemed … untidy. Continue reading “Assembly Versioning in SharePoint”

Assembly Versioning in SharePoint

How many sub-sites can a site have?

Last year I was working on a system where we were creating a lot of sites. I mean, tens of thousands of sites, and all within one site collection (they’d all have a few documents and tasks, but not many). Anyway, we were told by a partner’s developer that:

  • A Site Collection’s Root Site could only have 127 subsites before performance issues would start
  • Each of those subsites could have up to 2000 subsites before performance issues would start

This seemed a bit suspicious to me. I had read on MSDN about the capacity boundaries and limits, and knew about the 2000 subsites per site note:

Subsite
2,000 per site view
The interface for enumerating subsites of a given Web site does not perform well as the number of subsites surpasses 2,000. Similarly, the All Site Content page and the Tree View Control performance will decrease significantly as the number of subsites grows.

Okay, that’s fine. And I knew that the limit on the number of Sites per Site Collection is 250,000. But I couldn’t find anything about this ‘127’ subsites under the root site thing, and I couldn’t help but wonder if this was where the ‘127’ value had come from (125 x 2000 = 250,000) as I simply didn’t see why a root site should be any different to any other type of site. Thus, I decided to test it. Continue reading “How many sub-sites can a site have?”

How many sub-sites can a site have?

Portal Site Connection and Publishing Sites in SharePoint 2010

The Portal Site Connection can be a useful way of making your breadcrumbs link to a site collection that is ‘above’ them in the logical architecture of a site. It’s also a good way of navigating out of Search Sites and My Sites – which are kind of navigational black holes.

Unfortunately, they don’t work in Publishing sites. I investigated why. Continue reading “Portal Site Connection and Publishing Sites in SharePoint 2010”

Portal Site Connection and Publishing Sites in SharePoint 2010

FAIL: SPCriticalTraceCounter and TraceLevels

I’ve been looking at how to use the SPCriticalTraceCounter class for outputting messages in the Developer Dashboard. I came across a good post about how to do this by Frode Aarebrot, but it left me puzzled – what was the traceLevel integer about? Why wasn’t it an enumeration – like the Microsoft.SharePoint.Administration.TraceSeverity enum? Why those magic values? What happened if I used a wrong value?

Continue reading “FAIL: SPCriticalTraceCounter and TraceLevels”

FAIL: SPCriticalTraceCounter and TraceLevels

Hiding a Tab Group from a custom list type in SharePoint 2010's Ribbon

I have a custom list definition that I’ve been writing. The ribbon on it looks something like this:

Unfortunately, I don’t want some of the groups shown on this tab. For example, I don’t want any of the ‘Connect & Export’ group, or ‘Customize List’ – these things could actually break my solution. So, how to hide?

Initially, I looked at the HideCustomAction Feature element. This what you’d use for hiding links to settings pages, etc., and it seems a natural choice. Unfortunately, it doesn’t allow a RegistrationId in the same way that a CustomAction element does – so there was no way to restrict this to my list only. Damn.

Continue reading “Hiding a Tab Group from a custom list type in SharePoint 2010's Ribbon”

Hiding a Tab Group from a custom list type in SharePoint 2010's Ribbon

How DisableEventFiring / EventFiringEnabled works

I’ve got an event handler on a SharePoint list that’s fairly long running, and this then raised a question in the office – do these settings control event firing for the currently running event handler, or for the entire list?

Very often you see lines of code like this…

this.EventFiringEnabled = false;
item.Update();
this.EventFiringEnabled = true;

… but was this really necessary? Are people worried about events not being handled ‘cos firing is disabled, or is this just a convenient way of tracking whether events are enabled or not? Continue reading “How DisableEventFiring / EventFiringEnabled works”

How DisableEventFiring / EventFiringEnabled works