I have loads of old content with missing images. I would like to check if there is an image on each of them. If not, then i'll display a default image which sits on the ancestor page. Instead of adding images to each content manually, whats the best approach. I am trying it with xslt as follows.
public class DefaultImageController: UmbracoAuthorizedApiController //Only works if you are logged into the backend
{
public string SetDefaultImages()
{
var defaultImageId = 0;
var contentService = Services.ContentService;
var rootnodes = Umbraco.TypedContentAtRoot(); //Gets from the cache so fast to get all nodes
foreach (var node in rootnodes)
{
foreach (var descendandNode in node.Descendants().Where(n => n.HasProperty("nameOfImageProperty") && n.HasValue("nameOfImageProperty") == false)) //replace by node.Descendants("Doctypealias")... if you want to filter
{
var editNode = contentService.GetById(node.Id); //Gets from the db = slow, so only do this if you want to edit.
editNode.SetValue("nameOfImageProperty", defaultImageId);
contentService.SaveAndPublishWithStatus(editNode);
}
}
return "All nodes now have an image";
}
}
Adding default image to old content
I have loads of old content with missing images. I would like to check if there is an image on each of them. If not, then i'll display a default image which sits on the ancestor page. Instead of adding images to each content manually, whats the best approach. I am trying it with xslt as follows.
Use an UmbracoAuthorizedApiController and the ContentService
is working on a reply...