Saturday, August 3, 2013

Declarative (CAML) vs Imperative (.NET Code) List Creation on SharePoint

This has been a problem for me since I got my hands on SharePoint development. I am a .NET developer and I am used to write C# code quite easily for everything. At first few projects as usual creating a list was just a matter of few code lines for me on a feature activation. I knew there is a way I could do this with CAML but I did not try. Finally when a requirement came in to customizing a list edit template, I got to know it is easy to do with CAML where as hours of my research time went in waste when I tried the same with C#. This drives me forward to look and see why did Microsoft came up with CAML approach. I spent few time and I found a bunch of Pros and Cons each have.

In SharePoint as you may know, there are 3 easy ways to add a list to a SharePoint site. 
  • Using Code (C#.NET or VB.NET), known as Imperative
  • Use CAML, known as Declarative
  • Using SharePoint user interface (Designer or Web Application)

I am forgetting the SharePoint user interface as a developer approach for the rest of this application and go bit deep in others. 

  • Feature Activation and Deactivation

If you define a list in CAML, it deletes the list content types when deactivating the feature which were added when deploying the feature. This can be a good as well as a bit ugly when considering the developer perspective as the developer will have to re add data if needed. 

In another way, if you want to remove the list on feature deactivation, then you will have to write code if you are using .NET code.

  • Lookup columns

Lookup columns play a major role in SharePoint lists. In many documentations, it is said managing Lookups in CAML is not a problem but I have faced many. Anyway if you are trying to add Lookup to a list located outside the web, then that is not possible with CAML approach. 

  • Debugging

Nobody writes perfect code. In case of necessity on debugging the list creation code for some facts, CAML does not have any support where as .NET does by attaching the debugger to respective W3 worker process. 

  • GAC Deployment

A list functions inside a site but if you create a .NET assembly, that should be deployed to the Global Assembly Cache. In case of creating Sandbox solutions you can use CAML easily and create lists for the particular site without bothering the Farm Administrator. 

  • Custom Forms

Unless you have SharePoint designer installed in the production environment  which most customers do not like, with .NET code there is a big task you need to do modify the custom forms for Adding New Items, Viewing or Editing. When you define the list content type in CAML and create the list instance, it is just a single line that you need to modify to change the default Edit, Add or Display forms. 

This is just a summary of my findings and short span of time experience  Please feel free to comment or let me know the corrections needed to be made. 


  1. Can u give an example how to create the list using the caml please show in the practical way

    1. Sravan: It can be easily done using visual studio. You can add a Content Type (if you need your own one), Then List Definition (if you need) and then a List Instance which will create the actual list. I will post a detailed article later.