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

Announcements

No record found.

News and Announcements icon
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();
        }

  • Verified answer
    Martin Dráb Profile Picture
    241,656 Most Valuable Professional on at

    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

    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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Women in Power Builds Momentum

Expanding mentorship, skilling, and AI innovation

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 239 Super User 2026 Season 2

#2
Aayush Tiwari Profile Picture

Aayush Tiwari 221

#3
Subra Profile Picture

Subra 166 Super User 2026 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans