public ContentReference CreateNew(ContentReference parentNodeLink, Entry entry, ContentType nodeType) { //Create a new instance of CatalogContentTypeSample that will be a child to the specified parentNode. var content = _contentRepository.GetDefault<EntryContentBase>(parentNodeLink, nodeType.ID); // Set required properties content.Code = entry.code; return Update(content, entry, SaveAction.Publish); }
private void AddMedia(EntryContentBase content, Entry entry) { if (entry.images != null && entry.images.Any()) { for (int i = 0; i < entry.images.Count; i++) { var imageLink = entry.images[i]; var path = imageLink.path; var groupName = string.IsNullOrEmpty(imageLink.groupName) ? "default" : imageLink.groupName; var existingMedia = _urlResolver.Route(new UrlBuilder(path)) as MediaData; if (existingMedia != null) { // See if it is already linked var commerceMedia = content.CommerceMediaCollection.FirstOrDefault(m => m.AssetLink.Equals(existingMedia.ContentLink)); if (commerceMedia == null) { // Attach media to this product content.CommerceMediaCollection.Add( new CommerceMedia() { AssetLink = existingMedia.ContentLink, AssetType = "episerver.core.icontentimage", GroupName = groupName, SortOrder = i }); _log.Debug("Attaching media '{0}' to {1}", path, content.Name); } } else { // NOTE! We do not fail the import if the media does not exist // TODO: Upload media to folder. Needs to make sure // the path is correct } } } }
public ContentReference Update(EntryContentBase content, Entry entry, SaveAction saveAction) { if (string.IsNullOrEmpty(entry.urlSegment) == false) { content.RouteSegment = entry.urlSegment; } else { content.RouteSegment = entry.code.ToLowerInvariant(); } //Set the Name content.Name = entry.name; content.DisplayName = entry.name; // Override with language var displayName = GetPropertyValue(entry.properties, content.Language.IetfLanguageTag, "DisplayName"); if (string.IsNullOrEmpty(displayName) == false) { content.DisplayName = displayName; } SetProperties(content, entry.properties); // Configure Variation ConfigureVariationDefaults(content as VariationContent); // Add media AddMedia(content, entry); //Publish the new content and return its ContentReference. var contentReference = _contentRepository.Save(content, saveAction, AccessLevel.NoAccess); // Set default inventory if (Defaults.variationDefaultInventoryStock > 0) { SetDefaultInventory(entry.code, Defaults.variationDefaultInventoryStock); } // Set prices SetPrices(entry.code, entry.prices); return contentReference; }