Skip to content

Instantly share code, notes, and snippets.

@sitereactor
Created February 25, 2016 09:35
Show Gist options
  • Save sitereactor/32515b8eccb98f4a0e14 to your computer and use it in GitHub Desktop.
Save sitereactor/32515b8eccb98f4a0e14 to your computer and use it in GitHub Desktop.
Example of an Umbraco Backoffice controller used to do a bulk upgrade of certain content items
using System.Net;
using System.Net.Http;
using Umbraco.Web.WebApi;
namespace Example.Controllers
{
/// <summary>
/// Protectec Backoffice controller - Users must be logged in to call this
/// Url: /umbraco/backoffice/api/bulk/PostPerformBulkUpdate
/// </summary>
public class BulkController : UmbracoAuthorizedApiController
{
//Add a model to pass in details like ContentType alias, PropertyType alias
//and value to update
public HttpResponseMessage PostPerformBulkUpdate()
{
//Find a ContentType with a specific alias
var contentType = this.Services.ContentTypeService.GetContentType("myAlias");
//Here we get all content items based on the above ContentType
var contentService = this.Services.ContentService;
var contentOfType = contentService.GetContentOfContentType(contentType.Id);
//If you know the Ids of the content items
//var contentItems = contentService.GetByIds(new List<int> {1241, 1242, 1243});
//Iterate the content items based on the "myAlias" ContentType
foreach (var content in contentOfType)
{
//Update the value of a specific property
//(a check could have been done before this to ensure that the property exists and previous value was "X")
content.Properties["myPropertyAlias"].Value = "a different value then before";
//Save and publish the updated content item
var status = contentService.SaveAndPublishWithStatus(content);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment