Retrieve Multiple plugin

The other day I was doing a development that would require modifying the query to a determined entity so that the users wouldn’t see some records if the users didn’t have the role for.

After a bit of research, I’ve done this plugin that is very useful to hide the info.

Message: RetrieveMultiple, Pre-Validation

if (context.Mode == 0 && context.Stage == 10 && context.MessageName.Equals("RetrieveMultiple"))
{
	if (context.InputParameters.Contains("Query"))
	{
		if (context.InputParameters["Query"] is QueryExpression)
		{
			if (!UserHasRole(context.InitiatingUserId, "role", service))
			{
				QueryExpression objQueryExpression = (QueryExpression)context.InputParameters["Query"];

				ConditionExpression condition= new ConditionExpression()
				{
					AttributeName = "statuscode",
					Operator = ConditionOperator.In,
					Values = { 2 }
				};

				objQueryExpression.Criteria.AddCondition(condition);
			}
		}
	}
}

Update:

Message: Retrieve Multiple, Post-Operation

In this next example I’m removing information from the columns, in this case, it was the description and subject from Email entity.
Thanks to Aileen Gusni, in this post that she explains how to change a column value, I’ve adapted to my requirement:

IOrganizationService service = localContext.OrganizationService;
var context = localContext.PluginExecutionContext;

if (context.OutputParameters.Contains("BusinessEntityCollection"))
{
	var retrievedResult = (EntityCollection)context.OutputParameters["BusinessEntityCollection"];
	
	foreach (Entity entity in retrievedResult.Entities)
	{
		if (entity.Contains("description"))
			entity.Attributes.Remove("description");

		if(entity.Contains("subject"))
			entity.Attributes.Remove("subject");
	}
}

I post this simple code, but what I was using was filtering the information from determined security roles, i.e., I was checking if the user had the required security role to view the information.

Hope it helps!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s