Copied to clipboard

Flag this post as spam?

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


  • Flavio Spezi 129 posts 315 karma points
    Sep 14, 2014 @ 19:44
    Flavio Spezi
    0

    Convert MediaType in ContentType

    I'm using Media tree to store some website config parameters, like "Payment method", "Delivery methods", "Currencies".

    After some month I look that Media items have not the same features like Content items: I can Import/Export MediaType or Media items from/to another project, I can't "save without publish", I can't integrate Media or MediaType in custom Package.

    Then I would conver my "Config" node (and relative content) from Media item to Content item.

    I try to search in internet, but I have not found solutions. Then I decide to try it.

    This is the solution

    I develop a T-SQL command to convert a MediaType in ContentType and relative Media to Content.
    The first group of code is to declare variables.
    The second group is the section where you can configure the T-SQL command. Then the next groups are the update and insert commands.

    The T-SQL command convert one MediaType per time.
    Before to execute any DB operation, I suggest you to stop Web Application and to make a backup of the Database. If the MediaType to convert inherits by another MediaType, it is important to convert the base MediaType before the inherited MediaType.
    If the base MediaType is not to convert, you will create the inherited properties inside of MediaType to convert before to start: the newest properties must have the same propertyType and a new (temporary) alias.
    To convert a MediaType you will need to know the id of the type. You can find it executing this T-SQL query:
    SELECT nodeId FROM cmsContentType WHERE alias = 'mediaType-alias'

    Now you are ready to start.
    To convert a MediaType you will need to know the id of the type. You can set it in the T-SQL in the @mediaType_id variable.
    If your MediaType inherits by a MediaType that it will not convert, the first step will be to move field values from the inherited properties to the newest. It will be do per any property, one step one. Set @execute_moveFieldValues = 1 and @execute_convertMediaType = 0 to indicates that you want to move field values only. Then set @moveFieldValues_*** variables and run the T-SQL. Repeat it for any property to adjust. Now you can convert the MediaType. Set @execute_moveFieldValues = 0 and @execute_convertMediaType = 1 to indicates that you want convert the MediaType in ContentType. If the MediaType inherits from e MediaType that it will not convert, it is important that you set @convertMediaType_removeInherits = 1; otherwise if the MediaType inherits from another converted (or will be converted) MediaType set it to 0.

    The T-SQL command

    -- MediaType to work
    DECLARE @mediaType_id int;
    
    -- Config for actions to execute
    DECLARE @execute_moveFieldValues bit;
    DECLARE @execute_convertMediaType bit;
    
    -- Config for moving data from inherited MediaType to current MediaType
    DECLARE @moveFieldValues_oldFieldAlias varchar(200);    -- alias of property with data
    DECLARE @moveFieldValues_oldFieldMediaTypeId int;       -- mediaTypeId where property is declared
    DECLARE @moveFieldValues_newFieldAlias varchar(200);    -- alias of newest property where to move the data
    
    -- Config for conversion in ContentType
    DECLARE @convertMediaType_removeInherits bit;
    
    
    
    -- Setting variables
    
    
    SET @mediaType_id = 13449;
    SET @execute_moveFieldValues = 0;
    SET @execute_convertMediaType = 1;
    
    SET @moveFieldValues_oldFieldAlias = 'umbracoFile';
    SET @moveFieldValues_oldFieldMediaTypeId = 1033;
    SET @moveFieldValues_newFieldAlias = 'risorsa';
    
    SET @convertMediaType_removeInherits = 0;
    
    
    
    
    -- RUN
    
    
    IF (@execute_moveFieldValues = 1) BEGIN
    
        DECLARE @moveFieldValues_oldFieldId int;
        DECLARE @moveFieldValues_newFieldId int;
    
        SELECT @moveFieldValues_oldFieldId = TB1.ID
        FROM cmsPropertyType TB1
        WHERE TB1.contentTypeId = @moveFieldValues_oldFieldMediaTypeId AND TB1.Alias = @moveFieldValues_oldFieldAlias;
    
        SELECT @moveFieldValues_newFieldId = TB1.ID
        FROM cmsPropertyType TB1
        WHERE TB1.contentTypeId = @mediaType_id AND TB1.Alias = @moveFieldValues_newFieldAlias;
    
        IF (@moveFieldValues_oldFieldId > 0 AND @moveFieldValues_newFieldId > 0) BEGIN
    
            SELECT 'OK, propertyTypes founded.', null
            UNION ALL SELECT 'Old Field ID:', @moveFieldValues_oldFieldId
            UNION ALL SELECT 'New Field ID:', @moveFieldValues_newFieldId;
    
            UPDATE cmsPropertyData
            SET propertytypeid = @moveFieldValues_newFieldId
            WHERE propertytypeid = @moveFieldValues_oldFieldId;
    
    
        END ELSE BEGIN
    
            SELECT 'ERROR !!! Fields not found', null
            UNION ALL SELECT 'Old Field ID:', @moveFieldValues_oldFieldId
            UNION ALL SELECT 'New Field ID:', @moveFieldValues_newFieldId;
    
        END;
    
    END;
    
    
    IF (@execute_convertMediaType = 1) BEGIN
    
        -- Update nodeTypeObject of MediaType record
        UPDATE umbracoNode
        SET nodeObjectType = 'A2CB7800-F571-4787-9638-BC48539A0EFB'
        WHERE nodeObjectType = '4EA4382B-2F5A-4C2B-9587-AE9B3CF3602E' AND id = @mediaType_id
    
        -- Update nodeTypeObject of Media records of MediaType
        UPDATE TB1
        SET nodeObjectType = 'C66BA18E-EAF3-4CFF-8A22-41B16D66A972'
        FROM umbracoNode TB1
            INNER JOIN cmsContent TB2 ON TB1.id = TB2.nodeId
        WHERE TB2.contentType = @mediaType_id AND TB1.nodeObjectType = 'B796F64C-1F99-4FFB-B886-4BF4BC011A9C'
    
        -- Add version records for Media records of MediaType
        IF NOT EXISTS (SELECT * FROM cmsDocument TBa INNER JOIN cmsContent TBb ON TBa.nodeId = TBb.nodeId WHERE TBb.contentType = @mediaType_id) BEGIN
            -- -- DELETE TB1 FROM cmsDocument TB1 INNER JOIN cmsContent TB2 ON TB1.nodeId = TB2.nodeId WHERE TB2.contentType = @mediaType_id
    
            INSERT INTO cmsDocument (nodeId, published, documentUser, versionId, text, releaseDate, expireDate, updateDate, templateId, alias, newest)
            SELECT TB1.id, 0, 0, ISNULL((SELECT TOP 1 TBx.versionId FROM cmsPropertyData TBx WHERE TBx.contentNodeId = TB1.id), NEWID()), tb1.text, null, null, tb1.createDate, null, null, 1
            FROM umbracoNode TB1
                INNER JOIN cmsContent TB2 ON TB1.id = TB2.nodeId
            WHERE TB2.contentType = @mediaType_id
        END;
    
        -- Find inherits
        DECLARE @currentInherit int;
        SELECT @currentInherit = TB1.parentID FROM umbracoNode TB1 WHERE TB1.id = @mediaType_id;
    
        IF (@convertMediaType_removeInherits = 1 AND @currentInherit > 0) BEGIN
    
            UPDATE umbracoNode
            SET parentId = -1
                , path = REPLACE(path, ',' + CAST(@currentInherit AS varchar(20)) + ',', ',')
            WHERE id = @mediaType_id
    
            DELETE TB1
            FROM cmsContentType2ContentType TB1
            WHERE TB1.childContentTypeId = @mediaType_id
    
        END ;
    
    END;
    
Please Sign in or register to post replies

Write your reply to:

Draft