{"id":539,"date":"2012-04-19T08:20:30","date_gmt":"2012-04-19T07:20:30","guid":{"rendered":"http:\/\/blog.repsaj.nl\/?p=539"},"modified":"2012-04-19T08:20:30","modified_gmt":"2012-04-19T07:20:30","slug":"sp2010-bcs-issue-calculatepermissionsforcurrentthread","status":"publish","type":"post","link":"http:\/\/blog.repsaj.nl\/index.php\/2012\/04\/sp2010-bcs-issue-calculatepermissionsforcurrentthread\/","title":{"rendered":"SP2010: BCS issue; CalculatePermissionsForCurrentThread"},"content":{"rendered":"<p>This probably is the last post in my latest BCS series for now, since our project is almost finished \ud83d\ude42 One of the last hurdles I had to take was an error which only occured in our production environment for some reason (I guess production and test still aren&#8217;t similar enough). It occurred when trying to get a single BCS entity in code and had something to do with the permissions not being valid. <!--more--><\/p>\n<p>Here&#8217;s the code I used to get the entity:<\/p>\n<pre class=\"prettyprint [lang-cs|linenums:1]\">BdcService bdcService = SPFarm.Local.Services.GetValue&lt;BdcService&gt;();\r\nSPServiceApplication serviceApp = bdcService.Applications.Where(a =&gt; a.Name == bcsServiceAppName).First();\r\n\r\nSPServiceContext context = SPServiceContext.GetContext(serviceApp.ServiceApplicationProxyGroup, SPSiteSubscriptionIdentifier.Default);\r\nIMetadataCatalog catalog = bdcService.GetDatabaseBackedMetadataCatalog(context);\r\n\r\nMicrosoft.BusinessData.MetadataModel.IEntity entity = catalog.GetEntity(entitySchema, entityName);\r\n\r\n\/\/ just get the first lobSystemInstance, there won't be more then one\r\nILobSystemInstance LobSysteminstance = entity.GetLobSystem().GetLobSystemInstances()[0].Value;\r\n\r\nIEntityInstance entityinst = entity.FindSpecific(new Microsoft.BusinessData.Runtime.Identity(identifiers), LobSysteminstance);<\/pre>\n<p>Ok so that wasn&#8217;t working. It was instead giving me this error: <\/p>\n<pre class=\"prettyprint [lang-cs|linenums:1]\">\r\nMicrosoft.SharePoint.SPException: Cannot complete this action.  Please try again. ---> System.Runtime.InteropServices.COMException (0x80004005): Cannot complete this action.  Please try again.     \r\n at Microsoft.SharePoint.Library.SPRequestInternalClass.CalculatePermissionsForCurrentThread(Object& pvarAcl, Boolean bOnlyAces, UInt64& pPermGrant, UInt64& pPermDeny, Boolean& pbIsSiteAdmin, Boolean& pbIsSiteAuditor)     \r\n at Microsoft.SharePoint.Library.SPRequest.CalculatePermissionsForCurrentThread(Object& pvarAcl, Boolean bOnlyAces, UInt64& pPermGrant, UInt64& pPermDeny, Boolean& pbIsSiteAdmin, Boolean& pbIsSiteAuditor)     - \r\n -- End of inner exception stack trace ---     \r\n at Microsoft.SharePoint.SPGlobal.HandleComException(COMException comEx)     \r\n at Microsoft.SharePoint.Library.SPRequest.CalculatePermissionsForCurrentThread(Object& pvarAcl, Boolean bOnlyAces, UInt64& pPermGrant, UInt64& pPermDeny, Boolean& pbIsSiteAdmin, Boolean& pbIsSiteAuditor)     \r\n at Microsoft.SharePoint.Administration.SPAcl`1.CalculatePermissions()     \r\n at Microsoft.SharePoint.BusinessData.Infrastructure.BdcAccessControlList.AccessCheck(BdcRights rights)     \r\n at Microsoft.SharePoint.BusinessData.Runtime.DataClassRuntime.ExecuteInternal(IDataClass thisDataClass, ILobSystemInstance lobSystemInstance, ILobSystem lobSystem, IMethodInstance methodInstanceToExecute, IMethod methodToExecute, IParameterCollection nonReturnParameters, Object[]& overrideArgs)     \r\n at Microsoft.SharePoint.BusinessData.Runtime.EntityRuntime.ExecuteInternal(IDataClass thisDataClass, ILobSystemInstance lobSystemInstance, ILobSystem lobSystem, IMethodInstance methodInstanceToExecute, IMethod methodToExecute, IParameterCollection nonReturnParameters, Object[]& overrideArgs, IFilterCollection filters)     \r\n at Microsoft.SharePoint.BusinessData.Runtime.EntityRuntime.FindSpecific(IEntity thisEntity, Identity entityInstanceIdentity, String specificFinderName, ILobSystemInstance lobSystemInstance)     \r\n at Microsoft.SharePoint.BusinessData.Runtime.EntityRuntime.FindSpecific(IEntity thisEntity, Identity entityInstanceIdentity, String specificFinderName, ILobSystemInstance lobSystemInstance, OperationMode mode)     \r\n at Microsoft.SharePoint.BusinessData.Runtime.EntityRuntime.FindSpecific(IEntity this, Identity identifierValue, ILobSystemInstance lobSystemInstance, OperationMode operationMode, Boolean readNow)     \r\n at Microsoft.SharePoint.BusinessData.MetadataModel.Static.Entity.FindSpecific(Identity identity, ILobSystemInstance lobSystemInstance)    \r\n<\/pre>\n<p>And when running elevated, it changed to:<\/p>\n<pre class=\"prettyprint [lang-cs|linenums:1]\">\r\nAccess Denied for User 'CONTOSO\\\\spservice', which may be an impersonation by 'CONTOSO\\\\spservice'. Securable MethodInstance with Name 'GetSingleObjectEntityByID' denied access. Stack Trace:    \r\n at Microsoft.SharePoint.BusinessData.Runtime.DataClassRuntime.ExecuteInternal(IDataClass thisDataClass, ILobSystemInstance lobSystemInstance, ILobSystem lobSystem, IMethodInstance methodInstanceToExecute, IMethod methodToExecute, IParameterCollection nonReturnParameters, Object[]& overrideArgs)     \r\n at Microsoft.SharePoint.BusinessData.Runtime.EntityRuntime.ExecuteInternal(IDataClass thisDataClass, ILobSystemInstance lobSystemInstance, ILobSystem lobSystem, IMethodInstance methodInstanceToExecute, IMethod methodToExecute, IParameterCollection nonReturnParameters, Object[]& overrideArgs, IFilterCollection filters)     \r\n at Microsoft.SharePoint.BusinessData.Runtime.EntityRuntime.FindSpecific(IEntity thisEntity, Identity entityInstanceIdentity, String specificFinderName, ILobSystemInstance lobSystemInstance)     \r\n at Microsoft.SharePoint.BusinessData.Runtime.EntityRuntime.FindSpecific(IEntity thisEntity, Identity entityInstanceIdentity, String specificFinderName, ILobSystemInstance lobSystemInstance, OperationMode mode)     \r\n at Microsoft.SharePoint.BusinessData.Runtime.EntityRuntime.FindSpecific(IEntity this, Identity identifierValue, ILobSystemInstance lobSystemInstance, OperationMode operationMode, Boolean readNow)     \r\n at Microsoft.SharePoint.BusinessData.MetadataModel.Static.Entity.FindSpecific(Identity identity, ILobSystemInstance lobSystemInstance) \r\n<\/pre>\n<p>Ok. So to cut a very long story short: i tried A LOT of different things with permissions, different code snippets, you name it. And after too much time, I finally found the solution. Which is not in the FindSpecific call itself (which you would expect), but much earlier when retrieving the metadata catalog. I&#8217;m now using the following method:<\/p>\n<pre class=\"prettyprint [lang-cs|linenums:1]\">\r\n    private IEntityInstance GetBCSEntity(SPWebApplication application, string bcsServiceProxyName, string entitySchema, string entityName, object[] identifiers)\r\n    {\r\n      \/\/ retrieve the BDC (BCS) proxy \r\n      BdcServiceApplicationProxy proxy = (BdcServiceApplicationProxy)application.ServiceApplicationProxyGroup.Proxies.Single(p => p.DisplayName == bcsServiceProxyName);\r\n\r\n      IMetadataCatalog catalog = proxy.GetDatabaseBackedMetadataCatalog();\r\n\r\n      \/\/ fetches the entity (not an instance, but the model itself)\r\n      Microsoft.BusinessData.MetadataModel.IEntity entity = catalog.GetEntity(entitySchema, entityName);\r\n\r\n      \/\/ just get the first lobSystemInstance, there won't be more then one\r\n      ILobSystemInstance LobSysteminstance = entity.GetLobSystem().GetLobSystemInstances()[0].Value;\r\n\r\n      \/\/ execute the FindSpecific method to find our entity and then return it\r\n      return entity.FindSpecific(new Microsoft.BusinessData.Runtime.Identity(identifiers), LobSysteminstance);\r\n    }\r\n<\/pre>\n<p>I&#8217;m sorry I can&#8217;t really clarify what&#8217;s the difference between the two (they receive the same catalog as far as I know), but the second one works. One little tip: you probably do need to run this elevated because your normal user won&#8217;t have enough rights in the BCS app to query the model \/ services like this (if it does; it really shouldn&#8217;t).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This probably is the last post in my latest BCS series for now, since our project is almost finished \ud83d\ude42 One of the last hurdles I had to take was an error which only occured in our production environment for some reason (I guess production and test still aren&#8217;t similar enough). It occurred when trying<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[34],"tags":[11,7,47],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3KFR1-8H","_links":{"self":[{"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/posts\/539"}],"collection":[{"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/comments?post=539"}],"version-history":[{"count":0,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/posts\/539\/revisions"}],"wp:attachment":[{"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/media?parent=539"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/categories?post=539"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.repsaj.nl\/index.php\/wp-json\/wp\/v2\/tags?post=539"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}