Exemplo n.º 1
0
        private static ApexSharpConfig GetNewConnection(ApexSharpConfig config)
        {
            var xml = @"
                <soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:urn=""urn:enterprise.soap.sforce.com"">
                    <soapenv:Header>
                        <urn:LoginScopeHeader>
                        <urn:organizationId></urn:organizationId>
                        <urn:portalId></urn:portalId>
                        </urn:LoginScopeHeader>
                    </soapenv:Header>
                    <soapenv:Body>
                        <urn:login>
                            <urn:username>" + config.SalesForceUserId + "</urn:username>" +
                      "<urn:password>" + config.SalesForcePassword + config.SalesForcePasswordToken + "</urn:password>" +
                      "</urn:login>" +
                      "</soapenv:Body>" +
                      "</soapenv:Envelope>";


            var returnXml = PostLoginTask(config.SalesForceSoapUrl, xml);

            if (returnXml.Contains("INVALID_LOGIN"))
            {
                throw new SalesForceInvalidLoginException("Invalid Login");
            }
            else if (returnXml.Contains("API_DISABLED_FOR_ORG"))
            {
                throw new SalesForceInvalidLoginException("Webservice API is disabled for this login or organization.");
            }

            try
            {
                Envelope envelope = UtilXml.DeSerilizeFromXML <Envelope>(returnXml);


                var soapIndex =
                    envelope.Body.loginResponse.result.serverUrl.IndexOf(@"/Soap", StringComparison.Ordinal);
                var restUrl       = envelope.Body.loginResponse.result.serverUrl.Substring(0, soapIndex);
                var restSessionId = "Bearer " + envelope.Body.loginResponse.result.sessionId;


                config.SalesForceSoapUrl       = envelope.Body.loginResponse.result.serverUrl;
                config.RestUrl                 = restUrl;
                config.RestSessionId           = restSessionId;
                config.SessionCreationDateTime = DateTimeOffset.Now.ToUnixTimeSeconds() +
                                                 envelope.Body.loginResponse.result.userInfo.sessionSecondsValid;

                return(config);
            }
            catch (Exception e)
            {
                Console.WriteLine();
                Console.WriteLine("Fix the error and restart " + e);
                Console.ReadLine();
                System.Environment.Exit(0);
            }

            return(null);
        }
Exemplo n.º 2
0
        public string BulkRequest <T>(int checkIntervel)
        {
            SoqlCreator soql  = new SoqlCreator();
            string      query = soql.GetSoql <T>();

            var objectName = GetSalesForceObjectName <T>();

            var url     = "/async/36.0/job/";
            var jobInfo = new JobInfoRequest
            {
                operation       = "query",
                @object         = objectName,
                concurrencyMode = "Parallel",
                contentType     = "JSON",
            };
            var xml = UtilXml.SerializeToXML(jobInfo);

            var waitTask = Post(url, xml, "application/xml");

            waitTask.Wait();

            var jobInfoReply = UtilXml.DeSerilizeFromXML <JobInfoReply>(waitTask.Result);

            waitTask = Post(url + jobInfoReply.id + "/batch", query, "application/json");
            waitTask.Wait();

            var batchJobReply = JsonConvert.DeserializeObject <BatchJobReply>(waitTask.Result);

            while (batchJobReply.state != "Completed")
            {
                waitTask = Get(url + jobInfoReply.id + "/batch/" + batchJobReply.id);
                waitTask.Wait();
                var json = waitTask.Result;
                batchJobReply = JsonConvert.DeserializeObject <BatchJobReply>(json,
                                                                              new JsonSerializerSettings {
                    NullValueHandling = NullValueHandling.Ignore
                });

                if (batchJobReply.state != "Completed")
                {
                    Console.WriteLine($"Job {batchJobReply.state}, Waiting for {checkIntervel} miliseconds");
                    Thread.Sleep(checkIntervel);
                }
            }


            waitTask = Get(url + jobInfoReply.id + "/batch/" + batchJobReply.id + "/result");
            waitTask.Wait();

            var resultLength = waitTask.Result.Length;
            var result       = waitTask.Result.Substring(2, resultLength - 4);

            waitTask = Get(url + jobInfoReply.id + "/batch/" + batchJobReply.id + "/result/" + result);
            waitTask.Wait();

            return(waitTask.Result);
        }
Exemplo n.º 3
0
        public static void CreateInstanceBinder()
        {
            string xml          = File.ReadAllText(ApexSharp.GetSession().SalesForceLocation + @"\labels\CustomLabels.labels");
            var    customLabels = UtilXml.DeSerilizeFromXML <CustomLabels>(xml);

            foreach (var customLabel in customLabels.labels)
            {
                Console.WriteLine(customLabel.fullName + "  " + customLabel.value);
            }
        }
Exemplo n.º 4
0
        private static ApexSharpConfig GetNewConnection(ApexSharpConfig config)
        {
            var xml = @"
                <soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:urn=""urn:enterprise.soap.sforce.com"">
                    <soapenv:Header>
                        <urn:LoginScopeHeader>
                        <urn:organizationId></urn:organizationId>
                        <urn:portalId></urn:portalId>
                        </urn:LoginScopeHeader>
                    </soapenv:Header>
                    <soapenv:Body>
                        <urn:login>
                            <urn:username>" + config.SalesForceUserId + "</urn:username>" +
                      "<urn:password>" + config.SalesForcePassword + config.SalesForcePasswordToken + "</urn:password>" +
                      "</urn:login>" +
                      "</soapenv:Body>" +
                      "</soapenv:Envelope>";


            var retrunXml = PostLoginTask(config.SalesForceUrl, xml);

            if (retrunXml.Contains("INVALID_LOGIN"))
            {
                throw new SalesForceInvalidLoginException("Invalid Login");
            }
            Envelope envelope = UtilXml.DeSerilizeFromXML <Envelope>(retrunXml);

            var soapIndex     = envelope.Body.loginResponse.result.serverUrl.IndexOf(@"/Soap", StringComparison.Ordinal);
            var restUrl       = envelope.Body.loginResponse.result.serverUrl.Substring(0, soapIndex);
            var restSessionId = "Bearer " + envelope.Body.loginResponse.result.sessionId;


            config.SalesForceUrl           = envelope.Body.loginResponse.result.serverUrl;
            config.SessionId               = envelope.Body.loginResponse.result.sessionId;
            config.RestUrl                 = restUrl;
            config.RestSessionId           = restSessionId;
            config.SessionCreationDateTime = DateTimeOffset.Now.ToUnixTimeSeconds() + envelope.Body.loginResponse.result.userInfo.sessionSecondsValid;

            return(config);
        }
Exemplo n.º 5
0
        public async Task <string> Post(string url, string json, string dataType)
        {
            HttpRequestMessage request = new HttpRequestMessage
            {
                RequestUri = new Uri(_connectionDetail.RestUrl + url),
                Method     = HttpMethod.Post,
            };

            request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            request.Headers.Add("X-SFDC-Session", _connectionDetail.SessionId);

            Console.WriteLine(UtilXml.FormatXml(json));
            request.Content = new StringContent(json, Encoding.UTF8, dataType);

            HttpClient          httpClient      = new HttpClient();
            HttpResponseMessage responseMessage = await httpClient.SendAsync(request);

            switch (responseMessage.StatusCode)
            {
            case HttpStatusCode.Created:
                string jsonData = responseMessage.Content.ReadAsStringAsync().Result;
                return(jsonData);

            case HttpStatusCode.BadRequest:
                Console.WriteLine(responseMessage.Content.ReadAsStringAsync().Result);
                break;

            case HttpStatusCode.Unauthorized:
                Console.WriteLine(responseMessage.Content.ReadAsStringAsync().Result);
                break;

            default:
                Console.WriteLine(responseMessage.Content.ReadAsStringAsync().Result);
                break;
            }
            return(null);
        }