Search code examples
umbracoumbraco5

Programatically create new media type using fluent API of Umbraco 5.1


Using Umbraco 5.1 API, I am able to create a new content type (for creating content nodes under content tab) using the below code.

 // create content type
var typeBuilder = context.Hive.Cms().NewContentType("testType", "Test Type")
            .Define("value", "contentPicker", "Content")
            .Commit();

// create content node
var packageNode = context.Hive.Cms().NewRevision(packageNodeName, packageNodeName, "testType");
        packageNode.SetUploadedFile("value", postedFile);            
        packageNode.Publish();
        packageNode.Commit();

But is there a way to create media node using fluent API? I need to create a new custom media node with a custom type in the media tab tree. I have tried the below approaches, but none of them seem to work

1) context.Hive.Cms().NewRevision();
2) context.Hive.Cms<IMediaStore>().NewRevision();
3) builderStep.NewRevision<Media, IMediaStore>();
4) builderStep.NewRevision<TypedEntity, IMediaStore>();

Solution

  • This works, but the resulting media type is not complete, as it throws an error "name should be specified" when I try to manually create a media using this type.

    CmsBuilderStep<IMediaStore> builderStep = new CmsBuilderStep<IMediaStore>(context.Hive);
    var typeBuilder = builderStep.NewMediaType<EntitySchema, IMediaStore>("testType")                   
                   .Define("package", "uploader", "General Properties")
                   .Commit();
    

    Finally I decided to create the media type manually, and use the below code to create media item via code

            // Creating a new Media item using the scorm package zip file.
            var packageNode = context.Hive.Cms<IContentStore>().NewRevision(packageNodeName, packageNodeName, "testType")
            .SetUploadedFile("package", postedFile)
    
            // set parent to media root folder - this is what makes it come under media tree
            .SetParent(FixedHiveIds.MediaVirtualRoot)
            .Publish()
            .Commit();