Skip to content
This repository has been archived by the owner on Mar 11, 2020. It is now read-only.

SharePoint 101 F. Server side dev ~ Accessing List Data through CAML and LINQ to SharePoint

Philippe Lavoie edited this page Mar 11, 2020 · 1 revision

When you are server-side in the context of a site collection, the typical way to query for data in list is to use SharePoint's custom XML query language: CAML. A more modern approach is to use the SPMetal + LINQ to SharePoint combination.

##CAML: the Collaborative Application Markup Language

  • CAML is another term for all the XML schemas used to provision content in a SharePoint context
    • (e.g. all modules in Elements.xml files follow a CAML schema)
  • In the context of SharePoint list queries, CAML serves as an abstraction over SQL queries to the SharePoint database

References:

##Building your first CAML query

  • The SPQuery API

  • Using a CAML builder tool to debug your queries

  • Use a Recursive Scope, otherwise you won't find items in folders

  • How to avoid excessive database calls when dealing with large item lists

    • Don't forget your ViewFields, otherwise each SPListItem field access will hit the database
    • Avoid looping through all SPListItems in a large SPListItemCollection, prefer using SPListItemCollection.GetDatatable()

Reference:

A friendlier alternative: SPMetal / LINQ-to-SharePoint

  • SPMetal is a command line tool that comes with SharePoint Foundation that can generate C# entity classes by reverse engineering the contents of your SharePoint sites
  • With the DataContext generated by SPMetal, you can use LINQ to query over the generated entity classes and their associated list data source.

References

Clone this wiki locally