LINQ to SharePoint in SP2010 – Part3

Friends! Hope you read & understand previous posts. If not i request you to read those first from
Why LINQ? and LINQ to SharePoint because in this post i am explaining you how to use entity file (which we created in previous post) into any custom webpart.

In this post I will create a simple Web Part that will use LINQ to SharePoint syntax to fetch some information from the Announcements list.

STEP-1: Create a new project (I’m going to create a new Visual Web Part project)
STEP-2: Import your DataContext-file by choosing your Project -> Add -> Existing Item:
STEP-3: Specify your file (I created with named “MySPLinqEntities.cs” in previous post):
STEP-4: We are good to go once it’s properly placed in your project structure:
Your project looks like this in solution explorer after performing above steps.
https://mysharepointsolution.files.wordpress.com/2012/03/spmetal4.jpg

STEP-5: Add proper references
We need to reference the Microsoft.SharePoint.Linq assembly in order to use LINQ to SharePoint.
Right-click and choose “Add Reference” and select the Microsoft.SharePoint.Linq.dll file
https://mysharepointsolution.files.wordpress.com/2012/03/spmetal5.jpg

Reference the assemblies in code :
https://mysharepointsolution.files.wordpress.com/2012/03/spmetal6.jpg

Now we are ready to code.

Before starting to write code, have a brief look what we have done so far.
STEP-1: Create a new project (I’m going to create a new Visual Web Part project)
STEP-2: Import your DataContext-file by choosing your Project -> Add -> Existing Item:
STEP-3: Specify your file (I created with named “MySPLinqEntities.cs” in previous post):
STEP-4: We are good to go once it’s properly placed in your project structure:
STEP-5: Add proper references

STEP-6: Code!!!
I am going to create one Visual Web Part that will use LINQ to SharePoint to fetch all Announcements from my Announcement-list and work with those results.

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

It is very easy to get started with LINQ to SharePoint now, and all you really need to know is to start using the SPMetal tool to generate your entity classes and hook’em up with Visual Studio to start coding.

Apart from using SPMetal.exe tool, MS Plug-in for LINQ is available now!

More in-depth articles to come later on this.

LINQ to SharePoint and RunWithElevatedPrivileges

One comment

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 )

Facebook photo

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

Connecting to %s