EdwardChan
Crownpeak Employee

Replacing Binary Assets

How do you programmatically replace binary assets with the CMS Template API?

--


Edward Chan
Sr Product Consultant

## If I’ve helped, accept this response as a solution so that other’s can find is more quickly in the future.
## Have thoughts on Crownpeak products? We'd love to hear them. Speak with the Crownpeak Product Team..

0 Kudos
1 Reply
EdwardChan
Crownpeak Employee

The following code loads a source asset and the asset to replace. It then branches the later and replaces the branch with the source asset. 

<% 
    Asset sourceAsset = Asset.Load("/path/to/source/asset.jpg");
    Asset replaceAsset = Asset.Load("/path/to/asset/to/replace.jpg");

    Asset aBranchedFile = Asset.CreateBranch(replaceAsset);
    if (aBranchedFile.IsLoaded)
    {
        Out.WriteLine("ID: " + aBranchedFile.Id.ToString());
        string base64data = BinaryFile.LoadAsBase64(sourceAsset.Id.ToString());
        if(!aBranchedFile.UpdateFromBase64(base64data))
            Out.WriteLine("context: " + context.Error);
    }
 %>

 

--


Edward Chan
Sr Product Consultant

## If I’ve helped, accept this response as a solution so that other’s can find is more quickly in the future.
## Have thoughts on Crownpeak products? We'd love to hear them. Speak with the Crownpeak Product Team..

0 Kudos