Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

In Digizuite UI, there is an Episerver Commerce panel with ProductIds field. Example bellow and Example package are using this field to connect DAM assets to products in commerce.

...

Example code

Querying DAM for assets by codes

This code is essential for querying the DAM. Example taken from Example package source file CommerceAssetConnector.cs

Code Block
//Query to Digizuite
AssetQueryParameters aq = new Digizuite.Episerver.Models.AssetQueryParameters
{
    FoldersToSearch = null,
    AssetTypes = null,
    CropName = null,
    Sort = null,
    Limit = -1,
    Page = -1,
    OptionalFilters = new System.Collections.Generic.Dictionary<string, string>()
};
aq.OptionalFilters.Add("sProductIds_type_multistrings", "1");
aq.OptionalFilters.Add("sProductIds", code);
//possible to add products/skus
//aq.OptionalFilters.Add("sProductIds", nextcode);

IEnumerable<IContent> connectedMedia = IDigizuiteClient.Search(aq);

The IDigizuiteClient returns the IContent representation of the asset from DAM, that can be connected to Episerver Product/SKU CommerceMediaCollection where Media is connected.

How to connect assets to an Episerver product/SKU

This code sample attaches the IContent from DAM to product/SKUs

Code Block
IEnumerable<IContent> assetlist = _client.Search(aq); //_client is DI of IDigizuiteClient

if (assetlist.Any()) //check if any result from DAM
{
    if (_contentLoader.TryGet<IContent>(item.ContentReference, out IContent content))
    {
        if (content is IAssetContainer assetContainer)//contains CommerceMediaCollection
        {
            IAssetContainer ac = (content as EntryContentBase).CreateWritableClone() as IAssetContainer;
            var updated = false;
            //todo add Config remove CommerceMediaCollection for deletion of image, but this is not recomended for performance to delete and add for efter run, to much overhead.
            foreach (var asset in assetlist)
            {
                try
                {                                    
                    //check if allready connected
                    if (!ac.CommerceMediaCollection.Any(x => x.AssetLink.ID == asset.ContentLink.ID))
                    {
                        ac.CommerceMediaCollection.Insert(0, new CommerceMedia() { AssetLink = asset.ContentLink, SortOrder = 0, AssetType = "episerver.core.icontentimage", GroupName = "Digizuite" });
                        updated = true;
                    }
                    else
                    {
                        var m1 = $"Digizuite id {asset.ContentLink.ID} allready connected to {code}<br/>";
                        _logger.Debug(m1);
                        returnmessage += m1;
                        Success = true;
                        continue;
                    }
                }
                catch (Exception e)//if allready connected error
                {
                    var m2 = $"Digizuite id {asset.ContentLink.ID} failed to connect to {code} {e.Message}<br/>";
                    _logger.Debug(m2);
                    returnmessage += m2;
                    Success = false;
                    continue;
                }

                var m3 = $"Digizuite id {asset.ContentLink.ID} has been connected to {code}<br/>"; ;
                _logger.Debug(m3);
                returnmessage += m3;
                Success = true;
            }

            if (updated)
            {
                try
                {
                    //saving your change
                    //use ForceCurrentVersion and SkipValidation for performance boost/less overhead database work <-- secret
                    _contentRepository.Save(ac as IContent, EPiServer.DataAccess.SaveAction.Publish | EPiServer.DataAccess.SaveAction.SkipValidation | EPiServer.DataAccess.SaveAction.ForceCurrentVersion, EPiServer.Security.AccessLevel.NoAccess);
                }
                catch (Exception e) {
                    var m4 = $"Saving Commerce Asset failed. contentReference: {(ac as IContent).ContentLink.ID} code: {code}";
                    returnmessage += m4;
                    _logger.Error(m4, e);
                    Success = false;
                }
            }
        }
    }
}