Monday, October 4, 2010

How to Hide the Views in CRM 4.0 using plugin

This will work for an Account.............It will hide the inactive accounts from the view.
Same code will work for Contacts if you replace otc1=2 & Inactive Accounts to Inactive Contacts.

Code:--------------------------------
int otc1 = 1;
//StringCollection sc1 = new StringCollection();
//sc1.Add("Inactive Accounts");
//_hideViews.Add(otc1, sc1);
if (!(context.MessageName == MessageName.RetrieveMultiple && context.PrimaryEntityName == EntityName.savedquery.ToString() && context.InputParameters.Contains(ParameterName.Query) && context.OutputParameters.Contains(ParameterName.BusinessEntityCollection)))
return;

QueryExpression qe = context.InputParameters[ParameterName.Query] as QueryExpression;
if (qe == null) // Check it's a QueryExpression
return;
ConditionExpression cond = GetCondition(qe.Criteria, "returnedtypecode");
if (!(cond != null && cond.Operator == ConditionOperator.Equal && cond.Values.Length == 1 && cond.Values[0] is int)) // Check there is an equiality condition on returnedtypecode for an integer value
return;
int otc = (int)cond.Values[0];
if (otc1 != otc) // Check that we want to exclude views for this entity
return;
BusinessEntityCollection becViews = (BusinessEntityCollection)context.OutputParameters[ParameterName.BusinessEntityCollection];
if (!(becViews != null && becViews.EntityName == EntityName.savedquery.ToString())) // Check there are some views, and they are the right type
return;
//StringCollection scHide = _hideViews[otc];
for (int i = becViews.BusinessEntities.Count - 1; i >= 0; i--) // Iterate backwards, as items may be removed
{
DynamicEntity de = (DynamicEntity)becViews.BusinessEntities[i]; // BusinessEntityCollection is of type DynamicEntity
if (de.Properties.Contains("name") && (string)de.Properties["name"] == "Inactive Accounts") // Hide this view - i.e. remove it from the collection of returned views
becViews.BusinessEntities.RemoveAt(i);
}

No comments:

Post a Comment