Developer forum

Forum » Development » Accessing assets

Accessing assets

Fredrik
Fredrik
Reply

Hello

I want to be able to add, edit and delete assets (both documents, images and video links) via code in the back end. How do I do that? (Version 9.)

Thanks in advance,

Fredrik


Replies

 
Viktor Letavin Dynamicweb Employee
Viktor Letavin
Reply

Hi,
I hope sample below will help

using Dynamicweb.Ecommerce.Products;
using System.IO;
using System.Linq;
namespace Dynamicweb.Ecommerce.Examples.Products
{
    class ManageProductAssetsSample
    {
        void ManageDetails() 
        {
            var product = Dynamicweb.Ecommerce.Services.Products.GetProductById("MyProduct", string.Empty, true);
            var productAssets = Dynamicweb.Ecommerce.Services.Details.GetDetails(product);
            var myAssetCategoryId = 1;
            var myAssetCategory = Dynamicweb.Ecommerce.Services.DetailsGroups.GetById(myAssetCategoryId);
            var extensions = Dynamicweb.Ecommerce.Services.DetailsGroups.GetDetailExtensions(); //taken from globalSettings "/Globalsettings/Ecom/Product/Details/Extensions"
            foreach (var asset in productAssets) 
            {
                if (asset.Name == "My name") 
                {
                    Dynamicweb.Ecommerce.Services.Details.UpdateDefaultDetailForProduct(product, asset.Name);
                }
                if (asset.Value.StartsWith("/Files/Images/My category/")) 
                {
                    Dynamicweb.Ecommerce.Services.Details.SetDetailsGroup(asset, myAssetCategory.Id);
                }
                var assetExtension = Path.GetExtension(asset.Value);
                if (!extensions.Contains(assetExtension)) 
                {
                    Dynamicweb.Ecommerce.Services.Details.Delete(asset.Id);
                }
            }
            var newAsset = new Detail()
            {
                ProductId = product.Id,
                VariantId = product.VariantId,
                LanguageId = product.LanguageId,
                Name = "Some label",
                Keywords = "some key words",
                Value = "/Files/Images/myProdImage.jpg"
            };
            Dynamicweb.Ecommerce.Services.Details.Save(newAsset, product);
        }
    }
}

Best Regards, Viktor.

 

You must be logged in to post in the forum