In order to use LINQ to SharePoint, you will need to have entity classes created. Rather than having to create them manually which would take a long time, Microsoft provided a small utility that creates all those objects for you called SPMetal (click here for more information). The SPMetal utility works similarly to the SQLMetal utility (extracts SQL metadata from a database and generates entity classes). SPMetal will generate those entity classes which are used in LINQ to execute SharePoint queries. When those queries are executed, the LINQ statements are in turn translated to CAML.
SPMetal command syntax
SPMetal.exe /web:http://yoursite/ namespace:LINQtoSharePoint /code:SPLinq.cs
- web: The complete, absolute URL of the Web site whose data is modeled by the entity classes.
- namespace: The namespace that contains the entity class declarations.
- code: The relative or absolute path and file name of the output file.
Application Modernization Industry Quick Guides
Application modernization enables you to optimize business processes and transform the way you do business today, and in the future. Our industry experts have collected the latest application modernization trends impacting the automotive, financial services, healthcare, and manufacturing verticals.
For more command line options, see here.
After the command was successfully executed with SPMetal, code should have been created for you:
Add that code to your SharePoint project.
Notice that a class ‘SPLinqDataContext’ is created by the SPMetal tool containing all the lists available within the SharePoint site.
All those SharePoint lists can now be queried using the SPLinqDataContext class.
Now you can use the LINQ syntax to query SharePoint data: