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

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Create and Update Views for entity Programatically.

(0) ShareShare
ReportReport
Posted on by 3,355

Hello Experts,

I would like to create new views and update the existing views. I have a xml file which contain the fetchxml and layout xml for entity. I have to get the fetch and layout from the  xml file and execute the create and update method. For this I am trying to retrieve the existing view by passing view name and if exist update the view and if not exist, create view. Till getting the fetch and layout, is fine. but retrieving and creating view no luck.

Please Suggest.

Saroj

*This post is locked for comments

I have the same question (0)
  • Mahendar Pal Profile Picture
    45,095 on at

    Hi Saroj,

    Did you check this SDK sample: msdn.microsoft.com/.../gg594431.aspx

  • Suggested answer
    Bruno Lucas Profile Picture
    5,421 on at

    This is the msdn:

    msdn.microsoft.com/.../gg594431.aspx

    but this post is a little better explained

    www.resultondemand.nl/.../a6b5d363-4186-4bc8-a7eb-62f308fa9ef9.htm

  • Saroj Das Profile Picture
    3,355 on at

    Hi Mahendar, thank you very much for your reply. I checked the sample. I want to do it in Latebound not in earlybound.

    Please suggest.

    Thank you

  • Aileen Gusni Profile Picture
    44,524 on at

    Saroj,

    Yes, you are right, you need the fetchXml and aklso the layoutXml

    To create a new view:

    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 = "opportunity",

       FetchXml = fetchXml,

       LayoutXml = layoutXml,

       QueryType = 0

    };

    _orgService.Create(sq);

    For updating:

    (Latebound)

    Entity entity = new Entity("savedquery");

    entity.Id = guidOfViewyouwanttoedit;

    entity.FetchXML = fetchXml;

    entity.LayoutXML = layoutXML;

    service.Update(entity);

    That's all.

    But remember this is for system view, for user view you need to query the userquery entity.

    Hope this helps.

    Thank you.

  • Verified answer
    Aileen Gusni Profile Picture
    44,524 on at

    Saroj,

    Yes, you are right, you need the fetchXml and aklso the layoutXml

    To create a new view:

    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 = "opportunity",

       FetchXml = fetchXml,

       LayoutXml = layoutXml,

       QueryType = 0

    };

    _orgService.Create(sq);

    For updating:

    (Latebound)

    Entity entity = new Entity("savedquery");

    entity.Id = guidOfViewyouwanttoedit;

    entity.["fetchxml"] = fetchXml;

    entity.["layoutxml"] = layoutXML;

    service.Update(entity);

    if for creating then you just pass the rest attributes and change it to the service.Create(entity);

    That's all.

    But remember this is for system view, for user view you need to query the userquery entity.

    Hope this helps.

    Thank you.

  • Mahendar Pal Profile Picture
    45,095 on at

    Are you facing any issue while converting this code in late bound ?? you should be able to convert it easily using late bound, you just need to change your strong type entity name with Entity object and schema names with logical fields name that you can get from CRM.

    Thanks.

  • Saroj Das Profile Picture
    3,355 on at

    Hi Aileen, Thank you very much for your awesome reply. It worked for me.

    Thank you very much again.

    Saroj

  • Saroj Das Profile Picture
    3,355 on at

    Hi Mahendar, Thank you for your time and reply. I didn't get any issue while creating late bound.

    Thank you

  • Jharana Baliyar Singh Profile Picture
    2,667 on at

    Hi All,

    I want to know Is it possible to create Personal view through programatically ?

    When i am doing this its creating system view .So here instead of system view need personal view.Is it possible ????

    Please suggest anyone !!

  • Saroj Das Profile Picture
    3,355 on at

    Hi Jharana,

    We can create Personal view programatically. As this thread is closed, can you please create a new thread so that i can help you on this.

    Thanks,

    Saroj

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans