web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

What is SavedQuery and how do I call it / define it?

(0) ShareShare
ReportReport
Posted on by 25

Hello Experts,

I have copied the code below from another post in this forum. and wanted to use it to update a system view. However, when I use "SavedQuery sq = new SavedQuery{ }"

Visual Studio tells me: "The type or namespace name 'type/namespace' could not be found (are you missing a using directive or an assembly reference?) Compiler Error CS0246".

Am I missing any reference? Here are the ones I use:

using System;
using System.Linq;
using Microsoft.Xrm.Sdk;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Query;

One Doc I found is this one: https://docs.microsoft.com/en-us/dynamics365/customer-engagement/web-api/savedquery?view=dynamics-ce-odata-9

I tried to add Microsoft.Crm.Sdk; without any success.  Maybe the wrong documentation?

I hope someone can help me or point me into the right direction.

Thanks in advance !

The code below is the one I've copied:

   System.String layoutXml =

@"<grid name='resultset' object='3' jump='name' select='1'

   preview='1' icon='1'>

   <row name='result' id='opportunityid'>

   <cell name='name' width='150' />

   <cell name='customerid' width='150' />

   <cell name='estimatedclosedate' width='150' />

   <cell name='estimatedvalue' width='150' />

   <cell name='closeprobability' width='150' />

   <cell name='opportunityratingcode' width='150' />

   <cell name='opportunitycustomeridcontactcontactid.emailaddress1'

       width='150' disableSorting='1' />

   </row>

</grid>";

                    System.String fetchXml =

                    @"<fetch version='1.0' output-format='xml-platform'

   mapping='logical' distinct='false'>

   <entity name='opportunity'>

   <order attribute='estimatedvalue' descending='false' />

   <filter type='and'>

       <condition attribute='statecode' operator='eq'

       value='0' />

   </filter>

   <attribute name='name' />

   <attribute name='estimatedvalue' />

   <attribute name='estimatedclosedate' />

   <attribute name='customerid' />

   <attribute name='opportunityratingcode' />

   <attribute name='closeprobability' />

   <link-entity alias='opportunitycustomeridcontactcontactid'

       name='contact' from='contactid' to='customerid'

       link-type='outer' visible='false'>

       <attribute name='emailaddress1' />

   </link-entity>

   <attribute name='opportunityid' />

   </entity>

</fetch>";

                    SavedQuery sq = new SavedQuery
                    {

                        Name = "A New Custom Public View",

                        Description = "A Saved Query created in code",

                        ReturnedTypeCode = "Associated Hardware View",

                        FetchXml = fetchXml,

                        LayoutXml = layoutXml,

                        QueryType = 0

                    };


                    service.Update(sq); //service = IOrganizationService

                    Entity entity = new Entity("savedquery");

                    entity.Id = viewId;

                    entity["fetchxml"] = fetchXml;

                    entity["layoutxml"] = layoutXml;

                    service.Update(entity);

I have the same question (0)
  • Suggested answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    savedquery is the logicalname of System Views, so if your plan is to update a system view then you need to deal with savedquery inside the C# SDK.

    you get SavedQuery object if you generate the Early Bound (for example with a tool like Early Bound Generator inside XrmToolBox). Early Bound means that CRM entities are typed, so you have Contact with properties FirstName, LastName, etc etc, instead of dealing with late bound where it's not typed and you deal with Entity and the field names as string.

    In the code you posted this part is Early Bound:

    SavedQuery sq = new SavedQuery
    {
        Name = "A New Custom Public View",
        Description = "A Saved Query created in code",
        ReturnedTypeCode = "Associated Hardware View",
        FetchXml = fetchXml,
        LayoutXml = layoutXml,
        QueryType = 0
    };

    but the last part is late bound:

    Entity entity = new Entity("savedquery");
    entity.Id = viewId;
    entity["fetchxml"] = fetchXml;
    entity["layoutxml"] = layoutXml;
    service.Update(entity);

    technically if you just want to update the view, the late bound part is enough as it's written, you can comment the early bound and check if it works as you expect.

    hope it helps

  • PsychoFrettchen Profile Picture
    25 on at

    Thanks for the fast reply, it helped in Visual Studio. However, in CRM it does not have any effect. Could it be because of the structure of the layout?

    The one from above:

    System.String layoutXml =

    @"<grid name='resultset' object='3' jump='name' select='1'

      preview='1' icon='1'>

      <row name='result' id='opportunityid'>

      <cell name='name' width='150' />

      <cell name='customerid' width='150' />

      <cell name='estimatedclosedate' width='150' />

      <cell name='estimatedvalue' width='150' />

      <cell name='closeprobability' width='150' />

      <cell name='opportunityratingcode' width='150' />

      <cell name='opportunitycustomeridcontactcontactid.emailaddress1'

          width='150' disableSorting='1' />

      </row>

    </grid>";

    The view I want to update looks like this:

    (I changed the XML to the code below and removed some lines for the test)

    System.String layoutXml =

    @"<grid name='resultset' object='10003' jump='comf_name' select='1' icon='1' preview='1'>

    <row name='result' id='comf_mainframeid'>

    <cell name='name' width='300'/>

    <cell name='primaryaccountid' width='100'/>

    <cell name='parent_account' width='100'/>

    <cell name='mainframetypeid' width='100'/>

    <cell name='usage' width='125'/>

    <cell name='systemnumber' width='100'/>

    <cell name='node_name' width='100'/>

    <cell name='cpus' width='100'/>

    <cell name='cores_per_cpu' width='100'/>

    <cell name='decommisiondate' width='100'/>

    </row>

    </grid>

    Another mistake could be the registering ?

    Message: RetrieveMultiple

    primary Entity: account

    Stage: Pre-Operation

    Mode: Synchronous

    Thanks in advance !

  • Verified answer
    Guido Preite Profile Picture
    54,086 Moderator on at

    I don't know if the xml for the views are correct or not, but based on the information you posted, I strongly suggest to avoid updating a system view inside a plugin triggered on a retrieve multiple of an account (to be honest if not strictly necessary all plugins on retrieve multiple should be avoided). I don't know what you want to achieve with this code, but double check if you can do it with security roles.

    hope it helps

  • PsychoFrettchen Profile Picture
    25 on at

    Thanks for your help. I found another solution to achieve what I wanted to do because of your last reply. I've achieved it with the security roles.

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 70

#3
Jimmy Passeti Profile Picture

Jimmy Passeti 50 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans