web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

CRM Plugin - need to serialize to JSON

(0) ShareShare
ReportReport
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

I have the same question (0)
  • Verified answer
    ashlega Profile Picture
    34,477 on at

    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();
                }
            }


  • Verified answer
    ashlega Profile Picture
    34,477 on at
    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(); } }



  • ChrisJC Profile Picture
    1,540 on at

    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

  • ashlega Profile Picture
    34,477 on at

    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:) )

  • MaKeer Profile Picture
    on at

    Hi,

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



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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans