Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Edux 2 posts 72 karma points
    Feb 07, 2024 @ 14:40
    Edux
    0

    Updating Existing Documents by Using UmbracoAuthorizedApiController

    How to update properties of existing documents using a Controller. Context: I have a container page that has hunderts of news documents and each of them has many fields, s.a title, body etc. This documents have also SEO related information s.a metatitle and metadescription. I want to create a controller that checks if this fields are empty and in case to fill them with existing fields such as title and body. Follwoing the code of the controller: There is a HTTP requesto to SEO Controller and private methods. FillEmptyFields that should check and fill empty fields and another helper that creates a string. The problem is on FillEmptyFields that does not set the new values and also save them.

    >  public class SEOController : UmbracoAuthorizedApiController  {
    >      private readonly IContentService _contentService;
    >      private readonly ILogger<SEOController> _logger;
    >      private readonly HttpClient _httpClient;
    >      private readonly IShortStringHelper _shortStringHelper;
    >      private readonly IContentTypeBaseServiceProvider _contentTypeBaseServiceProvider;
    >      private readonly IUmbracoContextAccessor _accessor;
    >      private int index = 150;
    > 
    >      public SEOController(
    >          IContentService contentService,
    >          IShortStringHelper shortStringHelper,
    >          IContentTypeBaseServiceProvider contentTypeBaseServiceProvider,
    >          IUmbracoContextAccessor accessor,
    >          ILogger<SEOController> logger)
    >      {
    >          _logger = logger;
    >          _contentService = contentService;
    >          _shortStringHelper = shortStringHelper;
    >          _contentTypeBaseServiceProvider = contentTypeBaseServiceProvider;
    >          _accessor = accessor;
    > 
    >          _httpClient = new HttpClient();
    >      }
    > 
    >      // ~/umbraco/backoffice/api/seo/news
    >      public void News()
    >      {
    >          _accessor.TryGetUmbracoContext(out var context);
    >          IPublishedContent? root = context?.Content?
    >              .GetAtRoot()?.First()?
    >              .Children(x => x.ContentType.Alias == "knowledgeEvents")?.First()?
    >              .Children(x => x.ContentType.Alias == "newsEvents")?
    >              .Where(x => x.Name == "News & Events").First();
    > 
    >          if (root != null)
    >          {
    >              FillEmptyFields(root.Children());
    >          }
    >      }
    >     private void FillEmptyFields(IEnumerable<IPublishedContent> childrens)
    >     {
    > 
    >         if (childrens != null)
    >         {
    >             foreach (var child in childrens)
    >             {
    >                 var itemContent = _contentService.GetById(child.Id);
    >                 var singleNewsEventsTitle = child.HasValue("singleNewsEventsTitle") ?
    > child.Value<string>("singleNewsEventsTitle") : child.Name;
    >                 var body = CreateMetaDescription(child.Value<string>("singleKnowledgeEventsBody"));
    >                 var description = CreateMetaDescription(body);
    > 
    >                 if (string.IsNullOrEmpty(child.Value<string>("title")))
    >                 {
    >                     itemContent.SetValue("title", singleNewsEventsTitle);
    >                 }
    > 
    >                 if (string.IsNullOrEmpty(child.Value<string>("metaDescription")))
    >                 {
    >                     itemContent.SetValue("metaDescription", description);
    >                 }
    >                 if (itemContent != null) _contentService.SaveAndPublish(itemContent);
    >             }
    >         }
    >     }
    > 
    >     private string CreateMetaDescription(string? body)
    >     {
    >         string? result = string.Empty;
    > 
    >         result = (body != null && body.Length >= index) ? string.Concat(body.Substring(index), "...") : body;
    > 
    >         if (result != null)
    >         {
    >             return result;
    >         }
    >         else
    >         {
    >             return string.Empty;
    >         }
    >     } }
    

    Here is the error i see:

    {"ExceptionMessage":"Variation \"<null>,<null>\" is not supported by the property type.","ExceptionType":null,"StackTrace":null}
    

    Any suggestions?!

    Thank You

  • Alex Skrypnyk 6175 posts 24186 karma points MVP 8x admin c-trib
    Feb 07, 2024 @ 17:18
    Alex Skrypnyk
    1

    Hi Edux

    Can you check that docs have these properties?

    if (string.IsNullOrEmpty(child.Value

                {
                    itemContent.SetValue("title", singleNewsEventsTitle);
                }
    
                if (string.IsNullOrEmpty(child.Value<string>("metaDescription")))
                {
                    itemContent.SetValue("metaDescription", description);
                }
    
Please Sign in or register to post replies

Write your reply to:

Draft