Search code examples
xmlazuredynamics-crmazure-data-factoryfetchxml

How to replace row delimiter character with another character?


Here the line in which i am fetching the "comment" attribute from dynamics CRM using FetchXml

attribute name="comment"

If in a comment attribute the "/r/n" is coming in between them and if i want to replace that with some another special character, how can I replace "/r/n" with another, in FetchXml query?


Solution

  • You can't replace r/n/ as part of the FetchXml query (This would actually mean doing an update on the comment on the database). You first retrieve the records, and then do the replacing.

    For example, say you've got the comment field on the Account entity, then (on server side):

    string fetch = @"  
       <fetch version='1.0' mapping='logical' distinct='false'>  
         <entity name='account'>   
            <attribute name='comment'/>   
         </entity>   
       </fetch> ";   
    
    EntityCollection result = _serviceProxy.RetrieveMultiple(new FetchExpression(fetch));
    foreach (var account in result.Entities)
    {
       // replace \r\n with a comma
       string comment = account.GetAttributeValue<string>("comment"); 
       if (!string.IsNullOrEmpty(comment))
       {
           var lines = comment.Split(new string[] { "\r\n" }, StringSplitOptions.None);
           Console.WriteLine(string.Join(",", lines)); 
       } 
    }