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.
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.
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.
You can develop SharePoint projects by using either Visual Basic or Visual C#, and you can develop app for SharePoint projects by using JavaScript.
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.
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);
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With