SharePoint 2010 Linq doesn’t support anonymous users


SharePoint 2010 ships with support for Linq. The great thing about it is, that Linq simplifies the process of querying lists and working with the retrieved items. Instead of objects, which you get if you’re using CAML, Linq retrieves for you strongly typed objects what makes it extremely easy to work with. And although it seems really perfect there are a few things that you need to keep in mind before you refactor your code to use SharePoint Linq instead of CAML.

It’s for the Server API only

First of all SharePoint Linq is supported by the Server API only. If you’re working on a client application which uses the Client API, developing a cool Silverlight Web Part using the Silverlight Object Model or designing highly interactive components using ECMA script you have to use CAML. SharePoint Linq is not supported in any of these APIs. At this moment the only place that you can benefit of using Linq is the Server API (both in Farm and Sandbox solutions).

You need to log in first…

Another drawback on using SharePoint Linq is that it works only with authenticated users. At this moment there is no support for anonymous users. Let’s have a look at some sample code. Using the old-school CAML approach you could perfectly do this:

using (SPWeb web = SPContext.Current.Site.OpenWeb(“/subsite”))
{
    SPList list = web.Lists[“Foo”];
    SPListItemCollection items = list.GetItems(new SPQuery()
    {
        Query = "<OrderBy><FieldRef Ascending='TRUE' Name='SomeField' /></OrderBy>",
        ViewFields = "<FieldRef Name='SomeOtherField' />"
    });
    // do something with the results
}

The items variable would contain all retrieved items which you could then loop through and retrieve the value of every field by casting the object to its original value type.

The equivalent of the above in SharePoint Linq would look something like this:

Microsoft.SharePoint.Linq.DataContext context = new Microsoft.SharePoint.Linq.DataContext(“/subsite”);
EntityList<MyEntity> list = context.GetList<MyEntity>(“Foo”);
Items = from MyEntity item
        in list
        orderby item.SomeField
        select item;

In this scenario the Items list contains strongly typed MyEntity objects which field values are already casted to right types making it easier for you to work with.

So what’s the problem?

As soon as the context.GetList() method gets hit, the authentication prompt appears in the browser while working as an anonymous user. Opening exactly the same list through the Server API works perfectly and allows you to retrieve all the items.

And why is it bad?

The problem with lack of support for anonymous users is that it makes it difficult for you to reuse your code across different projects. Web Parts developed originally for collaboration solutions might not immediately work with Web Content Management solutions for example. So when considering Linq you really have to be sure that all users are authenticated and that the Web Part you are developing will never be used on with a solution that allows anonymous users.

Technorati Tags: SharePoint 2010,Linq

Others found also helpful: