How to use LINQ in SharePoint 2013/2016

This SharePoint tutorial, we will discuss how to use LINQ in SharePoint 2013/2016. We will also see, how to use SPMetal in SharePoint 2013/2016.

LINQ in SharePoint 2013

LINQ (Language Integrated Query) comes in SharePoint 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 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.

To work with LINQ, we need to have data context and LINQ classes. For this, we can use SPMetal.

SPMetal in SharePoint 2013

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>Spmetal.exe /web:http://win-pfcp2dgt8di/sites/EnjoySharePoint /code:"E:\MySPMetalEntity.cs"
linq in sharepoint 2013

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.

Now it is 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 the 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 Existing 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";
myEmpEntiry.MyEmployees.InsertOnSubmit(myEmpItem);
myEmpEntiry.SubmitChanges();
}

Then deploy the visual web part into the SharePoint site. You can check this article to know more about how to deploy the visual web part using PowerShell in SharePoint 2013.

Then the web part will appear like below:

linq in sharepoint 2016

Now once you click on the Add Item to List button, the item will be added successfully like below:

linq in sharepoint

System.invalidoperationexception columns associated with mappings have been deleted or renamed error in Linq to SharePoint

Now, we will see how to fix an error that comes when we work with Linq to SharePoint 2016. Recently while working with Linq to SharePoint I got the error as like below:

{System.InvalidOperationException: Columns associated with mappings have been deleted/renamed.
at Microsoft.SharePoint.Linq.EntityTracker.ValidateMapping()
at Microsoft.SharePoint.Linq.EntityTracker.SubmitChanges(ConflictMode failureMode, Boolean systemUpdate)
at Microsoft.SharePoint.Linq.DataContext.SubmitChanges(ConflictMode failureMode, Boolean systemUpdate)
at Microsoft.SharePoint.Linq.DataContext.SubmitChanges()
at LinqDemo.LinqtoSharePointDemo.LinqtoSharePointDemoUserControl.btnAdd_Click(Object sender, EventArgs e)
at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)}

System.invalidoperationexception columns associated with mappings have been deleted or renamed error in Linq to SharePoint

System.invalidoperationexception columns associated with mappings have been deleted or renamed error in Linq to SharePoint.

The error is related to the columns for the particular list you are working on. Verify the list column names carefully.

In my case, I was adding to a different list and I was assigned to different fields.

You may like following SharePoint tutorials:

Here, we discussed how to work with Linq in SharePoint 2013 and how we can use Linq with SPMetal in SharePoint 2013 or SharePoint 2016.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

>