Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Suggested answer

create complex view with filter using plugin in crm 365

(0) ShareShare
ReportReport
Posted on by 2,665

Hi All,

I need to show view in view page in crm 365 using plugin retrieve multiple.

Is any link or code to show complex view in crm ?

Thanks,

Jharana

  • Suggested answer
    Amit Katariya007 Profile Picture
    10,402 Super User 2025 Season 1 on at
    RE: create complex view with filter using plugin in crm 365

    There are some Mistakes like variable name is not correct and function name is not correctly declared. please check out this and try to add tracing in between code so that you will know how much code is working.

  • Suggested answer
    Amit Katariya007 Profile Picture
    10,402 Super User 2025 Season 1 on at
    RE: create complex view with filter using plugin in crm 365

    Line Number 28 it should be Query not query

    pastedimage1653652084119v1.png

    context.InputParameters["Query"] = modifier.ModifyQuery(query);

    Line number 30 : Remove static from it. it will be private QueryBase.

    Line number 63:

    Not present in the article.

    pastedimage1653653113494v4.png 

    Line Number 96:

    pastedimage1653652709541v2.png

    Line 112: also after this you have not provided the foreach.

    pastedimage1653652932098v3.png

    Function name is not correct.

    pastedimage1653653283874v5.png

    Thank you,

    Amit Katariya

  • Suggested answer
    Amit Katariya007 Profile Picture
    10,402 Super User 2025 Season 1 on at
    RE: create complex view with filter using plugin in crm 365

    Hello

  • Jharana Baliyar Singh Profile Picture
    2,665 on at
    RE: create complex view with filter using plugin in crm 365

    Hi Amit,

    Please find the vs error

    vserror.png

    vserror2.PNG

  • Suggested answer
    Amit Katariya007 Profile Picture
    10,402 Super User 2025 Season 1 on at
    RE: create complex view with filter using plugin in crm 365

    can you share error log or file?

  • Jharana Baliyar Singh Profile Picture
    2,665 on at
    RE: create complex view with filter using plugin in crm 365

    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

  • Suggested answer
    Amit Katariya007 Profile Picture
    10,402 Super User 2025 Season 1 on at
    RE: create complex view with filter using plugin in crm 365

    Hello Jharana,

    Please refer below Blogs which will help you to implement your requirement.

    www.dynamicpeople.nl/.../

    community.dynamics.com/.../create-view-using-dynamic-field-entries

    Thank you,

    Amit Katariya

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Kudos to the February Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 293,296 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,093 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156 Moderator

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans