Copied to clipboard

Flag this post as spam?

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


  • Adriano Fabri 344 posts 1175 karma points
    25 days ago
    Adriano Fabri
    1

    How to get specific Content Type in umbraco v8

    Hi, I'm updating my projects for Umbraco v8 but I have some problems.

    I'm trying to update this code that work perfectly in v7:

        var contentTypeService = Current.Services.ContentTypeService;
        var contentService = Current.Services.ContentService;
    
        if (contentTypeService.GetContentType("falmHKCacheCleanup") == null)
        {
            // Create Document Type "falmHKCacheCleanup"
            var createNewDocumentType = new ContentType(dtFalmContainer.Id)
                {
                    Alias = "falmHKCacheCleanup",
                    Name = "FALM Housekeeping - Cache Directory Cleanup",
                    AllowedAsRoot = false,
                    Icon = "icon-trash color-orange"
                };
    
                contentTypeService.Save(createNewDocumentType);
    
                Logger.Info<string>("FALM Housekeeping - Document Type 'FALM Housekeeping - Cache Directory Cleanup' successfully created");
            }
            else
            {
                Logger.Info<string>("FALM Housekeeping - Document Type 'FALM Housekeeping - Cache Directory Cleanup' already exist");
            }
    

    The problem is that in the VS2019 "Error List" panel I receive this error:

    'IContentTypeService' does not contain a definition for 'GetContentType' and no accessible extension method 'GetContentType' accepting a first argument of type 'IContentTypeService' could be found (are you missing a using directive or an assembly reference?)

    I tried to search a solution but I didn't find it.

    Can anyone help me? Thank you very much

  • Rhys Hamilton 132 posts 841 karma points
    25 days ago
    Rhys Hamilton
    100

    You could use the GetAllContentTypeAliases() method to retrieve all of the existing content type aliases, then check if it contains the alias falmHKCacheCleanup.

    Something like the following should work:

    var contentTypeService = Current.Services.ContentTypeService;
    var contentService = Current.Services.ContentService;
    
    // Get all content types
    var contentTypes = contentTypeService.GetAllContentTypeAliases();
    
    // If the alias does not exist, create docType
    if (!contentTypes.Contains("falmHKCacheCleanup"))
    {
        // Create Document Type "falmHKCacheCleanup"
        var createNewDocumentType = new ContentType(dtFalmContainer.Id)
        {
            Alias = "falmHKCacheCleanup",
            Name = "FALM Housekeeping - Cache Directory Cleanup",
            AllowedAsRoot = false,
            Icon = "icon-trash color-orange"
        };
    
        contentTypeService.Save(createNewDocumentType);
    
        Logger.Info<string>("FALM Housekeeping - Document Type 'FALM Housekeeping - Cache Directory Cleanup' successfully created");
    }
    else
    {
        Logger.Info<string>("FALM Housekeeping - Document Type 'FALM Housekeeping - Cache Directory Cleanup' already exist");
    }
    
  • Adriano Fabri 344 posts 1175 karma points
    1 week ago
    Adriano Fabri
    1

    Thank you, it works like a charme!!!

Please Sign in or register to post replies

Write your reply to:

Draft