Skip to content

Instantly share code, notes, and snippets.

@ordepdev
Created September 14, 2015 15:04
Show Gist options
  • Save ordepdev/39209ae80fdb6e8e265e to your computer and use it in GitHub Desktop.
Save ordepdev/39209ae80fdb6e8e265e to your computer and use it in GitHub Desktop.
Umbraco.Core.Services.ContentService example
using System.Web.Mvc;
using Umbraco.Core;
using Umbraco.Core.Services;
using Umbraco.Web;
namespace ContentServiceExample
{
public class ContentServiceExampleController : Controller
{
private IContentService _contentService;
public ContentServiceExampleController(IContentService contentService)
{
_contentService = contentService ?? ApplicationContext.Current.Services.ContentService;
}
[HttpPost]
public ActionResult GetAndUpdate(int id)
{
var content = _contentService.GetById(id);
int contentValue = (int)content.Properties["MyProperty"].Value;
contentValue.Properties["MyProperty"].Value = contentValue + 1;
var status = _contentService.SaveAndPublishWithStatus(votingItem);
if (status.Success) return new HttpStatusCodeResult(201);
else return new HttpStatusCodeResult(403);
}
[HttpPost]
public ActionResult Create(string name)
{
// this is the "parent" node where we'll create the new node
// it can be GetRootContent() or GetById(someRootId)
var root = contentService.GetRootContent().FirstOrDefault();
var content = _contentService.CreateContent("Record Name", root, "Record Document Type");
content.Properties["MyProperty"].Value = 10;
var status = _contentService.SaveAndPublishWithStatus(votingItem);
if (status.Success) return new HttpStatusCodeResult(201);
else return new HttpStatusCodeResult(403);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment