I’ll be honest. I don’t like writing CAML queries. There, I said it…

How about using LINQ instead to access the data in your lists and libraries? It’s really pretty simple.

You can use the SPMetal.exe located in c:\program files\common files\microsoft shared\web server extensions\15\bin to generate both a data context and LINQ classes which you can consume in your SharePoint project. One you do that, you no longer have to write giant ugly text string queries, but can instead focus on managing your SharePoint data with strongly-typed, entity classes.

To use SPMetal to generate the class file you will need to consume into your SharePoint 2013 project, create a batch file (*.bat) in your development environment as follows:

c:\program files\common files\microsoft shared\web server extensions\15\bin\spmetal.exe /web:http://dev.mydevweb.com
/code:”C:\Users\rhutton\Desktop\SPMetal.cs”
pause

When you run your batch file, a class file will be created. If you specified the class file to use the (*.cs) extension, then the data context and classes will be generated using C#. However, if you specify the class file to use a (*.vb) extension, then the data context and classes will be generated using that other language. 😉

SPMetal creates a data context and classes for each of your lists and libraries so you can access them in a strongly-typed, object oriented way.

Capture2.PNG

Now writing queries is super easy, readable, and definitely more maintainable:

Capture.PNG

Advertisements