I am trying to post a new item creation to a test store via C#, but I'm not sure how the syntax should read. Square Connect API requires at least one variation for new item creation, but I'm not sure how to add that to the JSON body. Here is what I have, but I'm not sure how to complete it.
var client = new RestSharp.RestClient();
var post = new RestRequest("https://connect.squareup.com/v1/me/items", Method.POST);
post.RequestFormat = DataFormat.Json;
post.AddHeader("Authorization", String.Format("Bearer {0}", testtoken));
post.AddBody(new { name = testname, variations = ???? });
This code works, but returns a response of an item must include at least one variation. I realize that, but do not know how to write it, or if it is even possible.
I am not opposed to going a different route.
Edited to add a sample request body from the Square documentation:
{
"name": "Milkshake",
"description": "It's better than yours",
"visibility": "PRIVATE",
"category_id": "36ac7016-3a4e-4934-81f1-9057ac613f2y",
"variations": [
{
"name": "Small",
"pricing_type": "FIXED_PRICING",
"price_money": {
"currency_code": "USD",
"amount": 400
},
"sku": "123"
}
]
}
Something like this should serialize to JSON in the correct format:
post.AddBody(new {
name = testname,
variations = new object[] {
new {
name = "Small",
pricing_type = "FIXED_PRICING",
price_money = new {
currency_code = "USD",
amount = 400
}
}
},
sku = "123"
});
Related
Example JSON, for example say I want the quote and author values. I wasn't able to get them unless I built a model around the JSON, which I'm not wanted to do as it would be more time consuming.
{
"success": {
"total": 1
},
"contents": {
"quotes": [
{
"quote": "Plant your own garden and decorate your own soul, instead of waiting for someone to bring you flowers.",
"length": "102",
"author": "Veronica A. Shoffstall",
"tags": [
"flowers",
"inspire",
"self-help",
"soul"
],
"category": "inspire",
"language": "en",
"date": "2022-12-22",
"permalink": "https://theysaidso.com/quote/veronica-a-shoffstall-plant-your-own-garden-and-decorate-your-own-soul-instead-o",
"id": "LQbKQGxVA2rcH4lIwn6OIweF",
"background": "https://theysaidso.com/img/qod/qod-inspire.jpg",
"title": "Inspiring Quote of the day"
}
]
},
"baseurl": "https://theysaidso.com",
"copyright": {
"year": 2024,
"url": "https://theysaidso.com"
}
}
My test example code with URL below. I tried it with Dynamic Object but can never get to the string.
try
{
private static readonly HttpClient _httpClient = new HttpClient();
// Make the API request
var response = _httpClient.GetAsync("https://quotes.rest/qod?language=en").Result;
response.EnsureSuccessStatusCode();
// Do something with the response
var value11 = response.Content.ReadAsStringAsync().Result;
var gett = JsonConvert.DeserializeObject<dynamic>(value11);
var quote= gett.contents.quotes.quote;
return quote;
}
catch (Exception ex)
{
quote = ex.Message;
return quote;
}
Looking at the JSON structure the quotes property is an array and as such you should use
var quote = gett.contents.quotes[0].quote;
Assuming that deserialization was not the cause of the failure.
There is no json convert error. quotes is array you can access gett .contents.quotes[0].quote.
Tips; you don't need to json convert and ReadAsString you can easily use like response.Content.ReadFromJsonAsync().Result?.contents.quotes[0].quote
How to dynamically change my custom adaptive card's text property value from within my C# code?
Here is my C# code
public static Attachment CreateMySearchCardAttachment()
{
// combine path for cross platform support
string[] paths = { ".", "Resources", "MySearchCard.json" };
var MySearchCardJson = File.ReadAllText(Path.Combine(paths));
var adaptiveCardAttachment = new Attachment()
{
ContentType = "application/vnd.microsoft.card.adaptive",
Content = JsonConvert.DeserializeObject(MySearchCardJson),
};
return adaptiveCardAttachment;
}
And my MySearchCard.json file goes below
{
"type": "AdaptiveCard",
"body": [
{
"type": "ColumnSet",
"columns": [
{
"type": "Column",
"items": [
{
"type": "Image",
"horizontalAlignment": "Right",
"spacing": "None",
"url": "",
"size": "Medium",
"width": "2px",
"height": "2px"
},
{
"type": "TextBlock",
"size": "Medium",
"weight": "Bolder",
"text": "Knowledgebase Search"
},
{
"type": "Input.Text",
"id": "searchText",
"placeholder": "Type your search text and click Search"
}
],
"width": 2
}
]
}
],
"actions": [
{
"type": "Action.Submit",
"title": "Search"
}
],
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"version": "1.0"
}
I am able to get this adaptive card to display inside my chat bot.But not sure how to dynamically change the text labels or their values. We want to dynamically change some of the text labels before displaying, then after displaying,dynamically show or hide based on user response.At a later point we want to integrate chat BoT with MS Teams.But prior to that I need to show the same from my emulator
If you look at my json, there is a text property with "text": "Knowledgebase Search". My question is how to change this text value dynamically from within my C# code?
Dynamically Changing Before Being Displayed
There's a few different ways to do this. The first option is probably the best, but they should all work.
1. Use the AdaptiveCards Package
Note: This package is different from, and newer than, Microsoft.AdaptiveCards -- Don't use this one
Since you know the exact part of the card that you'd like to change, you can:
string[] paths = { ".", "AdaptiveCard.json" };
var cardJson = File.ReadAllText(Path.Combine(paths));
var card = AdaptiveCard.FromJson(cardJson).Card;
var columnSet = (card.Body[0] as AdaptiveColumnSet);
var column = (columnSet.Columns[0] as AdaptiveColumn);
var knowledgeBlock = (column.Items[1] as AdaptiveTextBlock);
knowledgeBlock.Text = "Whatever You Want";
var attachment = new Attachment()
{
Content = card,
ContentType = "application/vnd.microsoft.card.adaptive"
};
var reply = stepContext.Context.Activity.CreateReply();
reply.Attachments = new List<Attachment>();
reply.Attachments.Add(attachment);
await stepContext.Context.SendActivityAsync(reply);
Result:
2. Use Data Binding (in Preview)
This is in preview and you still need to use the NuGet package from #1, but makes it easier to modify particular fields.
3. Edit the JSON with Newtonsoft.JSON
This is probably a little simpler, but less flexible. Something like this works and produces the same result as #1:
string[] paths = { ".", "AdaptiveCard.json" };
var cardJsonObject = JObject.Parse(File.ReadAllText(Path.Combine(paths)));
var knowledgeToken = cardJsonObject.SelectToken("body[0].columns[0].items[1]");
knowledgeToken["text"] = "Whatever You Want";
var attachment = new Attachment()
{
Content = cardJsonObject,
ContentType = "application/vnd.microsoft.card.adaptive"
};
var reply = stepContext.Context.Activity.CreateReply();
reply.Attachments = new List<Attachment>();
reply.Attachments.Add(attachment);
await stepContext.Context.SendActivityAsync(reply);
return await stepContext.NextAsync();
Dynamically Changing After Being Displayed
Changing the card after being displayed is a little more difficult. You first have to change the card in the code, as done above. You then have to use UpdateActivityAsync(). Basically, you send an activity with the same id, but a new card and it overwrites the previous card completely.
Note: You can only use this in channels that support updating activities. It's usually pretty easy to tell, because even without bots, the channel either will or won't let you edit messages. It sounds like you want to use Teams, so this will work fine.
You can use my answer here for how to update card activities with Teams. Note that this one is in Node, but you can still do it in C# the same way.
You can also use this other StackOverflow Answer from one of the guys on the MS Teams team.
I want to reuse a DocuSign template for multiple recipients in the same envelope. The template is rather simple; it has a few signing and date signed blocks.
The recipients will have different routing orders. From what I've seen, my API request needs to have the Routing Order and Role Name match the DocuSign template. If the role name and routing order don't match, I end up with an empty role (which has all of the signing blocks) on the envelope. I've also tried "merge roles on draft." The empty role will be merged into the second recipient, but I lose all of the template's signing blocks for that recipient.
Is there a way to use a template but modify the definition of the template recipient role? Ideally, I'd like to use the exact same template but change the routing order of that role for the second recipient. I'd like to avoid creating new templates in DocuSign since I could end up with many combinations.
I'd like to accomplish (UPDATE):
I want to use the same template two times in a single envelope. Each recipient will be assigned to an individual copy of the template. The final envelope should have two recipients, two documents, and each recipient will only have access and visibility to their document.
The issue is that the template's role defines the routing order. The routing order of "1" is applicable for the first recipient using the template, but the second recipient needs a routing order of "2." (The template's role expects a routing order of "1" in all cases, but I want that value to be a "2" for the second recipient.)
Example Template Information:
Template Name (for example purposes): Test Template #1
Role Name: Applicant 1
Routing Order: 1 (if I don't define the routing order, DocuSign treats it as a "1" anyways)
Example Request:
EnvelopeDefinition envDef = new EnvelopeDefinition();
var signer1 = new Signer()
{
RecipientId = "1",
Name = "First User 1",
RoleName = "Applicant 1",
Email = "fakeemail1#email.com",
RoutingOrder = "1"
};
var signer2 = new Signer()
{
RecipientId = "2",
Name = "First User 2",
RoleName = "Applicant 1",
Email = "fakeemail2#email.com",
RoutingOrder = "2"
};
envDef.CompositeTemplates = new List<CompositeTemplate>();
var composite1 = new CompositeTemplate()
{
ServerTemplates = new List<ServerTemplate>()
{
new ServerTemplate("1", "Test Template #1 TEMPLATE_ID_GUID_HERE")
},
InlineTemplates = new List<InlineTemplate>()
{
new InlineTemplate()
{
Sequence = "1",
Recipients = new Recipients()
{
Signers = new List<Signer>()
{
signer1
}
}
}
}
};
var composite2 = new CompositeTemplate()
{
ServerTemplates = new List<ServerTemplate>()
{
new ServerTemplate("2", "Test Template #1 TEMPLATE_ID_GUID_HERE")
},
InlineTemplates = new List<InlineTemplate>()
{
new InlineTemplate()
{
Sequence = "2",
Recipients = new Recipients()
{
Signers = new List<Signer>()
{
signer2
}
}
}
}
};
envDef.CompositeTemplates.Add(composite1);
envDef.CompositeTemplates.Add(composite2);
envDef.EnforceSignerVisibility = "true";
// Code to send envelope
Note: Also, I'm using composite templates since our envelopes will likely have various combinations of templates and uploaded documents.
Thank you!
This can be achieved by passing a query parameter - change_routing_order=true while creating an envelope. So endpoint for creating envelope will be
https://{{EnvironmentVal}}/restapi/v2/accounts/{{AccountIdVal}}/envelopes?change_routing_order=true
and body of the request will be
Req Body:
where same templateId - 076d9062-cfc7-408b-a47f-88c4b74af62b is used with same RoleName but diff routing order and diff Signer details
{
"compositeTemplates": [
{
"inlineTemplates": [
{
"recipients": {
"signers": [
{
"email": "email+internal#gmail.com",
"name": "John Doe",
"recipientId": "1",
"roleName": "Signer1",
"routingOrder": "1"
}
]
},
"sequence": "2"
}
],
"serverTemplates": [
{
"sequence": "1",
"templateId": "076d9062-cfc7-408b-a47f-88c4b74af62b"
}
]
},
{
"inlineTemplates": [
{
"recipients": {
"signers": [
{
"email": "email+internal2#gmail.com",
"name": "John Doe2",
"recipientId": "2",
"roleName": "Signer1",
"routingOrder": "2"
}
]
},
"sequence": "2"
}
],
"serverTemplates": [
{
"sequence": "1",
"templateId": "076d9062-cfc7-408b-a47f-88c4b74af62b"
}
]
}
],
"status": "sent"
}
I am unable to get word substitution to work consistently with sendgrid v3 api in c#. Sometimes the tags will be substituted, other times they will not. I am at a loss as to what causes this. Can anyone see any obvious errors in my code?
String apiKey = "KEY";
dynamic sg = new SendGridAPIClient(apiKey);
Email from = new Email("info#example.com");
String subject = "Hello World from the SendGrid CSharp Library";
Email to = new Email("example#gmail.com");
Content content = new Content("text/html", " ");
to.Name = "Joe";
Mail mail = new Mail(from, subject, to, content);
mail.TemplateId = "dfea45f3-d608-4860-9f38-c7d444qwrqwc1f";
Personalization subs = new Personalization();
subs.AddTo(to);
subs.AddSubstitution("*|url|*", "http://asdasdasd.com");
subs.AddSubstitution("*|username|*", "MrUsername");
mail.AddPersonalization(subs);
dynamic response = sg.client.mail.send.post(requestBody: mail.Get());
Remove the Personalization and add the following
mail.Personalization[0].AddSubstitution("*|url|*", "http://asdasdasd.com");
mail.Personalization[0].AddSubstitution("*|username|*", "MrUsername");
or see code samples here
I have found the issue my side. I think yours is the same. When you look at mail object before sending you will find there are 2 personification items in the array. You do this subs.AddTo(to); then later on mail.AddPersonalization(subs); This creates 2 email to in the personalization array - my example of the wrong payload was:
{
"from": {
"email": "no-reply#chromasports.com"
},
"subject": "",
"personalizations":
[
{
"to": [{
"email": "email#gmail.com"
}]
},
{
"to": [{
"email": "email#gmail.com"
}],
"substitutions": {
":token": "http://alabala.com/auth/reset-password#token=or514rqHTeLjtjlN6WRppOu53yJJ64nSzcK86GF6Ite2BaZRa58YPMfTmM0wzQs4tMLbHy8YlpieDVBae1aD99TKnMh7wYNOE2nmu8gWePQoZiWhbFLomVBvApHA1fuxIxQ1elui2QXAmGPtwDdvVOgvAiSF3HQteuvFwP5kXUnXXEeddYLIUHqJCDrATiOsSgxvcpKmhXhrhx78ns49f4hakGlLMncNgBuMGmL3wCduY9f22hjCs9tbIPq5h5V"
}
}
],
"content": [{
"type": "text/html",
"value": "\u003chtml\u003e\u003cbody\u003eHTML content\u003c/body\u003e\u003c/html\u003e"
}],
"template_id": "unique id"
}
'
Check your payload and to fix it try mail.AddPersonalization[0] = subs; Hope this solves your issue
I am planning to write a simple SugarCrm .Net client.
Could anyone give me a pointer to a .Net wrapper/library for the SugarCrm REST api?
//lasse
Realize this is old, but in case anyone stumbles across this looking for a .NET wrapper for newer version of the web service (SugarCRM > 6.7), here is a wrapper I am currently building https://github.com/dlively1/SugarSharp
There is a wrapper that uses SOAP called CandyWrapper, which is a bit old but could help you with a starting point on building your own.
http://developers.sugarcrm.com/wordpress/2011/08/10/web-services-in-your-own-language-part-6-candywrapper-for-net/
I also realize this is old, but for SugarCRM CE 6.x I created SugarRestSharp. The sample I give below is in json, but the request would be in C# models. This implements get_entry_list SugarCrm Rest API method.
Passing request to RestSharp:
dynamic data = new
{
session = sessionId,
module_name = moduleName,
query = queryString,
order_by = string.Empty,
offset = 0,
select_fields = selectFields,
link_name_to_fields_array = string.Empty,
max_results = maxCountResult,
deleted = 0,
favorites = false
};
var client = new RestClient(url);
var request = new RestRequest(string.Empty, Method.POST);
string jsonData = JsonConvert.SerializeObject(data);
request.AddParameter("method", "get_entry_list");
request.AddParameter("input_type", "json");
request.AddParameter("response_type", "json");
request.AddParameter("rest_data", jsonData);
var response = client.Execute(request);
Request in json
{
"session": "olgg6hf5sqi6hk9u3tgpafbn66",
"module_name": "Accounts",
"query": "",
"order_by": "",
"offset": 0,
"select_fields": [
"name",
"industry",
"website",
"shipping_address_city",
"id"
],
"link_name_to_fields_array": "",
"max_results": 10,
"deleted": 0,
"favorites": false
}
Response in json
{
"result_count": 10,
"total_count": "58",
"next_offset": 10,
"entry_list": [
{
"id": "1e0eec64-8cc6-58ff-57f1-58533731b145",
"module_name": "Accounts",
"name_value_list": {
"name": {
"name": "name",
"value": "New SugarRestSharp Acccount 1 10397"
},
"industry": {
"name": "industry",
"value": "Manufacturing"
},
"website": {
"name": "website",
"value": "www.sugarrestsharp1.com"
},
"shipping_address_city": {
"name": "shipping_address_city",
"value": "Los Angeles"
},
"id": {
"name": "id",
"value": "1e0eec64-8cc6-58ff-57f1-58533731b145"
}
}
},
......... (other 9 items truncated)
],
"relationship_list": [
]
}
For more implementation and wiki check SugarRestSharp: https://github.com/mattkol/SugarRestSharp