function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ZhenZhen 

Bulk API Sample app using C#

Does anyone know how to use Bulk API in C#?

the Web Service API can only update max 200 records at once.

Bulk API can update 10,000 records at once.

I only find sample code using Java.

Thanks.

 

kzmpkzmp

Hi,

I would recommend you to download the SalesForce Data Loader from SalesForce I believe and study that.

Although it is java it can easily be translated into C#.

 

Regards,

Kos

dkadordkador

The bulk API is rest-based, so all you need to do is write C# code that can send and receive HTTP requests to our servers.  Porting DataLoader to C# would not be trivial.

JSmithJSmith

Any more recent code samples for the BULK API using C#?  Still needed.

Thanks.

lane_cwelane_cwe

I tried to create a job but request.GetResponse() line returns error message - (400) Bad Request. Below is the code, any help is much appreciated.

 

 private void CreateJob()
        {
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://na2.salesforce.com/services/async/24.​0/job");
            request.Method = WebRequestMethods.Http.Post;
            request.ContentType = "application/xml; charset=UTF-8";
            request.Headers.Add("X-SFDC-Session", _SessionID);
            request.KeepAlive = false;
            request.Host = "na2.salesforce.com";
            request.UserAgent = ".NET Framework Test Client";
            request.Accept = "application/xml";
            
            string body = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
            body += "<jobInfo xmlns=\"http://www.force.com/2009/06/asyncapi/dataload\">";
            body += "<operation>query</operation>";
            body += "<object>Account</object>";
            body += "<concurrencyMode>Parallel</concurrencyMode>";
            body += "<contentType>XML</contentType>";
            body += "</jobInfo>";

            byte[] byteArray = Encoding.UTF8.GetBytes(body);
            request.ContentLength = byteArray.Length;

            Stream datastream = request.GetRequestStream();
            datastream.Write(byteArray, 0, byteArray.Length);
            datastream.Close();

            WebResponse response = request.GetResponse(); //This line returns (400) Bad Request
            datastream = response.GetResponseStream();
            StreamReader reader = new StreamReader(datastream);
            string responseFromServer = reader.ReadToEnd();

            response.Close();
        }

 

 

 

Longhorn94Longhorn94

Try this code to see more info about the error that's getting returned.  The responseFromServer string should contain more details about the error.

 

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(string.Format("{0}/{1}", _SFDCJobUrlBase, JobID));
            request.Method = WebRequestMethods.Http.Get;
            request.Headers.Add(_SFDCSessionHeaderTag, _SessionID);
            request.Host = _Host;
            request.UserAgent = _UserAgent;
            request.Accept = _Accept;
            try
            {
                //Call the service and get the response
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                if (HttpStatusCode.OK == response.StatusCode)
                {
                    Stream dataStream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(dataStream);
                    responseFromServer = reader.ReadToEnd();
                    response.Close();
                }
            }
            catch (WebException e)
            {
                using (WebResponse response = e.Response)
                {
                    HttpWebResponse httpResponse = (HttpWebResponse)response;
                    responseFromServer = string.Format("Error code: {0}  ", httpResponse.StatusCode);
                    using (Stream data = response.GetResponseStream())
                    {
                        responseFromServer += new StreamReader(data).ReadToEnd();
                        return responseFromServer;
                    }
                }
            }

 

lane_cwelane_cwe

Thank you Longhorn94. That helps.

NK123NK123

Hi Lane,

 

What did you do to resolve your issue with this line below:

Please let me know. I am getting the same issue.

 

Thanks,

NK

 

 WebResponse response = request.GetResponse(); //This line returns (400) Bad Request
Dhaval PanchalDhaval Panchal
I am also getting same issue.

I am getting below response

Error code: BadRequest 
<?xml version="1.0" encoding="UTF-8"?>
<error  xmlns="http://www.force.com/2009/06/asyncapi/dataload">
      <exceptionCode>InvalidUrl</exceptionCode>
      <exceptionMessage>unknown version: 10.0</exceptionMessage>
</error>
govindb1.396432156444516E12govindb1.396432156444516E12
How you are getting _SessionID.
Sorry but I am new in this