Use “LINQ to SharePoint” to query SharePoint lists and libraries with “Managed Metadata Column”

This is the search page we are going to create:

Data is from the following lists/library:

1. Train Feedback (attendees/users submit feedback forms for a course in “Training Records”) with lookup columns to lists: Training Records and Users

2. MiTek FSM (They are the trainers):

3. Training Records (date of the course, attendees, who are the trainers, etc…) with lookup columns to lists: MiTek FSM and Users

4. Users (attendees)

This is how we can make it:

Start a new “Visual Web Part” project in Visual Studio 2010, and select “Farm Solution”.

Open the Elements.xml file and change the group name:

Open the .webpart file and change the title and description for this web part:

Insert some labels, textbox, button and datagrids into the UserControl.ascx file:

and this is the design view:

Generate the utilities class using SPMetal:

Create a new folder called “class” inside your project folder, and create a GenerateEntities.bat and a parameters.xml file in this folder. We will use these two files to create a SPMiTek-CRM.cs file. What it does is going into the SharePoint site you defined in the GenerateEntities.bat file, get the information for the lists and libraries you defined in the parameters.xml file and put them into a .cs file, which you can include into your project to code against.

Double click on the GenerateUtilities.bat file and it will create a SPMiTek-CRM.cs file in the same folder:

Go into the solution manager of Visual Studio, click “Show All Files” then right click the .cs file you just created, then “Include In Project”:

This is how the SPMiTek-CRM.cs file looks like:

Need to add a reference to “Microsoft.SharePoint.Linq“:

We will also need to add a reference to “Microsoft.SharePoint.Taxonomy” because we will need to access a “Managed Metadata Column”: State.

Create a SPMetal-MMS.cs class to extend the SPMetal Genarated Utilities (SPMiTek-CRM.cs) so that we can work with the “State” column, which is a “Managed Metadata Column”.

This is the code for SPMetal-MMS.cs:


This is the code behind for the web part:

Dowload the code: Training Feedback ReportUserControl.ascx.cs

Right click your project and deploy:

Now you can go to your SharePoint site, create a new page, and insert the webpart:

This entry was posted in LINQ to SharePoint, SharePoint 2010, Visual Studio 2010. Bookmark the permalink.

Leave a Reply

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

You are commenting using your 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