Creating a xml file based on the orders information in CRM Dynamic 2015

Hi All,

I am trying to extract all the data fields in the Order Entity in CRM Dynamics 2015, but need help writing the code in C#.NET.

Can someone please give me some guidance?

Here's a sample of  how the xml output should look like.

-<Invoices >


-<Invoice>


-<Meta>

<Version>testVersion</Version>

</Meta>


-<Header>


-<InvoiceHeader>

<TradingPartnerId>000ALLTESTID</TradingPartnerId>

<InvoiceNumber>99999-123</InvoiceNumber>

<InvoiceDate>2013-10-05</InvoiceDate>

<PurchaseOrderDate>2013-08-29</PurchaseOrderDate>

<PurchaseOrderNumber>1010101010101</PurchaseOrderNumber>

<InvoiceTypeCode>U5</InvoiceTypeCode>

<BuyersCurrency>testBuyersCurrency</BuyersCurrency>

<Vendor>99999999</Vendor>

<CustomerAccountNumber>123456789</CustomerAccountNumber>

<CustomerOrderNumber>987654321</CustomerOrderNumber>

<PromotionDealNumber>55555555</PromotionDealNumber>

<ShipDate>2013-11-05</ShipDate>

</InvoiceHeader>


-<PaymentTerms>

<TermsType>03</TermsType>

<TermsBasisDateCode>7</TermsBasisDateCode>

<TermsDiscountPercentage>5.62</TermsDiscountPercentage>

<TermsDiscountDate>2013-11-28</TermsDiscountDate>

<TermsDiscountDueDays>60</TermsDiscountDueDays>

<TermsNetDueDate>2013-11-15</TermsNetDueDate>

<TermsNetDueDays>90</TermsNetDueDays>

<TermsDiscountAmount>1845.08</TermsDiscountAmount>

<TermsDescription>Must be paid in full by end of year</TermsDescription>

</PaymentTerms>


-<Date>

<DateTimeQualifier1>ORS</DateTimeQualifier1>

<Date1>2012-10-31</Date1>

<Time1>16:13:03-05:00</Time1>

</Date>


-<Contact>

<ContactTypeCode>OC</ContactTypeCode>

<ContactName>SPS Commerce</ContactName>

<PrimaryPhone>866-245-8100</PrimaryPhone>

<PrimaryFax>612-435-9401</PrimaryFax>

<PrimaryEmail>info@spscommerce.com</PrimaryEmail>

</Contact>


-<Address>

<AddressTypeCode>VN</AddressTypeCode>

<LocationCodeQualifier>TPL</LocationCodeQualifier>

<AddressLocationNumber>11111</AddressLocationNumber>

<AddressName>The White House</AddressName>

<Address1>333 South 7th Street</Address1>

<Address2>Suite 1000</Address2>

<City>Washington</City>

<State>DC</State>

<PostalCode>55402</PostalCode>

<Country>US</Country>

  
</Address>


-<Reference>

<ReferenceQual>MR</ReferenceQual>

<ReferenceID>testReferenceID</ReferenceID>

<Description>New products only. Do not reuse packaging</Description>

</Reference>


-<Notes>

<NoteCode>SPE</NoteCode>

<NoteInformationField>REPEAT LOGO PREVIOUS ORDER</NoteInformationField>

</Notes>


-<Tax>

<TaxTypeCode>BE</TaxTypeCode>

<TaxAmount>1845.08</TaxAmount>

<TaxPercent>8.50</TaxPercent>

<JurisdictionQual>MB</JurisdictionQual>

<JurisdictionCode>07034</JurisdictionCode>

<TaxExemptCode>2</TaxExemptCode>

<TaxID>99990000</TaxID>

</Tax>


-<ChargesAllowances>

<AllowChrgIndicator>C</AllowChrgIndicator>

<AllowChrgCode>C540</AllowChrgCode>

<AllowChrgAmt>85.02</AllowChrgAmt>

<AllowChrgHandlingDescription>This will cover the cost of shipping</AllowChrgHandlingDescription>

</ChargesAllowances>


-<FOBRelatedInstruction>

<FOBPayCode>WC</FOBPayCode>

<FOBLocationQualifier>RG</FOBLocationQualifier>

<FOBLocationDescription>Minnesota</FOBLocationDescription>

</FOBRelatedInstruction>


-<CarrierInformation>

<CarrierTransMethodCode>R</CarrierTransMethodCode>

<CarrierAlphaCode>UPGF</CarrierAlphaCode>

<CarrierRouting>UPS Ground Freight</CarrierRouting>

<CarrierEquipmentNumber>4806196209</CarrierEquipmentNumber>


-<ServiceLevelCodes>

<ServiceLevelCode>DS</ServiceLevelCode>

</ServiceLevelCodes>

</CarrierInformation>

</Header>


-<LineItems>


-<LineItem>


-<InvoiceLine>

<LineSequenceNumber>01</LineSequenceNumber>

<BuyerPartNumber>9999-SPS</BuyerPartNumber>

<VendorPartNumber>11155-559999</VendorPartNumber>

<ConsumerPackageCode>093597609541</ConsumerPackageCode>

<EAN>1234567890123</EAN>

<GTIN>12345678901234</GTIN>

<UPCCaseCode>98765432101</UPCCaseCode>

<NatlDrugCode>51456-299</NatlDrugCode>

<InternationalStandardBookNumber>999-0-555-22222-0</InternationalStandardBookNumber>


-<ProductID>

<PartNumberQual>VA</PartNumberQual>

<PartNumber>ABC-456789</PartNumber>

</ProductID>

<PurchasePrice>5.85</PurchasePrice>

<ShipQty>75</ShipQty>

<ShipQtyUOM>RA</ShipQtyUOM>

<ProductSizeDescription>Small</ProductSizeDescription>

<ProductColorDescription>Fire Truck Red</ProductColorDescription>

<ProductMaterialDescription>Faux Fabric</ProductMaterialDescription>


-<NRFStandardColorAndSize>

<NRFColorCode>600</NRFColorCode>

<NRFSizeCode>42-10651</NRFSizeCode>

</NRFStandardColorAndSize>

</InvoiceLine>


-<PriceInformation>

<PriceTypeIDCode>FCP</PriceTypeIDCode>

<UnitPrice>5.48</UnitPrice>

</PriceInformation>


-<ProductOrItemDescription>

<ItemDescriptionType>38</ItemDescriptionType>

<ProductDescription>Super comfortable</ProductDescription>

</ProductOrItemDescription>


-<Reference>

<ReferenceQual>CUDN</ReferenceQual>

<ReferenceID>testReferenceID</ReferenceID>

<Description>New products only. Do not reuse packaging</Description>

</Reference>


-<Notes>

<NoteCode>CAFA0</NoteCode>

<NoteInformationField>REPEAT LOGO PREVIOUS ORDER</NoteInformationField>

</Notes>


-<Tax>

<TaxTypeCode>ST</TaxTypeCode>

<TaxAmount>1845.08</TaxAmount>

<TaxPercent>8.50</TaxPercent>

<JurisdictionQual>SP</JurisdictionQual>

<JurisdictionCode>07034</JurisdictionCode>

<TaxExemptCode>2</TaxExemptCode>

<TaxID>99990000</TaxID>

</Tax>


-<ChargesAllowances>

<AllowChrgIndicator>C</AllowChrgIndicator>

<AllowChrgCode>C550</AllowChrgCode>

<AllowChrgAmt>85.02</AllowChrgAmt>

<AllowChrgHandlingDescription>This will cover the cost of shipping</AllowChrgHandlingDescription>

</ChargesAllowances>

</LineItem>

</LineItems>


-<Summary>

<TotalAmount>4058.92</TotalAmount>

<TotalNetSalesAmount>8504.16</TotalNetSalesAmount>

<TotalTermsDiscountAmount>1258.80</TotalTermsDiscountAmount>

<TotalQtyInvoiced>6859.59</TotalQtyInvoiced>

<TotalWeight>12592</TotalWeight>

<TotalLineItemNumber>3</TotalLineItemNumber>

<InvoiceAmtDueByTermsDate>75.02</InvoiceAmtDueByTermsDate>

<TotalQtyInvoicedUOM>WR</TotalQtyInvoicedUOM>

<TotalWeightUOM>5I</TotalWeightUOM>

</Summary>

</Invoice>

</Invoices>

Open in new window

teknovationAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

teknovationAuthor Commented:
Actually I came across this article which seems to do the job! However, I'm getting wrong values exported to the xml file.

Excellent article!

Output:
<Table1>
<salesorderid>3ef01e3e-9d4d-e511-8108-c4346bac995c</salesorderid>
<name>AAD - 9/15/2015</name>
<customerid>Microsoft.Xrm.Sdk.EntityReference</customerid>
</Table1>

-<Table1>

<name>TEST International11 - 2/6/2012</name>
<ordernumber>12354</ordernumber>
<customerid>Microsoft.Xrm.Sdk.EntityReference</customerid>
<statuscode>Microsoft.Xrm.Sdk.OptionSetValue</statuscode>
<totalamount>Microsoft.Xrm.Sdk.Money</totalamount>
</Table1>

Open in new window


The sales order id and customer id seems to be giving me crap data. Then the total amount and status codes has the values from XRM.SDK!  I am pasting the code where it generates this in my c#, can someone help?!

The error fails here, it probably has something to do with conversion of data types? The string value seems to come over okay. I tried convert to INT32 and (int32), but I guess you can't do that when you are trying to force it to a string.

foreach (var e in xmlResult.Entities)
            {
 XmlElement child_customerid = xml.CreateElement("customerid");
                child_customerid.InnerText = e.Attributes["customerid"].ToString();
                child_root_table.AppendChild(child_customerid);

                XmlElement child_ektronidstring = xml.CreateElement("neu_customerid");
                child_neu_customerid.InnerText =   e.Attributes["neu_customerid"].ToString();
                child_root_table.AppendChild(child_neu_customeridg);

Open in new window

0
teknovationAuthor Commented:
anyone?
0
teknovationAuthor Commented:
Nevermind - i found the solution while searching the net.

https://arunpotti.wordpress.com/2014/12/10/retrieve-records-using-fetchxml-c-sdk-in-crm/
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
teknovationAuthor Commented:
Found the solution
0
teknovationAuthor Commented:
In case the site ever goes down, here's the solution for how to code out the different tag types (money, date time etc.,)

((EntityReference)item.Attributes["parentcustomerid"]).Name + "\n";
((OptionSetValue)item.Attributes["gendercode"]).Value + "\n";
((DateTime)item.Attributes["birthdate"]).ToLocalTime().ToShortDateString().ToString() + "\n";
((Money)item.Attributes["creditlimit"]).Value
((Boolean)item.Attributes["donotsendmm"]).ToString();
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Dynamics

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.