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: ,


Possibly related posts

15 Responses to “SharePoint 2010 Linq doesn’t support anonymous users”

  1. Sezai Komur Says:

    Disappointing news Waldek :(

  2. Waldek Mastykarz Says:

    @Sezai: I know. Wish it was different, because using Linq is so much easier and more intuitive than using CAML.

  3. Sezai Komur Says:

    Having two cases where you can't use Linq

    1) Client Object Model APIs
    2) Anonymous access

    is two cases too many. It would have been a better situation if new SharePoint developers didn't have the need to learn CAML at all. Oh well, it's not that bad for the seasoned CAML devs out there.

  4. Marc Anderson Says:

    This is interesting to me because I ran into basically the same problem with the Web Services. See my blog post: Allowing Anonymous Access with SharePoint Web Services and SPServices. I know that it's sort of far-fetched to relate the two things, but maybe not? Maybe there's some underlying architecture decision here which would make this make sense. I'd love to know if that's the case.

    M.

  5. stefan demetz Says:

    You can use the standard LINQ, we've been using it for ages on SHraepoint 2007.

  6. SharePoint 2010: Recopilatorio de enlaces interesantes (VII)! « Pasión por la tecnología… Says:

    [...] SharePoint 2010 Linq doesn’t support anonymous users [...]

  7. Joe Says:

    I found a way that you CAN use Linq to SharePoint even when the user is Anonymous. At this point I'd like to know if I am doing something stupid. http://jcapka.blogspot.com/2010/05/making-linq-to-sharepoint-work-for.html

  8. Waldek Mastykarz Says:

    @Joe: nice tip. It's a shame though that you need to do elevation in order to just retrieve items that are publicly available anyway.

  9. Joe Says:

    I ran a quick test to see how bad the performance hit is when using the solution I suggest. It's really bad. More detail: http://jcapka.blogspot.com/2010/05/linq-to-sharepoint-for-anonymous-users.html

  10. Waldek Mastykarz Says:

    @Joe: Thanks for getting back on this, Joe. It is shame to hear that there is a significant performance hit for using SharePoint Linq with anonymous users. Hopefully we will see this change soon but as for now, we're back to CAML.

  11. Joe Says:

    Sorry about the constant commenting, but I thought I should put this in here for completeness. I ran some more tests on the Anonymous work-around and the performance seems to be just fine. My first test was too much of a quick and dirty one.
    http://jcapka.blogspot.com/2010/05/linq-to-sharepoint-for-anonymous-users_30.html

  12. Waldek Mastykarz Says:

    @Joe: Thanks for giving the test another shot. The only issue I see at the moment is, that, as you are elevating the privileges, there is a chance that you can see more than you should as anonymous user. Although I'm still not really convinced if that's the way to do it, it's good to know that it is possible should you need it.

  13. SBlangkon Says:

    You can check this link :

    http://menetes.blogspot.com/2011/02/adding-list-for-anonymous-user-in.html

  14. Anders Says:

    It seems MS has created a hotfix for the anonymous problem: http://support.microsoft.com/kb/2266423

    If fixes: "Anonymous users cannot use Language Integrated Query (LINQ) to query data in Microsoft SharePoint Foundation 2010. Additionally, the anonymous users are prompted for user credentials when they use LINQ to query data."

    It worked for me.

  15. Waldek Mastykarz Says:

    @Anders: Great! Thanks for getting back with it. Much appreciated :)

Leave a Reply

Security Code:

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS
Copyright © 2007 - 2012 Waldek Mastykarz

Creative Commons License