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