Hi Amit,
I am following the 1st link for this requirement but i am getting error in vs code.
I am using below complete code but getting error in vs.Is there any wrong in below code?
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace ConditionalFiltering
{
public class Class1 : IPlugin
{
public void Execute(IServiceProvider serviceProvider)
{
var context = (IPluginExecutionContext)serviceProvider.GetService(typeof(IPluginExecutionContext));
var serviceFactory = (IOrganizationServiceFactory)serviceProvider.GetService(typeof(IOrganizationServiceFactory));
var service = serviceFactory.CreateOrganizationService(context.UserId);
var tracingService = (ITracingService)serviceProvider.GetService(typeof(ITracingService));
var query = GetQuery(context);
if (query == null)
{
return;
}
var factory = new QueryModifierFactory(context, service, tracingService);
var modifier = factory.GetModifier(query);
context.InputParameters["query"] = modifier.ModifyQuery(query);
}
private static QueryBase GetQuery(IPluginExecutionContext context) => (!context.InputParameters.Contains("Query") ? null : (QueryBase)context.InputParameters["Query"]);
}
public class QueryModifierFactory
{
private readonly IPluginExecutionContext _context;
private readonly ITracingService _tracingService;
private readonly IOrganizationService _service;
public QueryModifierFactory(
IPluginExecutionContext context,
IOrganizationService service,
ITracingService tracingService)
{
_context = context;
_tracingService = tracingService;
_service = service;
}
public IQueryModifier GetQuery(QueryBase query)
{
if (query is FetchExpression)
{
var fe = query as FetchExpression;
if (fe.Query.Contains(TPAInvestorApprovalQueryModifier.ModifierFlag))
{
return new TPAInvestorApprovalQueryModifier(_context, _service, _tracingService);
}
}
_tracingService.Trace($"No modifier found, returing default modifier.");
return new NoModificationQueryModifier();
}
internal object GetModifier(QueryBase query)
{
throw new NotImplementedException();
}
}
public class NoModificationQueryModifier : IQueryModifier
{
/* public QueryBase ModifyQuery(QueryBase query)
{
return query;
}*/
}
public class TPAInvestorApprovalQueryModifier : IQueryModifier
{
internal static string ModifierFlag = "TPA/Investor Approval";
private readonly IPluginExecutionContext _context;
private readonly ITracingService _tracingService;
private readonly IOrganizationService _service;
public TPAInvestorApprovalQueryModifier(
IPluginExecutionContext context,
IOrganizationService service,
ITracingService tracingService)
{
_context = context;
_tracingService = tracingService;
_service = service;
}
}
public QueryBase ModifyQuery(QueryBase query)
{
FetchExpression fetchQuery = query as FetchExpression;
if (fetchQuery == null) return query;
//object _context = null;
// var tags = GetTagsForUser(_context.InitiatingUserId);
XDocument fetchXMLDoc = XDocument.Parse(fetchQuery.Query);
var entityElement = fetchXMLDoc.Descendants("entity").FirstOrDefault();
var entityName = entityElement.Attributes("name").FirstOrDefault().Value;
if (entityName != "quote") return query;
var filterElements = entityElement.Descendants("filter");
filterElements
.Descendants("condition")
.Where(c => c.Attribute("attribute").Value.Equals("wcl_tpacommission") && c.Attribute("value").Value.Equals(ModifierFlag))
.ToList()
.ForEach(x => x.Remove());
object lexicon = null;
var tagcondition = new XElement("Condition",
new XAttribute("attribute", lexicon.CallReport.Tag),
new XAttribute("operator", "in"));
/*foreach (var tag in tags)
{
tagcondition.Add(new XElement("value", tag.ToString()));
}*/
entityElement.Add(
new XElement("link-entity",
new XAttribute("name", "dp_callreport_dp_tag"),
new XAttribute("from", "dp_callreportid"),
new XAttribute("to", "dp_callreportid"),
new XAttribute("link-type", "inner"),
new XAttribute("intersect", "true"),
new XAttribute("filter", tagcondition)));
//object _tracingService = null;
// _tracingService.Trace(fetchXMLDoc.ToString());
return new FetchExpression(fetchXMLDoc.ToString());
}
/*private object GetTagsForUser(object initiatingUserId)
{
throw new NotImplementedException();
}*/
}
Thanks,
Jharana