Answered

Using Content Import API in .Net to Save a HTML field of a Specific Content Item

Below is how to use the Content Import API in a .Net project in order to save a HTML field for a particular content item.

public JsonResult PostUpdatedHtmlContent(int contentId)
{
var defaultLanguageCode = "en-us";

// Gets the "myHtml" form input without validating it since it contains
// html and would otherwise get rejected by .Net.
var myHtml = Request.Unvalidated().Form["myHtml"];

// Get the original content item and all its fields.
var getContentItemRetStr = ServerAPI.GetContentItem(contentId, defaultLanguageCode);
var getContentItemsRetObj = JsonConvert.DeserializeObject<APIResult<dynamic>>(getContentItemRetStr);
var contentItem = getContentItemsRetObj.ResponseData;

// Update MyHtmlPropertyName with what the user sent to the server.
contentItem.MyHtmlPropertyName = myHtml;
var contentItemString = JsonConvert.SerializeObject(contentItem);
var saveContentItemRetStr = ServerAPI.SaveContentItem(contentId, "MyContentReferenceName",
defaultLanguageCode, contentItemString, string.Empty);

var saveContentItemRetObj = JsonConvert.DeserializeObject<APIResult<dynamic>>(saveContentItemRetStr);

return new JsonResult
{
JsonRequestBehavior = JsonRequestBehavior.AllowGet,
Data = saveContentItemRetObj
};
1

Comments

1 comment

Please sign in to leave a comment.

Didn't find what you were looking for?

New post