Created
November 19, 2021 09:45
-
-
Save skttl/072dcea196b61b3d295602d52b206e97 to your computer and use it in GitHub Desktop.
Force Umbraco to update media tracking references
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using System.Web.Http; | |
using Umbraco.Core.Models; | |
using Umbraco.Web.WebApi; | |
namespace My.Controllers | |
{ | |
public class ReferencesController : UmbracoApiController | |
{ | |
[HttpGet] | |
public string Update() | |
{ | |
var sb = new StringBuilder(); | |
var allContent = Services.ContentService.GetRootContent(); | |
foreach (var content in allContent) | |
{ | |
SaveOrPublish(content, sb); | |
} | |
return sb.ToString(); | |
} | |
private void SaveOrPublish(IContent content, StringBuilder sb) | |
{ | |
try | |
{ | |
if (content.Published) | |
{ | |
Services.ContentService.SaveAndPublish(content, "*", content.WriterId, true); | |
} | |
else | |
{ | |
Services.ContentService.Save(content, content.WriterId, true); | |
} | |
sb.AppendLine(content.Name); | |
} | |
catch (Exception ex) | |
{ | |
sb.AppendLine(content.Name + " FAILED"); | |
} | |
var children = Services.ContentService.GetPagedChildren(content.Id, 0, int.MaxValue, out long totalRecords); | |
foreach (var child in children) | |
{ | |
SaveOrPublish(child, sb); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment