Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Answered

assign value to C# list<KeyValuePair<string,string> from X++

(0) ShareShare
ReportReport
Posted on by 225

Hello,

I have C# class. in that I have two properties of type "List<KeyValuePair<string,string>>" as shown in below screenshot. can anyone tell me how I can assign value to this from X++ ?

pastedimage1614361187741v1.png

In X++ I have created object of this class. and I want to assign value like:

object.ViewPrinciplesKeyValue = [{"Key":"Everyone","Value":"Everyone"}].

Is there anything like JsonArray, JsonObject in X++?

  • Verified answer
    Martin Dráb Profile Picture
    232,871 Most Valuable Professional on at
    RE: assign value to C# list<KeyValuePair<string,string> from X++

    You said you wanted to assign a string. To do that, you need to call JsonConvert.DeserializeObject to convert the string to a generic list. So why don't you wrap this logic in a string property of the C# class? It could look somehow like this:

    public string ViewPrinciplesJson
    {
        get
        {
            return JsonConvert.SerializeObject(ViewPrinciplesKeyValue);
        }
        set
        {
            ViewPrinciplesKeyValue = JsonConvert.DeserializeObject(value);
        }
    }

    Using such a property from X is trivial - you'll simply assign a string to it, as you asked for:

    object.ViewPrinciplesJson = [{"Key":"Everyone","Value":"Everyone"}];

  • Pratik Rami Profile Picture
    225 on at
    RE: assign value to C# list<KeyValuePair<string,string> from X++

    We have to call JsonConvert.DeserializeObject in C#.

    Martin, sorry but i am not able to understand what you said in last para. Can you please explain little more. I have to assign list to this property.

    What I am doing is: I create object of this class in X++...assign all properties values and then send this object as parameter to C# method which will call external api. In that method this object will be serialised and used as a requestData(body).

  • Martin Dráb Profile Picture
    232,871 Most Valuable Professional on at
    RE: assign value to C# list<KeyValuePair<string,string> from X++

    X++ doesn't officially support generics, therefore working with generic types from X++ isn't easy. Usually the best option is providing an alternative, non-generic interface that you can easily call from X++.

    By the way, does C# support an implicit deserialization of strings to JsonProperty objects, as your examples suggests? I would think that you need to explicitly call a deserializer (e.g. JsonConvert.DeserializeObject).

    Anyway, if you intention is to assign a string, create a string property (such as ViewPrinciplesJson) which will do the deserialization and assing the value of ViewPrinciplesKeyValue.

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

🌸 Community Spring Festival 2025 Challenge 🌸

WIN Power Platform Community Conference 2025 tickets!

Jonas ”Jones” Melgaard – Community Spotlight

We are honored to recognize Jonas "Jones" Melgaard as our April 2025…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 294,125 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 232,871 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,158 Moderator

Leaderboard

Product updates

Dynamics 365 release plans