PTB and ImageVault

Today I had a problem using a ImageVaultproperty with PageTypeBuilder.

Earlier I used EPiServers own image handling for every property but this client wanted to use ImageVault instead.

First I just changed the typeof(PropertyImageUrl) to PropertyImageVaultFile but that didnt work so I also changed the return type from string to int since PropertyImageVaultFile returns the ID of the selected file. So in short:

From:

        [PageTypeProperty(
            EditCaption = "Image",
            HelpText = "Sets the puffs image",
            UniqueValuePerLanguage = true,
            Searchable = false,
            SortOrder = 125,
            Type = typeof(PropertyImageUrl),
            Tab = typeof(ComposerTab))]
        public virtual string ImageUrl { get; set; }

To:

        [PageTypeProperty(
            EditCaption = "Image",
            HelpText = "Sets the puffs image",
            UniqueValuePerLanguage = true,
            Searchable = false,
            SortOrder = 125,
            Type = typeof(PropertyImageVaultFile),
            Tab = typeof(ComposerTab))]
        public virtual int ImageUrl { get; set; }

So what I get in return from that property is the Id of the image and then I just used the following code to create a IVFileData object, to note here is that I using Composer aswell and the ContentFunctionData[string PropertyName] is always an object if the property exists, so a additional null check is needed before we try to create and IVFileData object from the property. Since the ContentFunctionData[string PropertyName] returns a string for the value, I needed to parse the string aswell.

        public static IVFileData GetIVFileDataFromData(ContentFunctionData content, string PropertyName = "Image")
        {
            if (content != null && content[PropertyName] != null) 
            {
                string stringFileId = content[PropertyName].ToString();
                int fileId;
                if (int.TryParse(stringFileId, out fileId))
                {
                    IVFileData image = IVDataFactory.GetFile(fileId);
                    return image;
                }
            }
            return null;
        }