Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

How to get attribute values of a related's related entity

(0) ShareShare
ReportReport
Posted on by

Hello All,

For some custom requirement, I have to write custom code to replace the tags values of a Word Document Template from a C# code.  The code should not be written for specific CRM Entity or specific Word Template and should be general. That means i want to avoid writing any static attribute names. The code below is working fine for getting the value of entity's Attributes, its related Entity's attributes, but it doesn't work for related' related entity's attributes ( i want to get only id and its value e.g. Get Customer name from CustomerID ). i.e. I have Resource Entity, its related entity is Project and Project's related entity is Contact, Contact's related entity is Customer. I can get Project entity's attribute values, but not contact name and customer name. Any guidence on below code? to get nth level related entity's attribute values?

public MemoryStream BuildWordDocument(MemoryStream stream, byte[] wordDocumentBinary, Entity CRMEntity)
        {
            stream.Write(wordDocumentBinary, 0, wordDocumentBinary.Length);
            stream.Position = 0;

            WordprocessingDocument selectedDocument = WordprocessingDocument.Open(stream, true);
            Body documentBody = selectedDocument.MainDocumentPart.Document.Body;

            selectedDocument = WordprocessingDocument.Open(stream, true);

            foreach (var wordDocumentContentControl in selectedDocument.ContentControls())
            {
                if (CRMEntity.Attributes.Contains(wordDocumentContentControl.InnerText) == true)
                {
                    if (CRMEntity.Attributes[wordDocumentContentControl.InnerText].GetType().Name == "EntityReference")
                    {
                        EntityReference objReference = (EntityReference)CRMEntity.Attributes[wordDocumentContentControl.InnerText];
                        wordDocumentContentControl.InnerXml = wordDocumentContentControl.InnerXml.Replace(wordDocumentContentControl.InnerText, objReference.Name);
                    }
                    else
                    {
                        wordDocumentContentControl.InnerXml = wordDocumentContentControl.InnerXml.Replace(wordDocumentContentControl.InnerText, Convert.ToString(CRMEntity.Attributes[wordDocumentContentControl.InnerText]));
                    }
                }
            }
            selectedDocument.MainDocumentPart.Document.Save(); //update the word document in memory

            return stream;
        }


*This post is locked for comments

  • Verified answer
    meenxip Profile Picture
    meenxip on at
    RE: How to get attribute values of a related's related entity
    Check below code, which works perfectly fine.


    private const int MAX_RECURSIVE_CALLS = 30; private static int counter = 0; public static string RetrieveEntityData(IOrganizationService _service, Entity crmEntity, string attributeName) { counter++; if (counter <= MAX_RECURSIVE_CALLS) { List<string> keyattributes = new List<string>(); foreach (string attribute in crmEntity.Attributes.Keys) { keyattributes.Add(attribute); } for (int i = 0; i < crmEntity.Attributes.Count; i++) { if (crmEntity.Attributes[keyattributes[i]].GetType().Name == "EntityReference") { EntityReference objEntityReference = (EntityReference)crmEntity.Attributes[keyattributes[i]]; if (objEntityReference.LogicalName != "businessunit" && objEntityReference.LogicalName != "systemuser" && objEntityReference.LogicalName != "organization" && objEntityReference.LogicalName != "team") { Entity existingEntity = _service.Retrieve(objEntityReference.LogicalName, objEntityReference.Id, new ColumnSet(true)); if (existingEntity.Attributes.Contains(attributeName) == true) { if (existingEntity.Attributes[attributeName].GetType().Name == "EntityReference") { EntityReference objInnerEntityReference = (EntityReference)existingEntity.Attributes[attributeName]; counter = 0; return objInnerEntityReference.Name; } else { counter = 0; return Convert.ToString(existingEntity.Attributes[attributeName]); } } else { RetrieveEntityData(_service, existingEntity, attributeName); } } } } } counter = 0; return string.Empty; }

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

Announcing Our 2025 Season 1 Super Users!

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

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Tip: Become a User Group leader!

Join the ranks of valued community UG leaders

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,321 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans