How To: Using SPMetal Utility in SharePoint 2010 to “presumably” manipulate data on a BCS External List
So this blog started out with me trying to prove that can use LINQ to SharePoint via the SPMetal Utility in SharePoint 2010 to gain access to Business Connectivity Services (BCS) External Content Types // External Lists. In plain English, I wanted to use LINQ to SharePoint in as little code as possible and without using the BCS Template to do CRUD actions to External Lists created in SharePoint. I found out out that I was unable to using SharePoint 2010 BETA and Visual Studio 2010 Beta 2… basically SPMetal did not create the necessary Entity Classes for External Lists in the resultant C# file.
Therefore, I will turn this blog into a How-To for using the SPMetal Utility and point you to Serge Luca Blog for some great Web Cast on just how to use LINQ to SharePoint to access Lists in SharePoint 2010!
So the first thing you can do is bone up on SPMetal. You can read up on it at http://msdn.microsoft.com/en-us/library/ee538255(office.14).aspx or BING “SPMetal SharePoint”
Next I picked a SharePoint 2010 Site that had few clutter and still had List Created OOB that I could play with.
I settled on my “Charlie” Site
Next, I added an External Content Type to create an External List called “A Few People I Know” which resulted in the two screen shots below…
Then just to make sure I do my due diligence, I created two Custom Lists
- One List that will do lookups to my external List but remains a custom list
- One List that will just be a custom list with regular columns
So in my list called “Regular People I know List” I am doing lookups to my External Lists Data…
My Resulting List looks like below…
Then I created my RegularPeopleNOLookup List which is just a vanilla list with stuff in it.
Now Time for the FUN! Stuff, we get to crack open Visual Studio 2010 Beta 2 and Code Away!
Steps I took that led be to below..
- Created a Console Application
- Set the Build Type Platform to “Any CPU” since i am on a x64 bit system
- Add References to
Next I will use SPMetal to create located in the “BIN” folder under the “14-Hive”, is that what we are calling it… I cant remember, but I know we were given the nomenclature in Vegas!
The syntax for the utility against my Charlie Site is below..
At this time now as you can see in my working folder, I have a file created called “CharlieSiteLists.cs” which is where the Entity Classes are located for the Lists in the “Charlie” site… My HOPE is/was that I will get my External List showing up too.. bummer it didn’t…
Below you have the “CharlieSiteListDataContext” which is basically your wrapper to the Content Database through LINQ
Below you can see the Entity Classes representative of the SharePoint List in the “Charlie” Site.
What helps us out here is that the Entity List Class implements IQueryable and IEnumerable which will help us out in our LINQ operations.
So here is where I will bail out…since I wasn’t able to get to my SharePoint –> Business Connectivity Services –> External Content Type –> External List that I created in my Lab. So Just to show you how it LINQ to SharePoint works see below how I add a data element to my List… MUCH better examples and walkthru are available on Serge Luca’s Blog.
Have Fun with That! Hopefully the story changes with External List when RTM comes out… that’d be really cool.