/// <summary> /// Attempts to add redirect information to the given content item. /// </summary> /// <param name="content">The content that is being moved.</param> /// <param name="contentService">The content service responsible for operations involving <see cref="IContent"/></param> /// <param name="alias">The property editor alias.</param> /// <param name="helper">The <see cref="UmbracoHelper"/> that provides access to the published content cache.</param> private void AddRedirect(IContent content, IContentService contentService, string alias, UmbracoHelper helper) { // TODO: Probably don't need the second check for recycle bin. if (content.HasPublishedVersion && content.Id != UmbracoConstants.System.RecycleBinContent) { // The old url still exists in the published content cache. IPublishedContent publishedVersion = helper.TypedContent(content.Id); string url = publishedVersion.UrlAbsolute(); PropertyCollection props = content.Properties; // Grab the first property that matches our property editor alias. if (props.Any(p => p.PropertyType.PropertyEditorAlias.Equals(alias))) { Property prop = props.First(p => p.PropertyType.PropertyEditorAlias.Equals(alias)); List <BloodhoundUrlRewrite> rewrites; try { rewrites = JsonConvert.DeserializeObject <List <BloodhoundUrlRewrite> >(prop.Value.ToString()); } catch { rewrites = new List <BloodhoundUrlRewrite>(); } // Add a new rewrite to track the change then save/publish. if (!rewrites.Any(r => r.RewriteUrl.InvariantEquals(url))) { rewrites.Add(new BloodhoundUrlRewrite { RewriteUrl = url }); } content.SetValue(prop.Alias, JsonConvert.SerializeObject(rewrites.Distinct())); Attempt <PublishStatus> attempt = contentService.SaveAndPublishWithStatus(content); if (attempt.Exception != null) { LogHelper.Error <BloodhoundApplicationEvents>( $"Unable to update redirect url for content node with id {content.Id}", attempt.Exception); } // Recursively check for any children IEnumerable <IContent> children = content.Children().ToList(); if (children.Any()) { foreach (IContent child in children) { this.AddRedirect(child, contentService, alias, helper); } } } } }