Copied to clipboard

Flag this post as spam?

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


  • James Jackson-South 489 posts 1747 karma points c-trib
    Jun 11, 2013 @ 11:02
    James Jackson-South
    0

    DataType Example code please.

    Can someone please supply me with example code for generating a custom DataType. At present whenever the project is rebuilt, my custom dropdown is getting recreated which causes the prevalue "Value" property to be increased by 1 rendering it absolutely useless. This is a showstopper.

    I am using the 3.0 source from Codeplex with Umbraco 6.1

    Thanks.

        /// <summary>
        /// Defines the carousel mode data type.
        /// </summary>
        [DataType(Name = "Carousel Mode", UniqueId = "A0C2ABA7-EAAC-4FED-B8B1-F2FD073E4439", RenderControlGuid = "A74EA9C9-8E18-4D2A-8CF6-73C6206C5DA6", DatabaseDataType = DBTypes.Integer)]
        public class CarouselMode : DataTypeBase
        {
            /// <summary>
            /// Gets the pre-values for the carousel mode data type.
            /// </summary>
            /// <value>
            /// The pre-values for the carousel mode data type.
            /// </value>
            public override DataTypePrevalue[] Prevalues
            {
                get
                {
                    return new[]
                    {
                        new DataTypePrevalue("Fade", "Fade"),
                        new DataTypePrevalue("Slide", "Slide")
                    };
                }
            }
        }
  • Stephen 47 posts 270 karma points
    Jun 16, 2013 @ 22:20
    Stephen
    0

    Yeah it changes the prevalues every time it's recreated. Andy said you might have a fix for this?

    Otherwise you could work around it by using a Dropdown List, publishing keys (I think that's the one that returns the key "Fade" instead of the id) - use 

    RenderControlGuid="A74EA9E1-8E18-4D2A-8CF6-73C6206C5DA6"

    (that's slightly different to your one)

    Stephen

  • Tony 105 posts 163 karma points
    Sep 09, 2014 @ 10:03
    Tony
    0

    Where can I find a list of these RenderControl Guids?I have looks through the source, the database and the DLLs? I cant find them anywhere so I cant create any other type.

  • Ryios 122 posts 263 karma points
    Dec 19, 2014 @ 07:44
    Ryios
    0

    RenderGuid controls are obsolete in umbraco 7.

    To create a datatype with USiteBuilder, here is an example

        /// <summary>
    /// This is an example of making a datatype that gets synced by USiteBuilder
    /// 
    /// The way to check these, is to create one manually in the back office, then look at the cmsDataType and cmsDataTypePreValues tables in Sql Server for the umbraco site you are working on and match up the types.
    /// 
    /// E.g. For a Dropdown list, the datatype is Nvarchar, the name is what it will show up as in the data type UI in the back office.  PropertyEditorAlias is the name of the alias of the umbraco property editor that
    /// the datatype will be "e.g. Umbraco.DropDown" and is what determines the GUI the data type will render when you add a property of this datatype to a document type or media type.
    /// 
    /// The parent ID is the node id of the Property Editor that will get used, you determine this also in the datatypes table, and if you don't see it in there it might be in umbracoNodes, -39 is the node for the multi dropdown, but the multi drop down
    /// is the same control for the drop down it just has "Allow multiple" turned off for the non multiple select dropdown.
    /// </summary>
    [Vega.USiteBuilder.DataType(DataTypeDatabaseType = DataTypeDatabaseType.Nvarchar, Name = "StaffDepartmentDropdown", PropertyEditorAlias = "Umbraco.DropDown", ParentId = -39)]
    public class StaffDepartmentDropdown : DataTypeBase
    {
        public override DataTypePrevalue[] Prevalues
        {
            get
            {
                //Prevalues are a little tricky to, because you have 3 values for any prevalue that can be set,
                //1: The value "which will be determined by it's datatype"
                //   If the datatype above is Nvarchar then the value is treated as a string and can be just about anything,
                //   however if the datatype is integer then the value is expected to be an integer "event hough the column type is text, it will error out if you don't store a number in value."
                //   etc etc..
                //2: the sort order, this determines in what order the value will be used in the ui, some property editors use the sort order, some don't.  E.g. in a dropdown it determines the order of the values in the drop down (top to bottom)
                //3: the alias, the alias is used differently depending on the type of property editor.  In the example of a drop down the alias is actually the number value of the drop down where the value is the text of the drop down, e.g.
                //   <select>
                //     <option value="AliasIsHere">ValueIsHere</option>
                //   <select>
    
                DataTypePrevalue executive = new DataTypePrevalue("Executive", 0) { Alias = "0" };
                DataTypePrevalue productOwner = new DataTypePrevalue("Product Owner", 1) { Alias = "1" };
                DataTypePrevalue implementation = new DataTypePrevalue("Implementation", 2) { Alias = "2" };
                DataTypePrevalue sales = new DataTypePrevalue("Sales", 3) { Alias = "3" };
                DataTypePrevalue support = new DataTypePrevalue("Support", 4) { Alias = "4" };
                return new DataTypePrevalue[] { executive, productOwner, implementation, sales, support };
            }
        }
    }
    
  • Ryios 122 posts 263 karma points
    Feb 13, 2015 @ 17:48
    Ryios
    0

    Deploying datatypes with code if more complicated that how uSiteBuilder does it. For startes the UniqueId field is required even though it actually isn't "uSiteBuilder won't complain if it's null, but it should". If you leave the UniqueId field empty, then it does it's compare with the Name value. If you rename the datatype under that scenario it will create a new one (duping it) instead of renaming the existing one. It has to have that UniqueId to be able to get the existing data type in the event of a rename (which I've done a lot...).

    So I rewrote it to 1. Throw an error if I leave UniqueId empty, and 2. Only do it's fetch on the UniqueId, and now renaming works properly.

    On top of that it had a bug where it was creating dupes anyways (prevalue wise), so this should fix it for people if you get the source code, change this method, build and use that instead.

            private void SynchronizeDataTypes()
        {
            var types = Util.GetFirstLevelSubTypes(typeof(DataTypeBase));
            foreach (var dtType in types)
            {
                var dataTypeAttr = GetDataTypeAttribute(dtType);
                if (dataTypeAttr == null || string.IsNullOrEmpty(dataTypeAttr.UniqueId))
                {
                    throw new InvalidOperationException("UniqueId is required on data types, give it a unique guid to be sure it doesn't get duplicated!");
                }
                try
                {
                    this.AddToSynchronized(null, dataTypeAttr.Name, dtType);
                }
                catch (Exception exc)
                {
                    throw new Exception(
                        string.Format(
                            "DataType with name '{0}' already exists! Please use unique DataType names. DataType causing the problem: '{1}' (assembly: '{2}'). Error message: {3}",
                            dataTypeAttr.Name,
                            dtType.FullName,
                            dtType.Assembly.FullName,
                            exc.Message));
                }
                //Data types can be based on existing property editors, for that reason we can't just check if the type exists, because it might already
                //So we need to check if an umbraco node for our data types name exists first
                var db = ApplicationContext.Current.DatabaseContext.Database;
                var node = db.Fetch<dynamic>("SELECT * FROM umbracoNode where UniqueId=@0", dataTypeAttr.UniqueId).FirstOrDefault();                
                int nodeId = node != null ? (int)node.id : (int)-1;
                if (node == null)
                {
                    //The node doesn't exist, so we need to create our datatype this covers both angles, it will create a node using an existing property editor, or a node using a new property editor                    
                    DataTypeDefinition dtd = new DataTypeDefinition(dataTypeAttr.ParentId, dataTypeAttr.PropertyEditorAlias)
                    {
                        Name = dataTypeAttr.Name,
                        Key = new Guid(dataTypeAttr.UniqueId)
                    };                    
                    DataTypeService.Save(dtd);
                    nodeId = dtd.Id;
                }
                //Now we need to get the datatype node to get it's node id, to ensure we get/set prevalues for the node we want and not one of the other datatypes using a shared property editor.
                var dtdNode = db.Fetch<dynamic>("SELECT * FROM cmsDataType WHERE nodeId=@0", nodeId).FirstOrDefault();
                if (dtdNode == null)                
                    throw new InvalidOperationException("Create of DataType for: " + dataTypeAttr.Name + " Failed!");
                DataTypeDefinition dataTypeDefinition = DataTypeService.GetDataTypeDefinitionById(dtdNode.nodeId);
                dataTypeDefinition.Name = dataTypeAttr.Name;
                DataTypeService.Save(dataTypeDefinition);
                //now to setup our prevalues
                var deployingPrevalues = GetEditorSettings(dtType);
                DataTypeService.SavePreValues(dataTypeDefinition, deployingPrevalues);
            }
        }
    
  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Mar 24, 2015 @ 15:03
    Alex Skrypnyk
    0

    Where can we find RenderControlGuids ? 

    UniqueId - is just new Guid ? 

    How should we tie our new created .ascx umbraco control with datatype ?

    Thanks

  • Ryios 122 posts 263 karma points
    May 01, 2015 @ 08:26
    Ryios
    0

    UniqueId is just a new Guid yes, you specify it yourself and just need to make sure it's a New Guid with the GuidGen tool and not one you found somewhere.

    You need to specify it, because if you don't you have no way of knowing if your datatype exists or not or if one that exists is actually yours (because you can create datatypes with the same name that use different property editors)...

Please Sign in or register to post replies

Write your reply to:

Draft