Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
AI OnAI Off
Five New Optimizely Certifications are Here! Validate your expertise and advance your career with our latest certification exams. Click here to find out more
Which part doesn't work for you? You would have to save the content after adding the commerce media
Hi guys,
I want to upload an image to the assets tab of a Product Content , I have tried the following code but it doesnt work
var filePath = "C:\\fl.jpg"; var imageExtension = ".jpg"; var contentReferenceToFolder = new ContentReference(112837); var contentRepository = ServiceLocator.Current.GetInstance();
var newImage = contentRepository.GetDefault(contentReferenceToFolder);
var blobFactory = ServiceLocator.Current.GetInstance();
var byteArrayData = System.IO.File.ReadAllBytes(filePath);
var blob = blobFactory.CreateBlob(newImage.BinaryDataContainer, imageExtension);
using (var s = blob.OpenWrite())
{
var w = new StreamWriter(s);
w.BaseStream.Write(byteArrayData, 0, byteArrayData.Length);
w.Flush();
}
newImage.BinaryData = blob;
newImage.Name = "fl.jpg";
var contentLink = contentRepository.Save(newImage, SaveAction.Publish,AccessLevel.NoAccess);
// get a variant by its code that is equal to the sku here (it's given in the url )
var referenceConverter = ServiceLocator.Current.GetInstance();
var code = sku;
var variantLink = referenceConverter.GetContentLink(code);
var repo = ServiceLocator.Current.GetInstance();
var variant = repo.Get(variantLink);
var commerceMedia = new CommerceMedia() { AssetLink = contentLink };
var clone = variant.CreateWritableClone();
clone.Name = "Update";
contentRepository.Save(clone, SaveAction.Publish, AccessLevel.NoAccess);
clone.CommerceMediaCollection.Add(commerceMedia);