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 459 posts 1602 karma points
    Jun 28, 2019 @ 06:47
    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 140 posts 942 karma points
    Jun 28, 2019 @ 13:32
    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 459 posts 1602 karma points
    Jul 11, 2019 @ 09:14
    Adriano Fabri
    1

    Thank you, it works like a charme!!!

Please Sign in or register to post replies

Write your reply to:

Draft