I am trying to run a CAML query on a BCS list and getting access denied as an error when debugging the code:

SPList CMList = web.Lists[Constants.cmlist];
SPQuery query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name='clnum'/></OrderBy>";
SPListItemCollection items = CMList.GetItems(query);
properties.AfterProperties["Title"] = items.Count.ToString(); //error happens on this line

I had a more complicated query before, but I simplified it down to this to try to figure out where the error was coming from. The external list has been limited to 1000 items and I can access it directly with the items displaying correctly in the webpart.

However, when I try to get the count of the items in the query I just ran, I get the error "Access denied by business connectivity services." This doesn't make any sense to me as I can access the list directly with no problem. I have tried wrapping everything in elevated privileges as well to no effect.

Any insight would be appreciated, thanks.

  • This is a good question, and I think you are right in your assumptions. However, I can't see in your code that you assign properties anywhere in your code sample. Also (a long shot) have you set Metadata Store Permission?
    – Benny Skogberg
    Nov 1, 2013 at 19:50
  • Thanks for the comment. I have gone through and checked the permissions for the metadata store. The weird thing is that I can see the list normally so I clearly have access to the items. For the properties, I have tried replacing the line with "int i = items.Count;" and I get the same error. The problem is with accessing the SPListItemCollection somehow.
    – user11457
    Nov 2, 2013 at 19:54
  • I suppose that you have already Set Object Permission in your BCS. Access to the CA --> Manage Service Applications --> Click On your BCS service and then check it and Set the Permissions to your user.
    – Alvmad
    Nov 3, 2013 at 14:48
  • Yes, that is all set as well. If I view the list normally as the system account or under my own account, I can see the items with no issues. It only gives me this permission error when accessing the items through the query.
    – user11457
    Nov 4, 2013 at 14:37

3 Answers 3


Make sure your code is running under the context of a user who has access to BCS. I would recommend you use SPUserToken to impersonate a user rather than Elevated Privileges, since your app pool may not have access to BCS.


What's your back-end? SQL? Do you have Multiple ReadList methodInstances defined in your model? Try setting the "Method" parameter of the spQuery to the methodInstance you want to use to get the items and make sure the user has access to that methodinstance.


I have given up on this problem and will work around the problem. There seems to be some kind of limitation that exists in event receivers. I have even tried running the code using different user tokens. I neglected to mention that this code was running in an event receiver because I did not think it was at all relevant to the issue. Well, it turns out that if I put the exact same code into a timer job, it completes without issue.

I'm not sure if this is a bug or intended, but I haven't read about this exact issue anywhere else yet.

If you are having a strange access denied error and you are trying to access the BCS through an event receiver, it seems that you will have to work out a way to do it in a timer job instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.