Versions Compared

Key

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

...

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;
            //todoDecomment addto Config remove CommerceMediaCollection for deletion of image, but this is not recomended for performance to delete and add for efter run, to much overhead.
            //reset commerce collection
            //if (ac.CommerceMediaCollection.Any())
                  //ac.CommerceMediaCollection = new ItemCollection<CommerceMedia>();
                  
            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;
                }
            }
        }
    }
}

...