Post request to deep insert / complexe entities

May 18, 2012 at 4:24 AM
Edited May 18, 2012 at 9:11 AM

Hi all,

I need to make a post request to the back end to perform a deep insert update.

The service has already been programed to perform deep insert. However, when I post a request like this :

var request = {headers: {"X-Requested-With": "XMLHttpRequest",
                                      "Content-Type": "application/atom+xml",
                                      "DataServiceVersion": "2.0", "X-CSRF-Token" : token }, 
                      requestUri: ".....", 
                      method: "POST", 
                      data: updateTimesheetData, <--- this is an array 
                      user: uname,
                      password: pword}; 
OData.request( request, 
           function (data, response) {
                       //Success Callback alert("success");                       
                       //addTimeCallBackParse(data, entryMessageCount);
           }, function (err) {
                       //Error Callback: errorHandler(err); 
           } );

In fiddler, the request body that was formed with the above codes looked like this :

<feed xmlns="http://www.w3.org/2005/Atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"

xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<entry>
    <author><name/></author><title/>
    <content type="application/xml">
        <m:properties>
            <d:OrderId>0</d:OrderId>
            <d:errorCount m:null="true"/>
            <d:message1 m:null="true"/>
            <d:message2 m:null="true"/>
            <d:message3 m:null="true"/>
            <d:message4 m:null="true"/>
            <d:message5 m:null="true"/>
            <d:message6 m:null="true"/>
            <d:message7 m:null="true"/>
        </m:properties>
    </content>
</entry>
<entry>
    <author><name/></author><title/>
    <content type="application/xml">
        <m:properties>
            <d:OrderId>0</d:OrderId>
            <d:date>2012-0515T00:00:00</d:date>
            <d:code>.....</d:code>
            <d:WBSElement>.....</d:WBSElement>
            <d:activityCode>....</d:activityCode>
            <d:workType>....</d:workType>
            <d:hours xml:space="preserve"xmlns:xml="http://www.w3.org/XML/1998/namespace">8.00</d:hours>
        </m:properties>
    </content>
</entry>

</feed>

 

The correct request body which I need to generate looks like this :

<?xml version="1.0" encoding="UTF-8"?>
<atom:entry
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<atom:content type="application/xml">
<m:properties>
<d:OrderId>0</d:OrderId>
<d:errorCount m:null="true" />
<d:message1 m:null="true" />
<d:message2 m:null="true" />
<d:message3 m:null="true" />
<d:message4 m:null="true" />
<d:message5 m:null="true" />
<d:message6 m:null="true" />
<d:message7 m:null="true" />
</m:properties>
</atom:content>
<atom:link
rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/SOItems"
type="application/atom+xml;type=feed"
title="TSINSERT.SOHeader_SOItems">
<m:inline>
<atom:feed>
<atom:entry>
<atom:content type="application/xml">
<m:properties>
<d:OrderId>0</d:OrderId>
<d:date>...</d:date>
<d:code m:null="true" />
<d:WBSElement>...</d:WBSElement>
<d:activityCode>...</d:activityCode>
<d:workType>....</d:workType>
<d:hours>8</d:hours>
</m:properties>
</atom:content>
</atom:entry></atom:feed>
</m:inline>
</atom:link>
</atom:entry>

 

I am currently facing a HTTP 500 internal server error which I assume is due to the generated request body is not right.

Do anyone have any idea on how to generate the following request body with datajs?

All advice and input are really appreciated.

Thank you!

Amanda

May 30, 2012 at 8:29 PM

Hi Amanda,

    Inserting entities in OData has to be done one at a time. At this point datajs is very low level and will not go to far other than serializing whatever data you passed to it (and taking care of all the request handling and that sort of stuff for you).  If you want to make multiple inserts in a single request you can use a batch request:  (http://datajs.codeplex.com/wikipage?title=OData%20Code%20Snippets&referringTitle=Using%20OData scroll down to the "Adding Data in Batches" section)

    or you can do the following:

    

for (var item in updateTimeSheedData) {
    var request = {
         headers: {
             "X-Requested-With": "XMLHttpRequest",
             "Content-Type": "application/atom+xml",
             "DataServiceVersion": "2.0", 
             "X-CSRF-Token" : token }, 
         requestUri: ".....", 
         method: "POST", 
         data: updateTimeSheedData[item], <--- this is an array 
         user: uname,
         password: pword}; 
    
    OData.request(request, 
        function (data, response) {
            //Success Callback alert   ("success");                       
            //addTimeCallBackParse(data, entryMessageCount);
        }, function (err) {
            //Error Callback: errorHandler(err); 
        });
}

Regards,

Alex Trigo.

 

Mar 2, 2013 at 9:03 PM
This is obsolete as now datajs handles batch requests