I am working on building an app that accepts orgunitid and creates a new section associated with orgunitid. I'm using c#.
Here is my code.
string orgUnitId = textBoxOrgUnitId.Text;
string sectionCreateRoute = "/d2l/api/lp/1.0/" + orgUnitId + "/sections/";
var client = new RestClient(host);
var valenceAuthenticator = new D2L.Extensibility.AuthSdk.Restsharp.ValenceAuthenticator(userContext);
var requestCreateSection = new RestRequest(sectionCreateRoute, Method.POST);
valenceAuthenticator.Authenticate(client, requestCreateSection);
And, the JSON data I should provide will look like this.
{
"Name": "Test Section",
"Code": "" ,
"Description": { "Content": "Test", "Type" : "HTML" }
}
How can I create a new section with this JSON data.
Thanks,
Phillip
I've tried this code, but it still does not create a section.
string orgUnitId = textBoxOrgUnitId.Text;
string sectionCreateRoute = "/d2l/api/lp/1.0/" + orgUnitId + "/sections/";
var client = new RestClient(host);
var valenceAuthenticator = new D2L.Extensibility.AuthSdk.Restsharp.ValenceAuthenticator(userContext);
var requestCreateSection = new RestRequest(sectionCreateRoute, Method.POST);
requestCreateSection.AddJsonBody(new
{
Name = "Section Test",
Code = "156156",
Description = new { Content = "Test", Type = "Html" }
});
valenceAuthenticator.Authenticate(client, requestCreateSection);
Because you're using ResSharp I would recommend calling the following method;
public IRestRequest AddJsonBody(object obj)
{
this.RequestFormat = DataFormat.Json;
return this.AddBody(obj, "");
}
However in order to use this you need a C# object to represent that data. The class definitions would look something like this;
public class NewSectionPostBody
{
public string Name;
public string Code;
public SectionContentType Description;
}
public class SectionContentType
{
public string Content;
public string Type;
}
With these and your existing code I can do the following;
var requestCreateSection = new RestRequest(sectionCreateRoute, Method.POST);
// use object initializer to make instance for body inline
requestCreateSection.AddJsonBody(new NewSectionPostBody{
Name="Test Section",
Code=String.Empty,
Description= new SectionContentType { Content="Test", Type="HTLM" }
});
valenceAuthenticator.Authenticate(client, requestCreateSection);
RestSharp handles the object to json string serialization so you can basically just pass any object into that method and the resulting json will be used as the post body.
One last thing; if this is a one off request you don't even need to define the types I used for the body. You can just use the same inline initilization constructs to make an anonymous type if you were to just remove the classnames ie;
requestCreateSection.AddJsonBody(new {
Name="Test Section",
Code=String.Empty,
Description= new { Content="Test", Type="HTLM" }
});
^^ Rather than instantiating user defined class types I use anonymous types. If you're not able to reuse the types which make up the post body, this is a more efficient way of setting up the request.