Here we will discuss LINQ in SharePoint 2013. Also, we will discuss how we can use SPMetal in SharePoint 2013. LINQ (Language Integrated Query) comes in SharePoint in 2010 version to query data sources like objects, datasets, swl, xml etc. LINQ adds a SQL-like syntax and vocabulary to each of the languages, which can be used to query data sources.
The LINQ to SharePoint Provider is defined in the Microsoft.SharePoint.Linq namespace. It translates LINQ queries into Collaborative Application Markup Language (CAML) queries. It is no longer necessary for developers to know how to write CAML queries. LINQ queries can be used in server code.
– Caml query builder SharePoint 2013 Online
To work with LINQ, we need to have data context and LINQ classes. For this, we can use SPMetal.
SPMetal is a command-line tool that generates entity classes and these classes are used in LINQ to SharePoint queries and also used to add, delete, and change list items with concurrency conflict resolution.
SPMetal.exe is presented in the below location:
C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\BIN
Here C drive is the installation directory.
In my case I have installed SharePoint in F drive so the SPMetal path is like below:
F:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\BIN
The SPMetal took a parameter is like below:
Spmetal.exe /web:http://win-pfcp2dgt8di/sites/EnjoySharePoint /code:”E:\MySPMetalEntity.cs”
The whole command looks like below:
F:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\BIN>Spme
tal.exe /web:http://win-pfcp2dgt8di/sites/EnjoySharePoint /code:”E:\MySPMetalEnt
Once you run the command then you will see the file MySPMetalEntity.cs must have created in the path you have given in the command.
If you open the file using Visual Studio, you should be able to see like below:
Now it is the time to use the file in the coding.
Use the Class in Visual Studio:
Now we will use this class to add a new item to SharePoint 2013 list in a visual web part. Before going forward, you can use see steps to create a visual web part in SharePoint 2013 using Visual Studio 2013.
Here in this web part I have added a button and on click on that button I am trying to add an item to a list. I have created a custom list name as “MyEmployees” and I have added a custom column name as “FirstName” into it.
Now in your visual studio solution, first we need to give reference to Microsoft.SharePoint.Linq. The dll is available in the below directory.
F:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI
Here F drive is where I have installed SharePoint 2013.
After giving reference to the Linq dll, Add the .cs file which we have generated above by using SPMeta.exe. Right click on the project -> Add Exising Item and you can add the class.
Now in the button click I wrote the below code to add an item to the list.
protected void Button1_Click(object sender, EventArgs e)
MySPMetalEntityDataContext myEmpEntiry = new MySPMetalEntityDataContext(“http://win-pfcp2dgt8di/sites/EnjoySharePoint/”);
MyEmployeesItem myEmpItem = new MyEmployeesItem();
myEmpItem.Title = “Title 1”;
myEmpItem.FirstName = “My First Name”;
Then deploy the visual web part. You can check this article to know more how to deploy visual web part using PowerShell in SharePoint 2013. Then the web part will appear like below:
Now once you click on the Add Item to List button, the item will be added successfully like below:
-: FREE PDF e-Book Download :-
51 JSOM Examples in SharePoint Online
You May Also like the Following SharePoint Online Tutorials: