Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Answered

how can I pass list of object to data contract

(0) ShareShare
ReportReport
Posted on by 1,021

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

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

    Thanks Martin Dráb

  • Verified answer
    Martin Dráb Profile Picture
    Martin Dráb 230,842 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)

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,969 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,842 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans