SharePoint 2010 Linq doesn’t support anonymous users
Development, Inconvenient SharePoint, SharePoint 2010SharePoint 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.

















April 20th, 2010 at 7:20 am
Disappointing news Waldek :(
April 20th, 2010 at 7:30 am
@Sezai: I know. Wish it was different, because using Linq is so much easier and more intuitive than using CAML.
April 20th, 2010 at 7:59 am
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.
April 20th, 2010 at 12:12 pm
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.
April 24th, 2010 at 1:53 pm
You can use the standard LINQ, we've been using it for ages on SHraepoint 2007.
May 1st, 2010 at 8:04 pm
[...] SharePoint 2010 Linq doesn’t support anonymous users [...]
May 11th, 2010 at 6:00 pm
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
May 11th, 2010 at 6:50 pm
@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.
May 26th, 2010 at 6:14 pm
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
May 26th, 2010 at 6:27 pm
@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.
May 30th, 2010 at 9:27 pm
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
May 31st, 2010 at 6:10 am
@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.
February 27th, 2011 at 3:35 pm
You can check this link :
http://menetes.blogspot.com/2011/02/adding-list-for-anonymous-user-in.html
April 28th, 2011 at 12:12 pm
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.
April 30th, 2011 at 8:01 am
@Anders: Great! Thanks for getting back with it. Much appreciated :)