Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Linq on a Client Object model result from sharepoint

I am trying to use LINQ on a result i get from Client Object Model.

var rolesAssignments = context.Web.RoleAssignments;
context.Load(rolesAssignments, 
    roles => roles.IncludeWithDefaultProperties(role => role.Member,
    role => role.RoleDefinitionBindings));
context.ExecuteQuery();
var hasAdmin = rolesAssignments.Select(x => x.RoleDefinitionBindings.Cast<RoleDefinition>().Select(y => y.RoleTypeKind == RoleType.Administrator)).Any();

I get:

{System.NotSupportedException: Invalid usage of query execution. The query should be executed by using ExecuteQuery method on the client context object.

However, when i rewrite this to use a nested foreach loop, it works fine.

From what i can see from my linq query, im not using any properties thats not loaded.

like image 281
ruffen Avatar asked Jan 25 '12 11:01

ruffen


People also ask

What is LINQ and how is it used in SharePoint?

LINQ adds a SQL-like syntax and vocabulary to each of the languages, which can be used to query data sources. But unlike other languages and query syntaxes which vary from one type of data source to another, LINQ can be used to query, in principle, any data source whatsoever.

Why is LINQ used over caml for data retrieval from SharePoint list?

Advantages of LINQ over CAML:It can provide strongly typed objects at design time; we can create queries in code and can check that they are correct because we can the compiles the code. The results are returned from queries are strongly typed objects, so the items and fields can provide compile-time checking.

Can you use C# with SharePoint?

You can develop SharePoint projects by using either Visual Basic or Visual C#, and you can develop app for SharePoint projects by using JavaScript.

What is client side object model in SharePoint?

The client object model for SharePoint is a set of client-based libraries that represent the server object model. They are packaged in three different DLLs to accommodate a variety of development types. The client object model includes most of the major functions of the server API.


2 Answers

Sorry for necroposting, but I just faced this issue and was not able to find an answer here. The reason why your linq queries failed is client model collections implemented multiple iterators. And when you try enumerate your rolesAssignments you call IQueryable<T> extension methods. This methods (I assume) designed to pull data from server via some incapsulated soap calls and should not be used on client. Istead you should explicitly use extension methods of IEnumerable<T>. So, this will not work:

var hasAdmin = rolesAssignments.Select(predicate);

And this will work:

var hasAdmin = ((IEnumerable<RoleAssignment>)rolesAssignments).Select(predicate);
like image 113
Tommi Avatar answered Oct 27 '22 01:10

Tommi


This is off the top of my head but it should give you the idea. You may get a complaint about the use of Any in the query. If so remove it and then check hasAdmin.Any() after the ExecuteQuery is complete.

var query = rolesAssignments.Select(x => x.RoleDefinitionBindings.Cast<RoleDefinition>().Select(y => y.RoleTypeKind == RoleType.Administrator)).Any();
var hasAdmin = context.LoadQuery(query);
context.ExecuteQuery();
like image 42
Rob Windsor Avatar answered Oct 26 '22 23:10

Rob Windsor