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

Duplicate Record

Have you ever had a client that needed a functionality like this and the Dynamics CRM still doesn’t do it?

You can accomplish this with a simple code that you can put in a button in the ribbon.

You can accomplish this with a ASP.NET (if you are On-premise) page or a HTML web resource (Online). For this example I’m using the ASP.NET.

protected void Page_Load(object sender, EventArgs e)
{
	try
	{
		CopyEntity copy = new CopyEntity();
		string newEntityID = copy.CopyEntity(base.Request.QueryString["Id"].ToString(), base.Request.QueryString["user"].ToString());
		string str = ConfigurationManager.AppSettings["Server"].ToString() + @"/" + base.Request.QueryString["org"].ToString() + "/main.aspx?etc=" + ConfigurationManager.AppSettings["EntityType"].ToString() + "&id=%7b" + newEntityID + "%7d&newWindow=true&pagetype=entityrecord";
		base.Response.Write("window.location.replace('" + str + "')");
	}
	catch (Exception ex)
	{
		throw new Exception(ex.Message);
	}
}

When you do copy.CopyEntity, you have a class that you implement the logic to copy the record (query the selected record to get the data of the fields and create a new one) that is selected in the Homepagegrid or Form.

You can have this in a button, that invokes a Javascript that will call this page for example.

function callCopyEntity() {
	var userid = Xrm.Page.context.getUserId();
	var guid = Xrm.Page.data.entity.getId();
	var url = "connection";
	var address = url + guid + '&user=' + userid;
	window.open(address, 'DuplicateEntity', 'menubar=no,resizable=yes,toolbar=yes,location=yes');
}

This is an easy implementation that will allow the user to copy the records easily, which would have to create manually, having to change only the information that finds necessary.

Hope this helps.