Skip to content

Instantly share code, notes, and snippets.

@sitereactor
Created February 25, 2016 09:35
Show Gist options
  • Select an option

  • Save sitereactor/32515b8eccb98f4a0e14 to your computer and use it in GitHub Desktop.

Select an option

Save sitereactor/32515b8eccb98f4a0e14 to your computer and use it in GitHub Desktop.

Revisions

  1. sitereactor created this gist Feb 25, 2016.
    40 changes: 40 additions & 0 deletions BulkController.cs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    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);
    }
    }
    }