LINQ to SharePoint in SP2010 – Part2

In SharePoint 2010 we have the ability to use LINQ syntax to fetch items from your lists instead of using the “traditional” approach of CAML queries.
Here, I will give you a brief introduction to how you can get started using LINQ queries in SharePoint, also known as LINQ to SharePoint.

Please see MSDN for LINQ Basics.

Now Lets start the cream part of this post “LINQ to SharePoint

LINQ to SharePoint!!!
To work with LINQ in SharePoint 2010, SPMetal.exe tool is available which resides in the 14 hives bin folder (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN). SPMetel.exe is used to generate some entity classes which VS2010 can use to get IntelliSense, and allows for LINQ-based queries to be perfomed on SharePoint Sites lists.

IMPORTANTLY:
•LINQ to SharePoint queries are translated to proper CAML queries
•CAML queries are in turn later translated to SQL queries

SPMetal.exe
We will generate our entity-classes that are needed to perform these object oriented queries toward our SharePoint server using SPMetal.exe tool as below.

STEP-1 :
Launch a cmd-window with “run as administrator” and navigate to C:Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\bin

https://mysharepointsolution.files.wordpress.com/2012/03/spmetal1.jpg

STEP-2 :
To utilize SPMetal.exe tool we have to execute command as below

https://mysharepointsolution.files.wordpress.com/2012/03/spmetal2.jpg

This command will create “MySPLinqEntities.cs” file on Drive C: (You can change drive as well). So Navigate to Drive C: and find the created .cs file using SPMetal.exe

STEP-3 :
Now Open up the file and take a look at the content that SPMetal now have provided us with
https://mysharepointsolution.files.wordpress.com/2012/03/spmetal31.jpg

Look into above image and you find the class name is now MySPLinqEntitiesDataContext. It is based on the name you specify as your code file in the SPMetal.exe command line tool.[You can use any name, DataContext keyword will be appended with your file name]

We are done with File creation [Base is created to use LINQ in SharePoint]. We need to do is import it to one of our projects and use it!

Read this post for Web Part that utilizes LINQ to SharePoint

4 comments

  1. Have you ever thought about including a little bit more than just
    your articles? I mean, what you say is fundamental and everything.
    Nevertheless just imagine if you added some great photos or videos to give
    your posts more, “pop”! Your content is excellent but with images and
    videos, this website could definitely be one of the very best in its niche.

    Fantastic blog!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s