SPQuery

SPQuery for my tasks

Developing solution with multiple languages (or a language which is not English) sometimes can be a bit painful. To configure a Webpart to display only my tasks, I would filter for [Me] or [Ich]. To achieve the same via code / CAML, you can filter by UserID and not the string “Me”. 1: <Where> 2: <Eq> 3: <FieldRef Name="AssignedTo" /> 4: <Value Type="Integer"> 5: <UserID /> 6: </Value> 7: </Eq> 8: </Where> 9: <OrderBy> 10: <FieldRef Name="

Empty Admin Recycle Bin items

What is it? Usually the size of the recycle bin is not relevant. But on development machines, you don’t want lots of files in there, which make your databases grow without actually used data. What do you do? Go to the recycle bin, click on “Site Collection Recycle Bin”. The two stages of the recycle bin can be managed independently. The two views on the left let you switch between the first- and second stage.

PortalSiteMapProvider.GetCachedListItemsByQuery

With MOSS 2007 or SharePoint Server 2010 you can use the PoraltSiteMapProvider of the Microsoft.SharePoint.Publishing.dll assembly to retrieve cached listitems.<span style="color:#606060" id=lnum1 1:</span PortalSiteMapProvider ps = PortalSiteMapProvider.WebSiteMapProvider; <span style="color:#606060" id=lnum2 2:</span var pNode = ps.FindSiteMapNode(web.ServerRelativeUrl) as PortalWebSiteMapNode; <span style="color:#606060" id=lnum3 3:</span var query = new SPQuery <span style="color:#606060" id=lnum4 4:</span { <span style="color:#606060" id=lnum5 5:</span Query = "<Where><Neq><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Neq></Where>" <span style="color:#606060" id=lnum6 6:</span }; <span style="color:#606060" id=lnum7 7:</span SiteMapNodeCollection quoteItems = ps.

SPQuery with lookup columns returns no data

An SPQuery for lookup columns should be easy. Start the famous U2U Caml Editor, configure your query, and query the list. This query will get a result if the lookup column stores something like “1;#1”. But if its value is e.g. “1;#Title”, the query will not return an item. So what can we do about it? Add a LookupId=’TRUE’ to your query, so it will look like this: <Where> <Eq> <FieldRef Name='LookupField' LookupId='TRUE'/> <Value Type='Lookup'>2</Value> </Eq> </Where> Remember to omit the Query tags from the U2U Caml Editor when pasting the query into your Visual Studio source code!

Summary: Common Coding Issues

The Best Practice MSDN page has some interesting hints to generate better code. In this post I write about some point of that article, provide samples and fix bugs which are in the MSDN article. Caching You should only cache thread safe objects. What’s that? This means, that you should only cache objects, which can not be changed from the outside of your code. An itemCollection (as list.Items) is changed, if another user adds an item.

SPQuery, ViewFields and empty fields

If you want your query to return empty columns, you have to add Nullable=’TRUE’ to the viewfields. If you do not add the Nullable attribute, accessing the results of the query like this: oListItemAvailable[“Field1”] will give an Exception. I’ve made posted a comment about this on the page in the MSDN http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spquery.viewfields.aspx

Get a listitem by ID

Fetching a listitem by ID will generate an error, if the a listitem with the ID does not exist. To avoid this exception, you can get a listitem by id by searching for it:private SPListItem GetListItem(SPList List, int ListItemID) { try { string defaultView = List.DefaultView.Title; SPQuery query = new SPQuery(List.Views[defaultView]); string caml = String.Format("{0}", ListItemID); query.Query = caml; SPListItemCollection results = List.GetItems(query); if (results.Count == 1) { return results[0]; } } catch (Exception ex) { _ErrorMessage +=