If you ever catch yourself having to create List or Document Library Views programmatically – beware the CAML monsters!

J/K…

But you really do want to look out for how you generate and order your CAML if you are creating filtered views. I was working with a colleague a little while back and had the most awesome 2 days worth of troubleshooting sessions you could ever imagine…Not!

Here are the symptoms and how we troubleshooted:

The custom Web Part creates a unique view to a SharePoint List depending on various input data from a user. When the view is displayed in the regular List view everything looks ok. When that view was switched to the datasheet view, everything looked ok. But when the generated view goes directly into Datasheet View (which was an option), the data in the Datasheet view was funky. It was displaying some List items multiple times and omitting other items.

At first we thought that there might be something wrong with the List integrity, so we exported the List data to Excel. Data was there and it looked fine. Then we looked back at the List and Item ID’s to see if for some reason there were multiple items with the same data. Nope, the Datasheet view was showing the same items with the same ID’s multiple times. Then we tried to create a Datasheet view manually to see if it was something with the Web Part code or if it was a SharePoint bug. As it turns out, it was a little bit of both…

Using SPManager to carefully inspect and scrutinize all the properties of both the view generated by the custom Web Part and the view that was generated via the web UI we stumbled upon some suspect CAML! Dun dun dun….

Our custom Web Part was using CAML that looked something like this…

<Where>
   <Neq>
      <FieldRef Name="ContentType" />
      <Value Type="Text">sdfgdf</Value>
   </Neq>
</Where>
<OrderBy>
   <FieldRef Name="Title" />
</OrderBy>

The CAML that was generated via the List interface was like this:

<OrderBy>
   <FieldRef Name="Title" />
</OrderBy>
<Where>
   <Neq>
      <FieldRef Name="ContentType" />
      <Value Type="Text">sdfgdf</Value>
   </Neq>
</Where>

See the difference? We didn’t want to believe that that was it at first, but we updated the CAML for the Web Part and that got rid of the duplicate items in Datasheet view bug. What a load of mush right? Oh wells, problem solved.

A quick message from a sponsor: 

Tech Tip : Looking for a reliable collaboration tool? Try out hosted SharePoint and Exchange Add-Ons on high performance citrix vdi from CloudDesktopOnline and experience the ease of remote accessibility from your preferred device(PC/android/iOS) powered with 24*7*365 days top-notch technical support from Apps4Rent.

Advertisement