Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Getting images by name with the new assets system

Vote:
 

We used to get assets for a product by using the full file name for the asset

var assetEntities = EntityManager.List<EntityObject>(FolderElementEntity.ClassName, FilterElement.EqualElement(FolderElementEntity.FieldName, row.VariationImageName));

How do i get the same asset by file name using the new asset system? Can i search by using a propertycriteria?

#88035
Jun 30, 2014 8:49
Vote:
 

Hi,

Basically in new asset system, an asset is a content, so you can use the EPiServer Search API:s to search for it. Here's an example how:

http://world.episerver.com/Blogs/Eric-Herlitz/Dates/2013/1/Using-EPiServer-Search-in-EPiServer-7-MVC/

Regards.

/Q

#88058
Jul 01, 2014 5:24
Vote:
 

Thanks

Im not sure how to continue from here, how do I create a CommerceMedia from a MediaData item? What is an assetId?

                var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
                var loader = ServiceLocator.Current.GetInstance<IContentLoader>();
                var referenceConvertor = ServiceLocator.Current.GetInstance<ReferenceConverter>();

                var variant = loader.Get<VariationContent>(referenceConvertor.GetContentLink(catalogEntryDto.CatalogEntry[0].Code)); // Get variant from new API
                var entry = variant.LoadEntry();
                
                var contentId = GetContentIDFronFileName(row.VariationImageName);
                var image = loader.Get<MediaData>(new ContentReference(contentId));
                
                variant.CommerceMediaCollection.Add(new CommerceMedia(??????));
#88071
Jul 01, 2014 11:15
Vote:
 

Hi,

I don't know where you get that code. What is the goal you're trying to archive?

/Q

#88073
Jul 01, 2014 11:20
Vote:
 

When importing products to Episerver i want the product to make a connection with its default image defined by a full filename.

This is the old code for the project

        private void SetVariationImage(CatalogEntryDto catalogEntryDto, MaterialRow row)
        {
            var culture = MetaDataContext.DefaultCurrent.Language;
            var cultureInfo = new CultureInfo(culture);
            var variationWithParent = CatalogHelper.GetVariationWithParent(string.Concat(row.MaterialId, "_", row.SalesOrgId), cultureInfo);
            if (variationWithParent != null && variationWithParent.ParentEntry != null)
            {
                var indexList = new List<int>();
                for (var i = 0; i < catalogEntryDto.CatalogItemAsset.Count; i++)
                {
                    if(catalogEntryDto.CatalogItemAsset[i].GroupName.Equals(AssetGroup.PrimaryImage))
                        indexList.Add(i);
                }
                foreach (var index in indexList)
                {
                    catalogEntryDto.CatalogItemAsset[index].Delete();
                }
                CatalogContext.Current.SaveCatalogEntry(catalogEntryDto);
            }

            if (!string.IsNullOrEmpty(row.VariationImageName))
            {
                var assetEntities = EntityManager.List<EntityObject>(FolderElementEntity.ClassName, FilterElement.EqualElement(FolderElementEntity.FieldName, row.VariationImageName));
                var temp = assetEntities.FirstOrDefault();
                if (temp != null)
                {
                    var primaryKeyId = temp.PrimaryKeyId;
                    if (primaryKeyId != null && primaryKeyId.Value.ToString() != null)
                    {
                        while (true)
                        {
                            var exist = _productRepo.CatalogAssociationIdExist(1, catalogEntryDto.CatalogEntry[0].CatalogEntryId);
                            if (!exist) break;
                        }
                        var newPrimaryImageAssetRow = catalogEntryDto.CatalogItemAsset.NewCatalogItemAssetRow();
                        newPrimaryImageAssetRow.AssetKey = primaryKeyId.Value.ToString();
                        newPrimaryImageAssetRow.AssetType = "file";
                        newPrimaryImageAssetRow.CatalogNodeId = 1; //TODO double-check
                        newPrimaryImageAssetRow.CatalogEntryRow = catalogEntryDto.CatalogEntry[0];
                        newPrimaryImageAssetRow.GroupName = AssetGroup.PrimaryImage;
                        newPrimaryImageAssetRow.SortOrder = 0;

                        catalogEntryDto.CatalogItemAsset.AddCatalogItemAssetRow(newPrimaryImageAssetRow);
                        CatalogContext.Current.SaveCatalogEntry(catalogEntryDto);
                    }
                }
            }
        }
#88074
Jul 01, 2014 11:22
Vote:
 

Hi,

It's not entirely clear for me what you're trying to do, but basically, you can keep either way - keep your old code, only note the AssetKey is now obsolete (in 7.8.x) and you need to switch to AssetLink instead, or switch to new CommerceMedia way.

If you want to use CommerceMedia, it's basic class with this information. I'll assume you're using 7.5:

var commerceMedia = new CommerceMedia() 

{

    AssetKey = //GUID of the image

    AssetType = "file"

    GroupName = //Something you like

     SortOrder = //Something you like

}

Then you can add it to CommerceMediaCollection as normal.

Regards.

/Q

#88078
Edited, Jul 01, 2014 11:44
Vote:
 

I'm trying to save the variant, but im getting the following error

{System.ArgumentNullException: The provided content link does not have a value.
Parameter name: contentLink
   at EPiServer.Core.DefaultContentLoader.Get[T](ContentReference contentLink, ILanguageSelector selector)
   at EPiServer.Commerce.Validation.MissingStartingPointLanguageValidator.<>c__DisplayClass2.<ShouldShowWarning>b__1(ICommerceRouter route)
   at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
   at EPiServer.Commerce.Validation.MissingStartingPointLanguageValidator.ShouldShowWarning(CatalogContentBase content)
   at EPiServer.Commerce.Validation.MissingStartingPointLanguageValidator.Validate(CatalogContentBase instance)
   at EPiServer.Validation.ValidationService.ValidateRecursively(Object instance, HashSet`1 visitedInstances)
   at EPiServer.Core.ContentProvider.Validate(IContent content, SaveAction action)
   at EPiServer.Core.DefaultContentRepository.Save(IContent content, SaveAction action, AccessLevel access)
   at Avensia.Bona.Integration.ProductRefinement.MaterialProcessor.SetVariationImage(CatalogEntryDto catalogEntryDto, MaterialRow row) in c:\Projects\Avensia.Bona\Avensia.Bona.Integration.ProductRefinement\MaterialProcessor.cs:line 348
   at Avensia.Bona.Integration.ProductRefinement.MaterialProcessor.SetVariationMetaProperties(MaterialRow row, CatalogEntryDto entryDto, IEnumerable`1 catalogLanguages) in c:\Projects\Avensia.Bona\Avensia.Bona.Integration.ProductRefinement\MaterialProcessor.cs:line 319
   at Avensia.Bona.Integration.ProductRefinement.MaterialProcessor.ProcessCatalogEntry(MaterialRow row, CatalogEntryDto entryDto, Int32 catalogNodeId) in c:\Projects\Avensia.Bona\Avensia.Bona.Integration.ProductRefinement\MaterialProcessor.cs:line 85
   at Avensia.Bona.Integration.ProductRefinement.MaterialProcessor.ProcessMaterial(MaterialRow row) in c:\Projects\Avensia.Bona\Avensia.Bona.Integration.ProductRefinement\MaterialProcessor.cs:line 73
   at Avensia.Bona.Integration.ProductRefinementService.RefineProductFile(ProductRefinementFile file) in c:\Projects\Avensia.Bona\Avensia.Bona.Integration\ProductRefinementService.cs:line 33
   at Avensia.Bona.Site.ScheduledJobs.ProductIntegration.RunProductRefinmentForFile(ProductRefinementFile file) in c:\Projects\Avensia.Bona\Avensia.Bona.Site\ScheduledJobs\ProductIntegration.cs:line 80
   at NHibernate.Linq.TypeHelperExtensionMethods.ForEach[T](IEnumerable`1 query, Action`1 method)
   at Avensia.Bona.Site.ScheduledJobs.ProductIntegration.Execute() in c:\Projects\Avensia.Bona\Avensia.Bona.Site\ScheduledJobs\ProductIntegration.cs:line 54}

using

            if (!string.IsNullOrEmpty(row.VariationImageName))
            {

                var contentRepository = ServiceLocator.Current.GetInstance<IContentRepository>();
                var loader = ServiceLocator.Current.GetInstance<IContentLoader>();
                var referenceConvertor = ServiceLocator.Current.GetInstance<ReferenceConverter>();

                var variant = (VariationContent)loader.Get<VariationContent>(referenceConvertor.GetContentLink(catalogEntryDto.CatalogEntry[0].Code)).CreateWritableClone(); // Get variant from new API
                
                var contentId = GetContentIDFronFileName(row.VariationImageName);
                var image = loader.Get<MediaData>(new ContentReference(contentId));

                variant.CommerceMediaCollection.Add(new CommerceMedia(image.ContentGuid.ToString(), "file", "Primary", 0, 0));
                contentRepository.Save(variant, SaveAction.Publish, AccessLevel.NoAccess);
            }



#88080
Edited, Jul 01, 2014 13:03
Vote:
 

Hi,

The problem you're seeing seems to be unrelated to the asset problem. I suspect the issue was RouteStartingPoint, which was not properly configured for the current language.

What's the code you're using to register the Hierarchical router for Commerce? I would suggest to comment the code related to asset and run to see.

/Q

#88149
Jul 03, 2014 8:32
Vote:
 

Hi, we're seeing the same issue here when trying to save a product. The error is:

1: Error Description: The provided content link does not have a value.
Parameter name: contentLink
1: Source: EPiServer
1: Stack Trace:    at EPiServer.Core.DefaultContentLoader.Get[T](ContentReference contentLink, ILanguageSelector selector)
1:    at EPiServer.Commerce.Validation.MissingStartingPointLanguageValidator.<>c__DisplayClass2.b__1(ICommerceRouter route)
1:    at System.Linq.Enumerable.Any[TSource](IEnumerable`1 source, Func`2 predicate)
1:    at EPiServer.Commerce.Validation.MissingStartingPointLanguageValidator.ShouldShowWarning(CatalogContentBase content)
1:    at EPiServer.Commerce.Validation.MissingStartingPointLanguageValidator.Validate(CatalogContentBase instance)
1:    at EPiServer.Validation.ValidationService.ValidateRecursively(Object instance, HashSet`1 visitedInstances)
1:    at EPiServer.Core.ContentProvider.Validate(IContent content, SaveAction action)
1:    at EPiServer.Core.DefaultContentRepository.Save(IContent content, SaveAction action, AccessLevel access)

The code we're using to register the hierarchical router is:

CatalogRouteHelper.MapDefaultHierarchialRouter(RouteTable.Routes, () => ContentReference.StartPage, false);

If we remove this line the product will save ok but we need hierarchical routing enabled for the site.

#90336
Sep 08, 2014 3:33
Vote:
 

@Paul: It's quite clear that you're saving catalog content in another language, while that language was not enabled in the start page. You'll need to create a version of start page in that language before continuing.

Regards.

/Q

#90357
Sep 08, 2014 11:15
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.