Wednesday, December 30, 2009

LINQ TO SharePoint [SPMETAL.EXE]-PART-I

In this blog post i am going to show how to query SharePoint custom list  using LINQ to SharePoint providers. To query SharePoint list using LINQ to SharePoint first we need to create strongly typed classes using SPMETAL.EXE then we can able to program against SharePoint list.

LINQ to SharePoint provides

1) Strongly-typed queries and Intelligence Help while building Query.

2) Compile-time Check.

and LINQ to SharePoint going to get rid of CAML ( Collaborative Application Markup Language ) .

Steps to follow to do LINQ to SharePoint

1) Pass a SharePoint site or site collection URL  as parameter to SPMETAL.EXE command like utility.  you can find this utility  at 14 hives dir

(C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN)

2) SPMETAL will Generates Strongly typed entity classes for all  items ( TASKS, LIST, CALNDER etc..)  and emits DataContext   class.

3) Refer this DataContext class in any project and reference Microsoft.SharePoint.Linq.dll. now you can start doing LINQ to SharePoint

SPMETAL.EXE  Parameter Syntax

SPMETAL /web <SiteUrl> /namespace:<NamespaceName>  /code:<outputfilename.cs>

Here is the my site which has custom list

image

SPMETAL /web:http://localhost:44119/ /namespace:SreeniContacts  /code:SreeniLinqCtx.cs 

image

LINQ to SharePoint Query  (  Console application project )

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SreeniContacts;

namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
SreeniLinqCtxDataContext ctx = new SreeniLinqCtxDataContext("http://localhost:44119/");

var query = from contact in ctx.Sreenicontacts
where contact.Title =="Manager"
select contact;
foreach (var item in query)
{
Console.WriteLine(item.Title +" "+ item.Fname +" "+ item.Lname +" "+ item.Phone );
}
}
}
}
NOTE: choose Any CPU as your Platform targets otherwise you get the following error. 
image 
Right click the project select properties set Platform Targets as Any CPU. 
image 
 
image 
DataContext object will keep tracks of entity changes. using DataContext object we can do CURD operations on the list.
Part-II we will see how we can add Items to custom list using DataContext object.
Nandri(Thanks)
SreenivasaRagavan.

12 comments:

oscar said...

I am trying to use SPMetal. When I enter the command and run it I keep getting Object Reference Not Set to an instance of an Object".

I do not know how to get past this?

Sreeni.NET said...

Hi Oscar,
please check your site URL first.
if not can you share the command you are trying to execute using SPMETAL , let me have look.
Thanks
SreenvasaRagavan

oscar said...

The weird thing is that it worked one time and now it isnt working. I am just trying to regenerate it.

Below is my command. I created a .bat file with this:

SET SPMETAL="C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\BIN\SPMETAL.EXE"

%SPMETAL% /web:http://www.technologyblends.com /namespace:TechnologyBlends /code:TechnologyBlends.cs

pause

oscar said...
This comment has been removed by the author.
oscar said...
This comment has been removed by the author.
oscar said...

I have also tried running the command in the cmd windows and get the same message:

SPMETAL /web:http://www.technologyblends.com
/namespace:TechnologyBlends
/code:TechnologyBlends.cs

"www.technologyblends.com" is the sitename in IIS, so it does exist. And again, it worked the first time, but now it isnt working. Not sure what "Object Reference Not Set to an instance of an Object" actually means.

CARPENTIER Olivier said...

You can also use the new LINQ to SharePoint DSL Extension for Visual Studio 2010 available here:
http://code.msdn.microsoft.com/linq2spdsl/

This tool is useful to control SPMETAL code generation thanks to a schema inside Visual Studio, like LINQ to Entities.

Mike Smith said...

Nice write up... and thanks for the X64 reminder.

Cempaka Biru said...

hi, i confused when u used sreenilistdatacontext ctx =... is it yr file name?

Cempaka Biru said...

i already got where that name coming from but i got an access denied error..how to solve it? do you have any idea?

Sreeni.NET said...

Hi Biru,
can you give little more details (error message) on this error so that i can tell your right solution to slove thise.

Cempaka Biru said...

{"Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))"}

on the spmetal file at:
[Microsoft.SharePoint.Linq.ListAttribute(Name="Actual Installation")]
public Microsoft.SharePoint.Linq.EntityList ActualInstallation {
get {
return this.GetList("Actual Installation");
}
}

i keep googling for the answer but no luck.