Answered by:
Error When Hiding Views with Plugin

Question
-
I am using the following plug-in code to hide some system views on a CRM 2011, On-Site, installation
namespace APR.CRM.Plugin.HideViews { public class HideCRMViews : IPlugin { public void Execute(IServiceProvider serviceProvider) { // Obtain the execution context from the service provider. Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext) serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext)); // The InputParameters collection contains all the data passed in the message request. if (context.InputParameters.Contains("Query") == true) { QueryExpression qe = (QueryExpression)context.InputParameters["Query"]; if (qe.EntityName == "savedquery") { if (qe.Criteria != null) { if (qe.Criteria.Conditions != null) { /*The query is edited to look at views not starting with "Hidden" at the begining of the View Name*/ ConditionExpression queryCondition = new ConditionExpression("name", ConditionOperator.NotLike, "Hidden%"); qe.Criteria.Conditions.Add(queryCondition); //context.InputParameters.Properties[ParameterName.Query] = qe; context.InputParameters["Query"] = qe; } } } } } } }
It works fine in my development environment, but I get the following errors in the Training and Production environments.The Web Service plug-in failed in OrganizationId: 85f0ee33-2f7b-e011-8be8-842b2bfc99c6; SdkMessageProcessingStepId: a301231d-7a3b-e011-a47c-842b2b4b5425; EntityName:
savedquery; Stage: 10; MessageName: RetrieveMultiple; AssemblyName: APR.CRM.Plugins.Activities.CCHideViewsPlugin, Plugins, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=369dc9c07958467a; ClassName: APR.CRM.Plugins.Activities.CCHideViewsPlugin; Exception: Unhandled Exception:
System.InvalidCastException: Unable to cast object of type 'Microsoft.Xrm.Sdk.Query.FetchExpression' to type 'Microsoft.Crm.Sdk.Query.QueryExpression'. at APR.CRM.Plugins.Activities.CCHideViewsPlugin.Execute(IPluginExecutionContext context) at Microsoft.Crm.Extensibility.V4PluginProxyStep.ExecuteInternal(PipelineExecutionContext context) at Microsoft.Crm.Extensibility.VersionedPluginProxyStepBase.Execute(PipelineExecutionContext context)I have debugged the code in the Dev environment and see no casting errors. Anyone have any idea where this error may be happening?
Thanks.
Tim HaysWednesday, July 20, 2011 5:50 PM
Answers
-
Hi,
Try using the code like this:
namespace APR.CRM.Plugin.HideViews { public class HideCRMViews : IPlugin { public void Execute(IServiceProvider serviceProvider) { // Obtain the execution context from the service provider. Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext) serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext)); // The InputParameters collection contains all the data passed in the message request. if (context.InputParameters.Contains("Query") == true && context.InputParameters["Query"] is QueryExpression) { QueryExpression qe = (QueryExpression)context.InputParameters["Query"]; if (qe.EntityName == "savedquery") { if (qe.Criteria != null) { if (qe.Criteria.Conditions != null) { /*The query is edited to look at views not starting with "Hidden" at the begining of the View Name*/ ConditionExpression queryCondition = new ConditionExpression("name", ConditionOperator.NotLike, "Hidden%"); qe.Criteria.Conditions.Add(queryCondition); //context.InputParameters.Properties[ParameterName.Query] = qe; context.InputParameters["Query"] = qe; } } } } } } }
Jehanzeb Javeed
http://worldofdynamics.blogspot.com
Linked-In Profile |CodePlex Profile
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
- Proposed as answer by Daniel Cai - KingswaySoftMVP Wednesday, July 20, 2011 6:06 PM
- Marked as answer by Timothy Hays Wednesday, July 20, 2011 7:45 PM
Wednesday, July 20, 2011 6:03 PM
All replies
-
Hi,
Try using the code like this:
namespace APR.CRM.Plugin.HideViews { public class HideCRMViews : IPlugin { public void Execute(IServiceProvider serviceProvider) { // Obtain the execution context from the service provider. Microsoft.Xrm.Sdk.IPluginExecutionContext context = (Microsoft.Xrm.Sdk.IPluginExecutionContext) serviceProvider.GetService(typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext)); // The InputParameters collection contains all the data passed in the message request. if (context.InputParameters.Contains("Query") == true && context.InputParameters["Query"] is QueryExpression) { QueryExpression qe = (QueryExpression)context.InputParameters["Query"]; if (qe.EntityName == "savedquery") { if (qe.Criteria != null) { if (qe.Criteria.Conditions != null) { /*The query is edited to look at views not starting with "Hidden" at the begining of the View Name*/ ConditionExpression queryCondition = new ConditionExpression("name", ConditionOperator.NotLike, "Hidden%"); qe.Criteria.Conditions.Add(queryCondition); //context.InputParameters.Properties[ParameterName.Query] = qe; context.InputParameters["Query"] = qe; } } } } } } }
Jehanzeb Javeed
http://worldofdynamics.blogspot.com
Linked-In Profile |CodePlex Profile
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
- Proposed as answer by Daniel Cai - KingswaySoftMVP Wednesday, July 20, 2011 6:06 PM
- Marked as answer by Timothy Hays Wednesday, July 20, 2011 7:45 PM
Wednesday, July 20, 2011 6:03 PM -
Hi,
If system is passing FetchXML then you can also convert it into QueryExpression i.e.
FetchXmlToQueryExpressionRequest jj_QueryConvertRequest = new FetchXmlToQueryExpressionRequest(); jj_QueryConvertRequest.FetchXml = FetchXML; FetchXmlToQueryExpressionResponse jj_QueryConvertResponse = (FetchXmlToQueryExpressionResponse)service.Execute(jj); //jj_QueryConvertResponse.Query
Jehanzeb Javeed
http://worldofdynamics.blogspot.com
Linked-In Profile |CodePlex Profile
If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".- Proposed as answer by Jehanzeb.Javeed Wednesday, July 20, 2011 6:07 PM
Wednesday, July 20, 2011 6:07 PM -
Thanks. I feel dumb for not having thought of that. Just going in too many directions lately.
Tim
Tim HaysWednesday, July 20, 2011 7:46 PM -
Also with CRM 2011 you can disable system views without using a plugin. Just deactivate the ones you don't neeed.
Rich Dickinson
Program Manager - Dynamics CRMSaturday, July 23, 2011 2:44 AM -
Hi Javeed, I have a similar need where I need to restrict users to certain price-list. So I modified the query as below. for testing purpose, I am restricting users based on BU. I added a custom field ("new_businessentity") in the price-list so I can filter the query using RetrieveMultiple Message. .
public void Execute(IServiceProvider serviceProvider) { // Obtain the execution context from the service provider. IPluginExecutionContext context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext)); // Get a reference to the Organization service. IOrganizationServiceFactory factory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory)); IOrganizationService service = factory.CreateOrganizationService(context.UserId); // Get a reference to the tracing service. ITracingService tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService)); //if (context.Mode == 0 && context.Stage == 10 && context.MessageName.Equals("RetrieveMultiple") ) { // The InputParameters collection contains all the data passed in the message request. Entity sysuser = new Entity("systemuser"); sysuser = service.Retrieve("systemuser", context.InitiatingUserId, new ColumnSet("businessunitid")); _unitguid = sysuser.GetAttributeValue<EntityReference>("businessunitid").Id; if (context.InputParameters.Contains("Query")) { if (context.InputParameters["Query"] is QueryExpression) { // Get the QueryExpression from the property bag QueryExpression objQueryExpression = (QueryExpression)context.InputParameters["Query"]; if (objQueryExpression.EntityName == "savedquery" && objQueryExpression.Criteria != null && objQueryExpression.Criteria.Conditions != null) { ConditionExpression businessunitCondition = new ConditionExpression("new_businessunit", ConditionOperator.Equal, _unitguid); objQueryExpression.Criteria.Conditions.Add(businessunitCondition); context.InputParameters["Query"] = objQueryExpression; } } } } }
I registered at pre-event; RetrieveMultiple Message for PriceLevel Entity.
It doesn't work - when the user tried to select pricelist from advanced Find, he still sees all
What am I missing here? Any help is appreciated.
- Edited by CRM elite Sunday, August 12, 2012 4:33 PM
Sunday, August 12, 2012 4:31 PM -
Hi Jehanzeb,
I have made some system views hidden and its working properly. I have also worked it for CRM for Outlook online and took FetchXML -> converted it to QueryExpression -> Add conditions -> convert back QueryExpression to FetchXML but its not working for Outlook. ANy idea ?
Software Engineer
Wednesday, August 22, 2012 9:16 AM -
Hi Syed,
If you are using CRM 2011, you can hide system views by deactivating the views within the customization. Navigate to Settings->Customizations-> Customize the System -> Select the Entity -> Views -> Select the View to be deactivate and click on More Actions and Select Deactivate. This will hide the views from being displayed on the CRM and should hide it from Outlook as well.
Regards
Abish
Abish Asharaf
Wednesday, August 22, 2012 10:20 AM