Pages

Feb 4, 2011

Problems with SPMetal in SharePoint 2010

  1. SP metal can run against BCS - external content types don't get created by SPMetal;
  2. Hidden fields are not available to the SPMetal created proxy i.e. created by. Parameters.xml can be changed to display these default hidden fields; and
  3. Some column types are not picked up by SPMetal namely "Managed Metadata columns", "Publishing Html" or "Publishing Image". Additionaly any custom created columns are not included by SPMetal.
  4. Anonymous LINQ needs a work around. Update 27/11/2010, August Cumulative Update (CU) for SharePoint 2010 apparently fixes the anaonymous Linq to SharePoint issue. Ensure you get the latest CU due to the rerelease issues.
  5. Update: 08 Oct 2010 - List attachments are not picked up by SPMetal. You will need to use the Server side object model or extend SPMetal using a partial class.
  6. Update: 14 Oct 2010 - Multiselect columns are not updateable with mulitiple values using LINQ to SharePoint. You can update with 1 value only.
  7. Update 18 Oct 2010 - SPMetal does not like spaces in the url to the site that it generates off. Error the web at 'http://demo.dev/sites/my site' could not be found.
  8. Update 27 Nov 2010 - Using Linq to SharePoint across site collections.

LINQ to SharePoint in SharePoint 2010 using SPMETAL

Introduction

In SharePoint 2010 you now have the ability to use LINQ syntax to fetch items from your lists instead of using the "traditional" approach of CAML queries. (Including SPSiteDataQuery and SPQuery objects)

In this article I will give you a brief introduction to how you can get started using LINQ queries in SharePoint, also known as LINQ to SharePoint.

Basics of LINQ?

As a prerequisite to this article, I'm going to imply that you know what LINQ is and how to write basic LINQ queries in any .NET application already. I'm not going to dive into the details about LINQ or the syntax itself here - please see MSDN for that!

LINQ to SharePoint!

In order to work with LINQ in SharePoint 2010, we need use a tool called SPMetal.exe which resides in the 14\bin folder. This tool is used to generate some entity classes which Visual Studio 2010 can use to get IntelliSense, and allows for LINQ-based queries to be performed on your lists.

Noteworthy:

  • LINQ to SharePoint queries are translated to proper CAML queries
  • CAML queries are in turn later translated to SQL queries

SPMetal.exe

Using the tool called SPMetal, we generate our entity-classes that are needed to perform these object oriented queries toward our SharePoint server.

These are the required steps to get hooked up:

  1. Launch a cmd-window and navigate to C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\bin
    image
  2. Run the following command to utilize the SPMetal.exe tool with the following syntax:
    1. SPMetal.exe /web:http://yoursite /code:C:\YourEntityFile.cs
    2. Example:
      image
  3. Now navigate to C:\ (or wherever you chose to output your file) and make sure the file has been generated:
    image
  4. Open up the file and take a look at the content that SPMetal now have provided us with:
    image
    Note that the class name is now MyEntitiesDataContext. It's based on the name you specify as your code file in the SPMetal.exe command line tool.

    If you were to use /code:C:\Awesome.cs instead, it would generate a class calledAwesomeDataContext.

With that done - all we need to do is import it to one of our projects and use it!

Visual Studio 2010 - Let's create a sample Web Part that utilizes LINQ to SharePoint

In this sample I will create a simple Web Part that will use LINQ to SharePoint syntax to fetch some information from the Announcements list. A basic sample I use in my training classes as well, and should be fairly easy to grasp!

  1. Create a new project (I'm going to create a new Visual Web Part project)
  2. Import your DataContext-file by choosing your Project -> Add -> Existing Item:
  3. Specify your file (mine is called MyEntities.cs):
  4. Make sure it's properly placed in your project structure - then we're good to go:
    image

Alright - that's easy enough. Thus far we have created an entity file using SPMetal.exe and now we have successfully imported it into our project.

Add proper references

Now in order to use LINQ to SharePoint, you also need to reference the Microsoft.SharePoint.Linqassembly. Point to references, right-click and choose "Add Reference" and select the Microsoft.SharePoint.Linq.dll file:
image

In your code, reference the assemblies:
image

Ready to code?

What you should've done up until now is this:

  1. Generate your entities using the SPMetal.exe tool
  2. Reference the newly created file from your SharePoint project
  3. Make sure you're using the proper references for System.Linq and Microsoft.SharePoint.Linq
  4. Be ready to code :-)

Sharepoint 2010 : Client-side classes and server-side equivalents

Client

Server

ClientContext

SPContext

Site

SPSite

Web

SPWeb

List

SPList

ListItem

SPListItem

Field

SPField

Notice that the SharePoint Foundation 2010 managed client object model uses the same legacy naming pattern for site collections and sites as the server object model. The Site class represents site collections, and theWeb class represents sites. My preference for using these classes is to name the variables so that the variable name indicates whether it is a site collection or a site, even though you must use the Site and Web classes

SharePoint 2010: Getting Started With Client Object Model

Along with the Server Object Model, SharePoint 2010 introduces new set of API called Client Object Model

Refer to my blog post SharePoint 2010: Introducing the Client Object Model to get to know more about Client Object Model.

The Client Object Model (OM) is a new programming interface for SharePoint 2010 where code runs on a user’s client machine against a local object model and interacts with data on the SharePoint Server.

Lets get started on our ‘Hello Word’ Client Object Model console application.

Step 1 – Reference the Client Object Model

You need to reference two dlls which are available at C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI

1. Microsoft.SharePoint.Client

2. Microsoft.SharePoint.Client.Runtime

image

Add the using namespace for Microsoft.SharePoint.Client

using Microsoft.SharePoint.Client;

Step 2 – Create the Client Context

[ We will use the Main function to write our piece of code ]

You need to first create a context to the SharePoint site:

ClientContext context =
new ClientContext(http://);
Step 3 – Create Your Query

Here is the next block of code, explanations will follow below:

Web site = context.Web;
context.Load(site, s => s.Title);

As you can see, we are querying for what we require and nothing else will be returned. In our code, we pass the site to the context, but say we want to query only the property Title.

Step 4 – Execute Your Query

Below is the code to execute the query:

context.ExecuteQuery();

The ExecuteQuery() method is a synchronous method call.

Step 5 – The Output

Below is the output:

image

Another Query Example

ListCollection lists = site.Lists;
IEnumerable listsCollection =
context.LoadQuery(lists.Include(
l => l.Title,
l => l.Id));

The above code queries all the Lists in the site, but only returns the Title and the Id properties.

In the next post, we will see how we can query asynchronously.

SharePoint 2010: Introducing the Client Object Model

Today

If you want to access SharePoint Server 2007 Data today, you have two ways of doing it (based on your needs):

  1. Writing a Server Application
    • This application resides in server
  2. Writing a Client Application
    • This client application can run from any other server or a desktop

How are we building these Server and Client applications today?

Server Application

SharePoint Server Application

We interact with the Server Object Model, which are nothing but SharePoint APIs and build applications.

For example:

List announcements = new List();
SPSite site = SPContext.GetContext(HttpContext.Current).Site;
using (SPWeb curWeb = site.OpenWeb())
{
      SPList lstAnnouncements = curWeb.Lists[new Guid(LibraryName)];
 
      //rest of the code
}

Client Application

SharePoint Client Application

We use the SharePoint Web Services which then interacts with the SharePoint API. Usually, developers either build their own set of web services which exposes only certain methods/functions they want to expose to the application or consume the default out of the box SharePoint web services. This becomes difficult if you are going write rich client applications such as Silverlight, or Javascript applications and most of the time accessing SharePoint data via web services is complicated. That is why, I am not going to show you an example code!

The Future of SharePoint Client Applications

SharePoint 2010 introduces the new Client Object Model. From the sneak peak videos:

“The Client Object Model (OM) is a new programming interface for SharePoint 2010 where code runs on a user’s client machine against a local object model and interacts with data on the SharePoint Server. Client OM methods can be called from JavaScript, .NET code or Silverlight code and makes building rich client applications for SharePoint easy.”

So, how things are going to change?

Shareoint Client Application

One API to rule them all – Yep, whether its WPF or Windows Forms or Silverlight or Javascript – your code uses Client Object Model to interact with the SharePoint site to access the data. Now, there is something common that everybody can use instead of creating their own wrapper services to access SharePoint data!

Any code sample?

The only source of information available for now is the Developer Sneak Peak Video.

In the video, a Silverlight application is built using the Client Object Model.

The assemblies added to the project are:

Client Object Model

  • Microsoft.SharePoint.Client.Silverlight.dll
  • Microsoft.SharePoint.Client.Silverlight.Runtime.dll

And also there is a using statement added in the code behind:

Client Object Model

using Microsoft.SharePoint.Client;

And the code showcasing this new Client Object Model:

Client Object Model Code

You can explore the Microsoft.SharePoint.Client namespace in the SharePoint 2010 Technical Preview Documentation

Microsoft.SharePoint.Client namespace

Lets hope this Client Object Model simplifies accessing SharePoint data from client applications :)