Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

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

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);

  • PsychoFrettchen Profile Picture
    PsychoFrettchen 25 on at
    RE: What is SavedQuery and how do I call it / define it?

    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.

  • Verified answer
    Guido Preite Profile Picture
    Guido Preite 54,063 Moderator on at
    RE: What is SavedQuery and how do I call it / define it?

    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
    PsychoFrettchen 25 on at
    RE: What is SavedQuery and how do I call it / define it?

    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 !

  • Suggested answer
    Guido Preite Profile Picture
    Guido Preite 54,063 Moderator on at
    RE: What is SavedQuery and how do I call it / define it?

    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

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

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

Dynamics 365 Community Newsletter - September 2024

Check out the latest community news

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,537 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,520 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans