get Contacts based on Account in SugarCRM REST API in C# .Net - c#

i want to retrieve all contacts based on account in sugarcrm rest api using C# .net
i have tried
json = serializer.Serialize(new
{
session = sessionId,
module_name = "accounts",
query = "",
order_by = "",
offset = "0",
select_fields = "",
link_name_to_fields_array = "",
max_results = "2000",
deleted = 0
});
values = new NameValueCollection();
values["method"] = "get_entry_list";
values["input_type"] = "json";
values["response_type"] = "json";
values["rest_data"] = json;
response = client.UploadValues(sugarUrl, values);
responseString = Encoding.Default.GetString(response);
var accountsList = serializer.Deserialize<Dictionary<string, dynamic>>(responseString);
i am able to get all accounts and contacts but i am not getting relations between them i.e which contact belong to which account
Thanks for help in advance
UPDATE :
object[] linkNameToFieldsArray = new object[1]
{
new object[2, 2]
{
{ "name", "contacts" },
{ "value", new string[2]
{ "id", "last_name"
}
}
};
json = serializer.Serialize(new
{
session = sessionId,
module_name = "accounts",
query = "",
order_by = "",
offset = "0",
select_fields = "",
link_name_to_fields_array = linkNameToFieldsArray , ***//just added this to get related records***
max_results = "2000",
deleted = 0
});
values = new NameValueCollection();
values["method"] = "get_entry_list";
values["input_type"] = "json";
values["response_type"] = "json";
values["rest_data"] = json;
response = client.UploadValues(sugarUrl, values);
responseString = Encoding.Default.GetString(response);
var accountsList = serializer.Deserialize<Dictionary<string, dynamic>>(responseString);

Assuming a SugarCRM 6.4 or 6.5 system and API version REST v4_1...
I don't know the C#/.NET syntax/lingo, but 'link_name_to_fields_array' needs to be an array with keys of module names (e.g. "Contacts") and values that are arrays of the fields you want. The JSON would look like this:
{
"session":"asdfasdfsrf9ebp7jrr71nrth5",
"module_name":"Accounts",
"query":"",
"order_by":null,
"offset":0,
"select_fields":[
"id",
"name"
],
"link_name_to_fields_array":[
{
"name":"contacts",
"value":[
"id",
"last_name"
]
}
],
"max_results":"2",
"deleted":false
}
Also - I wrote this to help non PHP-devs interact with this version of the API, since documention is largely PHP based. You may find it useful: https://gist.github.com/matthewpoer/b9366ca4197a521a600f

Related

403 Error using PostAsJsonAsync but Works in Postman

I'm trying to create a POST call using HttpClient that I have fully working in Postman. Here is the body I'm sending in Postman:
{
"searchType": "games",
"searchTerms": [
"mario"
],
"searchPage": 1,
"size": 20,
"searchOptions": {
"games": {
"userId": 0,
"platform": "",
"sortCategory": "popular",
"rangeCategory": "main",
"rangeTime": {
"min": 0,
"max": 0
},
"gameplay": {
"perspective": "",
"flow": "",
"genre": ""
},
"modifier": ""
},
"users": {
"sortCategory": "postcount"
},
"filter": "",
"sort": 0,
"randomizer": 0
}
}
I have this written as the following in C#:
var client = _httpClientFactory.CreateClient(HttpClients.HowLongToBeat.ToString());
var request = new HowLongToBeatRequest
{
SearchType = "games",
SearchTerms = searchTerm.Trim().Split(" "),
SearchPage = 1,
Size = 20,
SearchOptions = new SearchOptions
{
Games = new SearchOptionsGames
{
UserId = 0,
Platform = "",
SortCategory = "popular",
RangeCategory = "main",
RangeTime = new SearchOptionsGamesRangeTime
{
Min = 0,
Max = 0
},
Gameplay = new SearchOptionsGamesGameplay
{
Perspective = "",
Flow = "",
Genre = ""
},
Modifier = ""
},
Users = new SearchOptionsUsers
{
SortCategory = "postcount"
},
Filter = "",
Sort = 0,
Randomizer = 0
}
};
//var json = JsonSerializer.Serialize(request);
//var content = new StringContent(json, Encoding.UTF8, "application/json");
//var response = await client.PostAsync("api/search", content);
var response = await client.PostAsJsonAsync("api/search", request, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
return new HowLongToBeatResponse();
I have this set up as
The url I'm hitting is: https://www.howlongtobeat.com/api/search and I'm setting it up like so in my Startup.cs
services.AddHttpClient(HttpClients.HowLongToBeat.ToString(), config =>
{
config.BaseAddress = new Uri("https://www.howlongtobeat.com/");
config.DefaultRequestHeaders.Add("Referer", "https://www.howlongtobeat.com/");
});
I am passing this Referer header in my Postman collection as well.
Basically, I can't figure out why this code gets a 403 in C# but the Postman that I think is exactly the same is getting a successful response. Am I missing something?
Let me know if there's any missing info I can provide.
I solved my problem. The issue was that this specific API required a User Agent header specified.
I think the problem is here, The BaseAddress property needs to be suffixed (https://www.howlongtobeat.com/) with a forward slash and here you already set route as well, change it to
services.AddHttpClient(HttpClients.HowLongToBeat.ToString(), config =>
{
config.BaseAddress = new Uri("https://www.howlongtobeat.com/");
config.DefaultRequestHeaders.Add("Referer", "https://www.howlongtobeat.com/api/search");
});
And then
var response = await client.PostAsJsonAsync("api/search", request, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
Updated:
try this, here I have hard-coded the Base URL for testing purposes.
try
{
var data_ = JsonConvert.SerializeObject(root);
var buffer_ = System.Text.Encoding.UTF8.GetBytes(data_);
var byteContent_ = new ByteArrayContent(buffer_);
byteContent_.Headers.ContentType = new MediaTypeHeaderValue("application/json");
string _urls = "https://www.howlongtobeat.com/api/search";
var responses_ = await _httpClient.PostAsJsonAsync(_urls, byteContent_);
if (responses_.StatusCode == HttpStatusCode.OK)
{
Console.WriteLine("[GetPrimeryAccount] Response: Success");
string body = await responses_.Content.ReadAsStringAsync();
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message); ;
}

Post array of products in shopify

Trying to post array of products using c# httpClinet to shopify.But getting 400 bad request. Also tried to post array of product = new object like items, but also getting 400 code.
var arrayOfProducts = new
{
products = new[]
{
new
{
title = "qqwqw",
description = "test",
product_type = "Game",
vendor = "test"
},
new
{
title = "1233eewq",
description = "test",
product_type = "Game",
vendor = "test"
}
}
};
var resp = await http.PostAsync($"{_shopify.Path}/products.json", new StringContent(
JsonSerializer.Serialize(arrayOfProducts)
, Encoding.UTF8, "application/json"), stoppingToken);

Create dynamic object from a JSON disregarding null fields

I need to consume a method that the input parameter is a dynamic object, but I feed the object through a received JSON
Dynamic Builder:
public static dynamic PayChargeObject()
{
var body = new
{
payment = new
{
banking_billet = new
{
customer = new
{
name = "",
email = "",
cpf = "",
birth = "",
phone_number = "",
address = new
{
street = "",
number = "",
neighborhood = "",
zipcode = "",
city = "",
complement = "",
state = "",
},
juridical_person = new
{
corporate_name = "",
cnpj = "",
}
},
expire_at = "",
discount = new
{
type = "",
value = 0
},
conditional_discount = new
{
type = "",
value = 0,
until_date = ""
},
message = ""
}
}
};
return body;
}
My JSON Data:
{
"payment": {
"banking_billet": {
"customer": {
"name": "person name",
"email": "personnamez#gerencianet.com.br",
"cpf": "94271564656",
"birth": "1977-01-15",
"phone_number": "41991234567"
},
"expire_at": "2019-12-12"
}
}
Note that json will not always have all available fields in the object filled, but the method I need to consume does not accept null values in the fields, and my problem is this, when deserializing JSON in the dynamic object, the fields not used in JSON are created as null in the dynamic object
JSON Convert and Method call:
var obj = JsonConvert.DeserializeAnonymousType(MyJSON, PayChargeObject())
dynamicMethod.PayCharge(obj);
Dynamic Object with null fields on Debug
How can I solve this problem?
With help of #dbc solution:
replace:
var obj = JsonConvert.DeserializeAnonymousType(MyJSON, PayChargeObject())
dynamicMethod.PayCharge(obj);
with:
dynamicMethod.PayCharge(JObject.Parse(MyJSON));

Converting JSON code of Facebook's Generic Template to C# in Bot framework

I am having a hard time converting this generic template of facebook to C#. I am not sure if i converted it right. Below is the code i tried but is not rendering on messenger. Thank you.
curl -X POST -H "Content-Type: application/json" -d '{
"recipient":{
"id":"<PSID>"
},
"message":{
"attachment":{
"type":"template",
"payload":{
"template_type":"generic",
"elements":[
{
"title":"Welcome!",
"image_url":"https://petersfancybrownhats.com/company_image.png",
"subtitle":"We have the right hat for everyone.",
"default_action": {
"type": "web_url",
"url": "https://petersfancybrownhats.com/view?item=103",
"webview_height_ratio": "tall",
},
"buttons":[
{
"type":"web_url",
"url":"https://petersfancybrownhats.com",
"title":"View Website"
},{
"type":"postback",
"title":"Start Chatting",
"payload":"DEVELOPER_DEFINED_PAYLOAD"
}
]
}
]
}
}
}
}' "https://graph.facebook.com/v2.6/me/messages?access_token=<PAGE_ACCESS_TOKEN>"
This is what i tried in c# but it is not working. I am not sure if i converted it the proper way. Any help would be appreciated thank you.
Activity previewReply = stepContext.Context.Activity.CreateReply();
previewReply.ChannelData = JObject.FromObject(
new
{
attachment = new
{
type = "template",
payload = new
{
template_type = "generic",
elements = new
{
title = "title",
subtitle = "subtitle",
image_url = "https://thechangreport.com/img/lightning.png",
buttons = new object[]
{
new
{
type = "element_share,",
share_contents = new
{
attachment = new
{
type = "template",
payload = new
{
template_type = "generic",
elements = new
{
title = "x",
subtitle = "xx",
image_url = "https://thechangreport.com/img/lightning.png",
default_action = new
{
type = "web_url",
url = "http://m.me/petershats?ref=invited_by_24601",
},
buttons = new
{
type = "web_url",
url = "http://m.me/petershats?ref=invited_by_24601",
title = "Take Quiz",
},
},
},
},
},
},
},
},
},
},
});
await stepContext.Context.SendActivityAsync(previewReply);
The elements and buttons attributes need to be lists. Take a look at the example template below.
var attachment = new
{
type = "template",
payload = new
{
template_type = "generic",
elements = new []
{
new {
title = "title",
image_url = "https://thechangreport.com/img/lightning.png",
subtitle = "subtitle",
buttons = new object[]
{
new {
type = "element_share",
share_contents = new {
attachment = new {
type = "template",
payload = new
{
template_type = "generic",
elements = new []
{
new {
title = "title 2",
image_url = "https://thechangreport.com/img/lightning.png",
subtitle = "subtitle 2",
buttons = new object[]
{
new {
type = "web_url",
url = "http://m.me/petershats?ref=invited_by_24601",
title = "Take Quiz"
},
},
},
},
},
}
},
},
},
},
},
},
};
reply.ChannelData = JObject.FromObject(new { attachment });
Note, you only need to add a share_contents element to your template if your main template is different from the template you are trying to share. Otherwise, your button can just be new { type = "element_share" }, which makes the template far less complex.
Also, be sure to Whitelist all of your URLs and make sure all of the image URLs work properly - a couple of them weren't working properly. The template won't render if the URLs aren't Whitelisted and image links are broken.
Hope this helps!

Converting JSON array to JSON Object

I am trying to convert a JSON array to an Object. This is a hacked way but good enough for my purpose.
Basically I am writing a method to get this
var data = [{
"MonthYearShortName": "Sep-13",
"TotalCustomers": 1905.0,
"Aquisition": 317.0,
"Attrition": 9.0
}, {
"MonthYearShortName": "FY-14",
"TotalCustomers": 2158.0,
"Aquisition": 401.0,
"Attrition": 15.0909090909091
}]
into something like this
data = [{
key: 'Attrition',
color: '#d62728',
values: [{
"label": "Sep-13",
"value": 9
}, {
"label": "FY-14",
"value": 15.0909090909091
}]
},
{
key: 'Total Customer',
color: '#1f77b4',
values: [{
"label": "Sep-13",
"value": 1905
}, {
"label": "FY-14",
"value": 2158
}]
},
{
key: 'Aquisition',
color: '#1f7774',
values: [{
"label": "Sep-13",
"value": 317
}, {
"label": "FY-14",
"value": 401
}]
}
];
The colors will be static for now. I will be handling it later.
Now getting to my hacked way of getting this (this is crude I know)
I tried something like this to just get the attrition out
var data = #"[{""MonthYearShortName"": ""Sep-13"",""TotalCustomers"": 1905.0,""Aquisition"": 317.0,""Attrition"": 9.0}, {""MonthYearShortName"": ""FY-14"",""TotalCustomers"": 2158.0,""Aquisition"": 401.0,""Attrition"": 15.0909090909091}]";
JArray a = JArray.Parse(data);
var label1 = a[0]["MonthYearShortName"].ToString();
var label2 = a[1]["MonthYearShortName"].ToString();
var totalCustomer1 = a[0]["TotalCustomers"].ToString();
var totalCustomer2 = a[1]["TotalCustomers"].ToString();
var aquisition1 = a[0]["Aquisition"].ToString();
var aquisition2 = a[1]["Aquisition"].ToString();
var attrition1 = a[0]["Attrition"].ToString();
var attrition2 = a[1]["Attrition"].ToString();
JObject rss1 =
new JObject(
new JProperty("channel",
new JObject(
new JProperty("Key", "Attrition"),
new JProperty("color", "#d62728"),
new JProperty("values",
new JArray(
from p in a
select new JObject(
new JProperty("label", a[0]["MonthYearShortName"].ToString()),
new JProperty("value", attrition1),
new JProperty("label", a[1]["MonthYearShortName"].ToString()),
new JProperty("value", attrition2)))))));
When I tried this I get a
Can not add property label to Newtonsoft.Json.Linq.JObject. Property with the same name already exists on object.
Now if someone can suggest a cleaner way (as I cant think of any right now), I would be greatful or if my code could be corrected, that would be helpful.
Thanks
Can you try doing it like this?
[Test]
public void Test()
{
var data = #"[
{'MonthYearShortName': 'Sep-13','TotalCustomers': 1905.0,'Aquisition': 317.0,'Attrition': 9.0},
{'MonthYearShortName': 'FY-14','TotalCustomers': 2158.0,'Aquisition': 401.0,'Attrition': 15.0909090909091}
]";
dynamic jarr = JArray.Parse(data);
var attritions = new List<ExpandoObject>();
var aquisitions = new List<ExpandoObject>();
var totalCustomers = new List<ExpandoObject>();
foreach (dynamic o in jarr)
{
dynamic attr = new ExpandoObject();
attr.label = o.MonthYearShortName;
attr.value = o.Attrition;
attritions.Add(attr);
dynamic acq = new ExpandoObject();
acq.label = o.MonthYearShortName;
acq.value = o.Aquisition;
aquisitions.Add(acq);
dynamic cust = new ExpandoObject();
cust.label = o.MonthYearShortName;
cust.value = o.TotalCustomers;
totalCustomers.Add(acq);
}
dynamic attrition = new ExpandoObject();
dynamic aquisition = new ExpandoObject();
dynamic totalCustomer = new ExpandoObject();
attrition.Key = "Attrition";
attrition.Color = "#d62728";
attrition.Values = attritions;
aquisition.Key = "Acquisition";
aquisition.Color = "#1f7774";
aquisition.Values = aquisitions;
totalCustomer.Key = "Total Customer";
totalCustomer.Color = "#1f77b4";
totalCustomer.Values = totalCustomers;
var result = new[] { attrition,totalCustomer, aquisition };
var resultString = JsonConvert.SerializeObject(result, Formatting.Indented);
Console.Write(resultString);
}

Categories