Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

CRM Plugin - need to serialize to JSON

Posted on by 1,540

Hi,

I'm struggling to figure out how I need to create a JSON string to send to an external API (Campaign Monitor). Unfortunately this being CRM Online I can't use there wrappers which would have made things a lot easier.

I have two main queries (assuming I'm approaching this correctly). 1 I need to format my DataContract correctly so I can build the child elements. I currently have this but don't think it's quite right:

[DataContract(Namespace = "")]
    public class smartEmail
    {
        [DataMember]
        public string To { get; set; }
        [DataMember]
        public IEnumerable<KeyValuePair<string, string>> Data { get; set; }

        [Serializable]
        public struct KeyValuePair<K, V>
        {
            public K Key { get; set; }
            public V Value { get; set; }
        }
    }


It needs to look something like:

{
    "To": [
        "Joe Smith <joesmith@example.com>"
    ],
    "Data": {
        "firstname": "Joe",
        "lastname": "Smith"
    }
}


And secondly my code to generate the object, particularly the data element. Here is what I have:

//create data
Dictionary<string, object> data = new Dictionary<string, object> { };
data.Add("firstname", "Joe");
data.Add("lastname", "Smith");

//"create email
smartEmail CMEmail = new smartEmail();
CMEmail.To = "Joe Smith<joesmith@example.com>";

//Build JS
string wsData = "";
using (var ms = new MemoryStream())
{
    var js = new DataContractJsonSerializer(typeof(smartEmail));
    js.WriteObject(ms, CMEmail);
    ms.Position = 0;
    var sr = new StreamReader(ms);
    wsData = sr.ReadToEnd();
}
// deal with wsData string...


*This post is locked for comments

  • MaKeer Profile Picture
    MaKeer on at
    RE: CRM Plugin - need to serialize to JSON

    Hi,

    Does "WriteObject" works in Sandbox mode? For me its CRM Online, and it is failing at DataContractJsonSerializer.WriteObject method



  • ashlega Profile Picture
    ashlega 34,475 on at
    RE: CRM Plugin - need to serialize to JSON

    That I'm not sure about. If you make it a "collection" of some sort, it'll probably serialize into

    data:

    [

      {"name":"value"},

      {"name":"value"}

    ]

    and it's not what you need. Pretty sure it's doable, but I can't do it off the top of my head (actually, you might want to ask this in the C# forums.. it's the bread and butter for those folks:) )

  • ChrisJC Profile Picture
    ChrisJC 1,540 on at
    RE: CRM Plugin - need to serialize to JSON

    Hi Alex,

    Many thanks - that sorts out what I need to do. My only question is what if I need a dynamic 'key'. A bit like late-bound in that inside data I might need to pass in a field that I'm not yet aware of.

    Thanks,

    Chris

  • Verified answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: CRM Plugin - need to serialize to JSON
    And if "To" is supposed to be an array, you can change it like this:

    [DataContract(Namespace = "")] public class FullName { [DataMember] public string firstname; [DataMember] public string lastname; } [DataContract(Namespace = "")] public class smartEmail { [DataMember] public List<string> To; [DataMember] public FullName data; } static void Test() { //"create email smartEmail CMEmail = new smartEmail(); CMEmail.To = new List<string>(); CMEmail.To.Add("Joe Smith<joesmith@example.com>"); CMEmail.data = new FullName(){ firstname = "Joe", lastname = "Smith" }; //Build JS string wsData = ""; using (var ms = new MemoryStream()) { var js = new DataContractJsonSerializer(typeof(smartEmail)); js.WriteObject(ms, CMEmail); ms.Position = 0; var sr = new StreamReader(ms); wsData = sr.ReadToEnd(); } }



  • Verified answer
    ashlega Profile Picture
    ashlega 34,475 on at
    RE: CRM Plugin - need to serialize to JSON

    Hi,

      you can try this:

           [DataContract(Namespace = "")]
            public class FullName
            {
                [DataMember]
                public string firstname;
                [DataMember]
                public string lastname;
            }
            [DataContract(Namespace = "")]
            public class smartEmail
            {
                [DataMember]
                public string To { get; set; }
                [DataMember]
                public FullName data;
            }
            static void Test()
            {
                
                
    
                //"create email
                smartEmail CMEmail = new smartEmail();
                CMEmail.To = "Joe Smith<joesmith@example.com>";
                CMEmail.data = new FullName(){
                    firstname = "Joe",
                    lastname = "Smith"
                };
    
                //Build JS
                string wsData = "";
                using (var ms = new MemoryStream())
                {
                    var js = new DataContractJsonSerializer(typeof(smartEmail));
                    js.WriteObject(ms, CMEmail);
                    ms.Position = 0;
                    var sr = new StreamReader(ms);
                    wsData = sr.ReadToEnd();
                }
            }


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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans