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

Community site session details

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

how can I pass list of object to data contract

(0) ShareShare
ReportReport
Posted on by 1,025

Hello expert please i need help to pass list of object to data contract

first, i create a data contract

[DataContract('Colors')]
class ColorDataContract
{
    Name colorName;
    DataAreaId dataAreaId;
    [DataMember]
    public Name parmColorName(Name _colorName = colorName)
    {
        colorName = _colorName;
        return colorName;
    }
}

second, create a service class

class ColorService
{
    public void InsertColor(ColorDataContract colorObj)
    {
        ColorsTable _color;
        Name _name;
    
        _name = colorObj.parmColorName();
    
        ttsbegin;
        
        _color.Name = _name;
        _color.insert();
        
        ttscommit;
        }
    }
    
    public void insertColorList(List colorList)
    {
        ListIterator iterator;
        ListEnumerator enumerator;
        ListIterator literator;
        ColorDataContract colorDataContract;
    
        enumerator = colorList.getEnumerator();
    
        while(enumerator.moveNext())
        {
            colorDataContract = enumerator.current();
    
            if(colorDataContract != null)
            {
                this.InsertColor(colorDataContract);
            }
        }
    }

}

after that, I create a new service group and service and assign the method to the service, etc. everything until this point is working fine

In the code below i try to consume custom service in json using the Microsoft simple integration

github.com/.../d365fo.integrations

my problem is how to pass the list to my contract into JSON
i can pass data to insertColor method without any issue my problem with insertColorList method

static void Main(string[] args)
        {
            string sessionUrl = "/api/services/XXXServices/XXXColors/insertColorList";
            string GetUserSessionOperationPath = string.Format("{0}{1}", ClientConfiguration.Default.UriString.TrimEnd('/'), sessionUrl);
            var request = HttpWebRequest.Create(GetUserSessionOperationPath);

            //how to pass the list to my service
            
            
            //Post one color
            //var con = new ArafaContractList();
            //con.parmColorName = "Blue";

            //var contract = new { colorObj = con };

            string json = JsonConvert.SerializeObject(contract);
            Byte[] byteArray = Encoding.UTF8.GetBytes(json);
            request.Headers[OAuthHelper.OAuthHeader] = OAuthHelper.GetAuthenticationHeader(true);
            request.Method = "POST";
            request.ContentLength = byteArray.Length;
            request.ContentType = "application/json";

            using (var stream = request.GetRequestStream())
            {
                stream.Write(byteArray, 0, byteArray.Length);
            }

            using (var response = (HttpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    using (StreamReader streamReader = new StreamReader(responseStream))
                    {
                        string responseString = streamReader.ReadToEnd();

                        Console.WriteLine(responseString);
                    }
                }
            }

            Console.ReadLine();
        }

I have the same question (0)
  • Verified answer
    Martin Dráb Profile Picture
    237,681 Most Valuable Professional on at
    RE: how can I pass list of object to data contract

    What problem do you have with insertColorList?

    One thing I noticed it that the method isn't decorated with AifCollectionTypeAttribute. You should add it:

    [AifCollectionTypeAttribute('colorList', Types::Class, classStr(ColorDataContract))]
    public void insertColorList(List colorList)

  • source258147 Profile Picture
    1,025 on at
    RE: how can I pass list of object to data contract

    Thanks Martin Dráb

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 > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 683 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 563 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 398 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans