Feed your CAML some IntelliSense so it doesn’t bite you in the you-know-what!

camldotnet_80x80 As every SharePoint developer knows, the CAML is an ornery beast. Get too close – it’ll spit at you, and as soon as you get knee-deep in the hot sands of SharePoint-land, it’s just a matter of time before it bites you in the ***!

But wait! There’s hope for all you CAML-geeks!

Announcing the CAML.NET IntelliSense project

I’ve added a new component to the CAML.NET toolset that aims to help you tame that sometimes finicky, but ever-popular and oh-so-useful CAML language.

Click here to download the CAML.NET.Intellisense installer.

You probably already know how to add the SharePoint schemas to Visual Studio so you get IntelliSense when editing CAML files. If not, here’s a link to a great article by my friend Andrew Connell that walks you through the procedure.

This is a good first step, but we can take it even further by copying the core schema files and then extending them so they provide more information. Specifically, we can add the detailed guidance from the SDK that is scattered all over the Internet and that developers seem to have a hard time finding.

Let’s face it – even with an up to date set of bookmarks, who wants to go searching all over the place for that missing tidbit that tells you just what that obscure attribute is for when you’re right in the middle of building the CAML for your feature? If you’re like me, this quickly becomes a major distraction. Hard enough to stay focused in the first place!

Using Annotated Schemas

The main idea behind the CAML.NET IntelliSense project is to extend the core schemas in two ways:

  1. Gather as much information as possible about each element and attribute and place it into xs:annotation elements so that it pops up in context while editing; and
  2. Identify and replace as many xs:string types as possible with enumerated types so the valid values for each attribute also popup in context while editing.

For example, using the core WSS schemas directly, we get a drop-down like the following while editing a Field element:

PlainSchema

That’s better than nothing, but it doesn’t say what the attribute is for, so unless we’re already familiar with it, we have to refer back to the SDK either online or in the CHM file. On the other hand, using the enhanced annotated WSS schema, we can provide more information in context. We only need to refer to the SDK documentation when we really need it. Most of the time, we just need a hint, which is what Intellisense is all about, right?

EnhancedSchema

Extending the Core Schemas with Enumerated Types

In addition to annotating the schemas with the corresponding SDK documentation for each element, we can go one step further and change the plain vanilla xs:string types to custom enumerated types so we get the list of valid choices for the attribute values. This comes in very handy and also helps to eliminate those nasty little typos that can be really hard to find (and the CAML gets mighty snippy about).

There are lots of places in the SDK documentation where the expected attribute values are listed in the description, but are not enumerated in the schema. This again means that we have to go digging through the documentation to discover what SharePoint expects the attribute values to be. This can be an enormous waste of time – even if you have a pretty good idea of what the values are. Who can keep all that detail in their head?

So, I’ve tried to find and enumerate as many attribute values as I could so they are available at your fingertips when you need them. Each enumeration is also annotated, so you get contextual help for the available values as shown below.

CamlIntellisense

Here’s another example that shows the enumeration of list template types when adding a ListInstance element to a feature. This is a biggie. How many times have you had to hunt down the table of list template type identifiers?

ListTemplateTypeEnum

Installing the Enhanced Schemas

The CAML.NET IntelliSense download package includes an MSI installer that copies the extended schemas into the local Visual Studio schema cache, so all you have to do is run the installer and then close and re-open Visual Studio. The current version handles both Visual Studio 2005 and 2008.

Note: If you’re already using the wss.xsd schema from the 12 hive, you should remove the previous catalog file from the Visual Studio schema cache. You may also have to fiddle with your IDE settings a bit because Visual Studio somehow remembers the previous schema settings and adds both the old and the new schemas for files that have already been edited, event after you have removed the old catalog file. This can interfere with the new catalog file you’ve just installed. In the worst case, you will have to choose “don’t use this schema” from the “schemas” dialog for files you have already started editing.

As you can imagine, there are a LOT of elements and attributes to deal with. I’ve made a pretty good start at addressing the most frequently used items, and I’ll continue to add more annotations as my schedule permits. If you find missing or incorrect annotations, please drop me a line or leave a comment and I’ll update the schema.

Now you can ride that CAML high in the saddle and head on out to the SharePoint. You know – the oasis? Right next to the 12 hive! See you there!

“The camel has a single hump,
The dromedary two;
Or else the other way around,
I’m never sure. Are you?”

– Ogden Nash

Stay tuned for CAML.NET 2.0!

19 comments

  1. Hi John, thanks for the efforts in getting this time-saver to the rest of us.

    Just thought I’d mention, for those of us who are using x64 environments, after you install the MSI, I found the "CAML.NET.IntelliSense.xml" file located here: [b]C:Program Files (x86)Microsoft Visual Studio 9.0XmlSchemas[/b]. If you look inside the file, however, you’ll find that the catolog is looking for the schema here: [b]file://C:/Program Files/CAML.NET/IntelliSense/caml.xsd[/b].

    On my system, I had to change that schema reference to [b]…C:/Program Files (x86)/…. [/b]Once I did that, fired up VS, all was well. Looking forward to zipping through my SP schema files now! Awesome!

    Keep up the great work!

  2. Absolutely Sterling work there.

    I edited mine to include this, I added the Format Attribute and I believe that it is of subtype DisplayFormat. Hope Im right and hope it helps.

    <xs:complexType name="QueryColumnDefinition" mixed="true">
    <xs:complexContent>
    <xs:extension base="LookupColumnDefinition">
    <xs:sequence />
    <xs:attribute name="Default" type="xs:string" />
    <xs:attribute name="URLEncode" type="TRUEFALSE" />
    <xs:attribute name="Format" type="DisplayFormat" />
    </xs:extension>
    </xs:complexContent>
    </xs:complexType>

  3. I had to turn off other schemata using the schema dialog for all files I had touched before installing this, since intellisense disappeared altogether for those files. After this it works great!

  4. CAML.NET IntelliSense works fine with VS2005 but what to do to get it work on VS2008? I added Schema file in property window while creating a new xml file and adding xmlns reference to sharepoin, but intellisense did not work.
    Thank you in advance for your answer.
    Bharat.

  5. Hi John!
    Cool, but I have trouble using caml.xsd. VS 2008 is working just fine with the other four .xsd-files but as soon as I am adding caml.xsd (I added all five for testing purpose by hand) intellisense is not working anymore. Any suggestions would be great.

    Thanks a lot and best regards!

  6. Hi Bharat,

    If you run the MSI file, it will install itself automatically for both VS2005 and VS2008, and it works on 32bit and 64bit systems. Then you have to restart Visual Studio. If all else fails, check that the files exist in the following locations:

    %programfiles%caml.netintellisensecaml.xsd
    %programfiles%Microsoft Visual Studio 9.0xmlSchemasCAML.NET.IntelliSense.xml

    If you have any other schema file associated with the "http://schemas.microsoft.com/sharepoint/" namespace, then Intellisense will stop working. To check, create a new XML file and setup the schema (this must be exactly correct – case sensitive), then open the "schema" dialog from the properties window. Sort by namespace and make sure that the only schema checked is the CAML.XSD file described above.

    Cheers,
    John

  7. Hi Peso,

    There can be only one xsd definition loaded for any given namespace/entity pair. You have to remove the other ones in order for Intellisense to work. If VS detects two definitions for the same entity, it doesn’t know which one to load and therefore does not load anything.

    Hope this helps,
    John

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.