Copied to clipboard

Flag this post as spam?

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


  • Jonathan Roberts 409 posts 1063 karma points
    Jul 19, 2019 @ 13:18
    Jonathan Roberts
    0

    UmbracoAuthorizedJsonController not working in V8

    Does anyone know how to get this bit of V7 code to work in Version 8?

     [PluginController("DataTypeApi")]
    public class DataTypeSettingsController : UmbracoAuthorizedJsonController
    {
    
            [HttpGet]
        public long? GetFacebookAppId(int pageId)
        {
            var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
            IPublishedContent currentPage = umbracoHelper.TypedContent(pageId);
            IPublishedContent homePage = currentPage.Ancestors().Where(q => q.DocumentTypeAlias == "dtHome").FirstOrDefault();
            IPublishedContent siteSettings = homePage.Children.Where(q => q.DocumentTypeAlias == "dtSiteSettings").FirstOrDefault();
    
            if (siteSettings != null)
            {
                return siteSettings.GetPropertyValue<long?>("ssAppId").HasValue ? siteSettings.GetPropertyValue<long?>("ssAppId").Value : 0;
            }
    
            return 0;
        }
    
            [HttpGet]
        public object GetDataTypeById(int id)
        {
            var dtd = Services.DataTypeService.GetDataTypeDefinitionById(id);
            return this.FormatDataType(dtd);
        }
    
            [HttpGet]
        public object GetDataTypeByAlias(string alias)
        {
            string correctName = alias.Replace("_", " ");
            var dtd = Services.DataTypeService.GetDataTypeDefinitionByName(correctName);
            return this.FormatDataType(dtd);
        }
    
     protected object FormatDataType(IDataTypeDefinition dtd)
        {
            if (dtd == null)
            {
                throw new HttpResponseException(HttpStatusCode.GatewayTimeout);
            }
    
            var propEditor = PropertyEditorResolver.Current.GetByAlias(dtd.PropertyEditorAlias);
    
            // Force converter before passing prevalues to view
            var preValues = Services.DataTypeService.GetPreValuesCollectionByDataTypeId(dtd.Id);
            var convertedPreValues = propEditor.PreValueEditor.ConvertDbToEditor(propEditor.DefaultPreValues, preValues);
    
            return new
            {
                Guid = dtd.Key,
                PropertyEditorAlias = dtd.PropertyEditorAlias,
                PreValues = convertedPreValues,
                View = propEditor.ValueEditor.View
            };
        }
    }
    
  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Jul 21, 2019 @ 08:44
    Marc Goodson
    101

    Hi Jonathan

    Lots of little changes between V7 and V8 in terms of syntax, I'm not fully sure of the context of what you are trying to achieve, and there are big changes to how PreValue configuration is handled in V8, but the following is the output of taking what you had and fixing it up to build in V8, with notes about the PreValues thing...

    using System.Linq;
    using System.Net;
    using System.Web.Http;
    using Umbraco.Core.Models;
    using Umbraco.Core.Models.PublishedContent;
    using Umbraco.Core.PropertyEditors;
    using Umbraco.Web;
    using Umbraco.Web.Editors;
    using Umbraco.Web.Mvc;
    
    namespace umbraco_8_1_release.Controllers
    {
        [PluginController("DataTypeApi")]
        public class DataTypeSettingsController : UmbracoAuthorizedJsonController
        {
    
            [HttpGet]
            public long? GetFacebookAppId(int pageId)
            {
                IPublishedContent currentPage = Umbraco.Content(pageId);
                IPublishedContent homePage = currentPage.Ancestors().Where(q => q.ContentType.Alias == "dtHome").FirstOrDefault();
                IPublishedContent siteSettings = homePage.Children.Where(q => q.ContentType.Alias == "dtSiteSettings").FirstOrDefault();
    
                if (siteSettings != null)
                {
                    return siteSettings.HasValue("ssAppId") ? siteSettings.Value<long?>("ssAppId").Value : 0;
                }
    
                return 0;
            }
    
            [HttpGet]
            public object GetDataTypeById(int id)
            {
                var dtd = Services.DataTypeService.GetDataType(id);
                return this.FormatDataType(dtd);
            }
    
            [HttpGet]
            public object GetDataTypeByAlias(string alias)
            {
                string correctName = alias.Replace("_", " ");
                var dtd = Services.DataTypeService.GetDataType(correctName);
                return this.FormatDataType(dtd);
            }
    
            protected object FormatDataType(IDataType dtd)
            {
                if (dtd == null)
                {
                    throw new HttpResponseException(HttpStatusCode.GatewayTimeout);
                }
               var propertyEditorAlias =  dtd.Editor.Alias;
    
    
                //PropertyEditorResolver has been removed
                // var propEditor = PropertyEditorResolver.Current.GetByAlias(dtd.Editor.Alias);
                //var preValues = Services.DataTypeService.GetPreValuesCollectionByDataTypeId(dtd.Id);
                // var convertedPreValues = propEditor.PreValueEditor.ConvertDbToEditor(propEditor.DefaultPreValues, preValues);
    
                // in the Umbraco source it seems in PropertyValueConverters that there is a strongly typed object for each editor's configuraiton
                // that gives you access to PreValues
                //for example for Slider property editor:
                var configuration = dtd.ConfigurationAs<SliderConfiguration>();
                configuration.InitialValue = 12.5M;
                //although there is also a DefaultConfiguration here:
                var defaultConfig = dtd.Editor.DefaultConfiguration;
                //there is a method to get the ValueEditor and therefore the 'view'
                var valueEditor = dtd.Editor.GetValueEditor();
    
    
                return new
                {
                    Guid = dtd.Key,
                    PropertyEditorAlias = dtd.Editor.Alias,
                    // not sure what you want to return here?
                    //PreValues = convertedPreValues,
                    View = valueEditor.View
                };
            }
        }
    }
    

    ... but caveated with the fact I've never had to do anything like this! - but hopefully this steers you on your way, on getting the UmbracoAuthorizedJsonController to work!

    regards

    Marc

  • Jonathan Roberts 409 posts 1063 karma points
    Jul 22, 2019 @ 09:47
    Jonathan Roberts
    0

    Hi, This is great - thank you so much for all your help,

    Jon

  • Stefano Beretta 102 posts 247 karma points
    Feb 03, 2022 @ 13:44
    Stefano Beretta
    0

    Hi!

    By any chance have you discovered how to get the prevalues?

    Thank you

    S

Please Sign in or register to post replies

Write your reply to:

Draft