Copied to clipboard

Flag this post as spam?

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


  • sam how 30 posts 130 karma points
    Dec 07, 2024 @ 16:52
    sam how
    0

    Umbraco CMS Data to javascript

    How can I pass data to my JavaScript? For example, I have a content/page with a blocklist, and inside the blocklist, I want to get the value of a data type "text" and pass it to my JavaScript. What is the most efficient way to do this?

  • sam how 30 posts 130 karma points
    Dec 10, 2024 @ 09:01
    sam how
    0

    I was able to fetch the blocklist properties inside my blocklist (using the controller i created) and fetch it using javascript but I was not able to get/show the values of the data types in my javascript

    public class TestingApiController: UmbracoApiController { private readonly IUmbracoContextAccessor _umbracoContextAccessor;

        public  TestingApiController(IUmbracoContextAccessor umbracoContextAccessor)
        {
            _umbracoContextAccessor = umbracoContextAccessor;
        }
    
        [HttpGet]
        public ActionResult<IEnumerable<string>> GetAll()
        {
    
            if (!_umbracoContextAccessor.TryGetUmbracoContext(out var context))
            {
                return Problem("Unable to get Umbraco context.");
            }
    
    
            if (context.Content == null)
            {
                return Problem("Content cache is null.");
            }
    
    
            var contentType = context.Content.GetContentType(ImageAndTextBlock.ModelTypeAlias);
    
            if (contentType == null)
            {
                return Problem($"Content type for '{ImageAndTextBlock.ModelTypeAlias}' not found.");
            }
    
    
            var blockListProperties = contentType.PropertyTypes
                .Select(pt => new
                {
                    PropertyName = pt.DataType,
                    DataTypeId = pt.DataType.Id,
                    DataTypeName = pt.DataType.Configuration.ToJson()
                });
    
            if (!blockListProperties.Any())
            {
                return Ok("No Block List properties found for the SsoPage content type.");
            }
    
            return Ok(blockListProperties);
    
    
        }
    }
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies