internal string ProcessEclXlink(XmlElement xlinkElement) { string eclStubComponentId = xlinkElement.GetAttribute("href", "http://www.w3.org/1999/xlink"); IContentLibraryContext eclContext; IContentLibraryMultimediaItem eclItem = GetEclItem(eclStubComponentId, out eclContext); // This may look a bit unusual, but we have to ensure that ECL Item members are accessed *before* the ECL Context is disposed. using (eclContext) { // Set additional ECL Item properties as data attributes on the XLink element xlinkElement.SetAttribute("data-eclId", eclItem.Id.ToString()); xlinkElement.SetAttribute("data-eclDisplayTypeId", eclItem.DisplayTypeId); if (!string.IsNullOrEmpty(eclItem.MimeType)) { xlinkElement.SetAttribute("data-eclMimeType", eclItem.MimeType); } if (!string.IsNullOrEmpty(eclItem.Filename)) { xlinkElement.SetAttribute("data-eclFileName", eclItem.Filename); } string eclTemplateFragment = eclItem.GetTemplateFragment(null); if (!string.IsNullOrEmpty(eclTemplateFragment)) { // Note that the entire Template Fragment gets stuffed in an XHTML attribute. // This may seem scary, but there is no limitation to the size of an XML attribute and the XLink element typically already has content. xlinkElement.SetAttribute("data-eclTemplateFragment", eclTemplateFragment); } // TODO: ECL external metadata (?) string directLinkToPublished = eclItem.GetDirectLinkToPublished(null); return(string.IsNullOrEmpty(directLinkToPublished) ? PublishBinaryContent(eclItem, eclStubComponentId) : directLinkToPublished); } }
internal void ProcessEclStubComponent(Component eclStubComponent) { IContentLibraryContext eclContext; IContentLibraryMultimediaItem eclItem = GetEclItem(eclStubComponent.Id, out eclContext); // This may look a bit unusual, but we have to ensure that ECL Item members are accessed *before* the ECL Context is disposed. using (eclContext) { eclStubComponent.EclId = eclItem.Id.ToString(); string directLinkToPublished = eclItem.GetDirectLinkToPublished(null); eclStubComponent.Multimedia.Url = string.IsNullOrEmpty(directLinkToPublished) ? PublishBinaryContent(eclItem, eclStubComponent.Id) : directLinkToPublished; // Set additional ECL Item properties as ExtensionData on the ECL Stub Component. const string eclSectionName = "ECL"; eclStubComponent.AddExtensionProperty(eclSectionName, "DisplayTypeId", eclItem.DisplayTypeId); eclStubComponent.AddExtensionProperty(eclSectionName, "MimeType", eclItem.MimeType); eclStubComponent.AddExtensionProperty(eclSectionName, "FileName", eclItem.Filename); eclStubComponent.AddExtensionProperty(eclSectionName, "TemplateFragment", eclItem.GetTemplateFragment(null)); IFieldSet eclExternalMetadataFieldSet = BuildExternalMetadataFieldSet(eclItem); if (eclExternalMetadataFieldSet != null) { eclStubComponent.ExtensionData["ECL-ExternalMetadata"] = eclExternalMetadataFieldSet; } } }
internal XmlElement BuildEntityModel(EntityModelData entityModelData, Component eclStubComponent) { IContentLibraryContext eclContext; IContentLibraryMultimediaItem eclItem = GetEclItem(eclStubComponent.Id, out eclContext); // This may look a bit unusual, but we have to ensure that ECL Item members are accessed *before* the ECL Context is disposed. using (eclContext) { BinaryContent eclStubBinaryContent = eclStubComponent.BinaryContent; string directLinkToPublished = eclItem.GetDirectLinkToPublished(_emptyAttributes); entityModelData.BinaryContent = new BinaryContentData { Url = string.IsNullOrEmpty(directLinkToPublished) ? PublishBinaryContent(eclItem, eclStubComponent) : directLinkToPublished, MimeType = eclItem.MimeType ?? eclStubBinaryContent.MultimediaType.MimeType, FileName = eclItem.Filename ?? eclStubBinaryContent.Filename, FileSize = eclStubComponent.BinaryContent.Size }; XmlElement externalMetadata = null; if (!string.IsNullOrEmpty(eclItem.MetadataXml)) { XmlDocument externalMetadataDoc = new XmlDocument(); externalMetadataDoc.LoadXml(eclItem.MetadataXml); externalMetadata = externalMetadataDoc.DocumentElement; } entityModelData.ExternalContent = new ExternalContentData { Id = eclItem.Id.ToString(), DisplayTypeId = eclItem.DisplayTypeId, TemplateFragment = eclItem.GetTemplateFragment(_emptyAttributes) // Note: not setting Metadata here, but returning the external metadata as raw XML. }; return(externalMetadata); } }
protected virtual string GetECLUrl(string uri) { Component c = (Component)engine.GetObject(uri); if (c == null) { throw new Exception(string.Format("Error loading item with uri {0}", uri)); } log.Debug(System.Threading.Thread.CurrentThread.ManagedThreadId + ": Instantiating a new ECL Session"); using (IEclSession localSession = SessionFactory.CreateEclSession(engine.GetSession())) { IEclUri eclUri = localSession.TryGetEclUriFromTcmUri(uri); if (eclUri != null) // this is an ECL item { log.Debug(System.Threading.Thread.CurrentThread.ManagedThreadId + ": Fetching IContentLibraryContext"); using (IContentLibraryContext context = localSession.GetContentLibrary(eclUri)) { try { IContentLibraryMultimediaItem item = null; // This is done this way to not have an exception thrown through GetItem, as stated // in the API doc. // The reason to do this, is because if there is an exception, // the ServiceChannel is going into the aborted state... log.Debug(System.Threading.Thread.CurrentThread.ManagedThreadId + ": Get Items"); // GetItems allows up to 20 (depending on config) connections. // After that any new connection is aborted / not created. var items = context.GetItems(new IEclUri[] { eclUri }); if (items != null && items.Count == 1) { item = (IContentLibraryMultimediaItem)items.First(); log.Debug(System.Threading.Thread.CurrentThread.ManagedThreadId + ": Item Fetched"); } if (item == null) { log.Warning(System.Threading.Thread.CurrentThread.ManagedThreadId + ": Item with ECL URI: " + eclUri + " not found. This MM item is used in: " + c.Id); throw new Exception(string.Format("ECL item not found (ecl uri = {0}, tcm uri = {1}", eclUri, c.Id)); } string distributionUrl = item.GetDirectLinkToPublished(null); string result = distributionUrl.ToLower(); if (!string.IsNullOrEmpty(result)) { log.Debug(System.Threading.Thread.CurrentThread.ManagedThreadId + ": Returning: " + result); return(result); } } finally { log.Debug(System.Threading.Thread.CurrentThread.ManagedThreadId + ": Going out of the context using block statement."); } } } } return(string.Empty); }
private string ImportSingleItem(IEclUri eclUri) { string id = "tcm:0-0-0"; IContentLibraryMultimediaItem eclItem = (IContentLibraryMultimediaItem)_eclContentLibraryContext.GetItem(eclUri); string extension = eclItem.Filename.Substring(eclItem.Filename.LastIndexOf('.') + 1); MemoryStream ms = null; string tempPath; try { // create some template attributes IList <ITemplateAttribute> attributes = CreateTemplateAttributes(eclItem); // determine if item has content or is available online string publishedPath = eclItem.GetDirectLinkToPublished(attributes); if (string.IsNullOrEmpty(publishedPath)) { // we can directly get the content IContentResult content = eclItem.GetContent(attributes); ms = new MemoryStream(); content.Stream.CopyTo(ms); ms.Position = 0; } else { // read the content from the publish path using (WebClient webClient = new WebClient()) { byte[] thumbnailData = webClient.DownloadData(publishedPath); ms = new MemoryStream(thumbnailData, false); } } // upload binary (using netTcp binding as configured in SDL Tridion, because this Model extension is running inside the UI) using (StreamUploadClient suClient = new StreamUploadClient("streamUpload_netTcp_2012")) { tempPath = suClient.UploadBinaryContent(eclItem.Filename, ms); } } finally { if (ms != null) { ms.Dispose(); } } // create tcm item var mmComponent = new ComponentData { Id = id, Title = eclItem.Title, Schema = new LinkToSchemaData { IdRef = _schemaUri }, LocationInfo = new LocationInfo { OrganizationalItem = new LinkToOrganizationalItemData { IdRef = _folderUri } } }; // put binary data in tcm item (using netTcp binding as configured in SDL Tridion, because this Model extension is running inside the UI) using (SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient("netTcp_2012")) { // impersonate with current user client.Impersonate(_username); // set metadata var schemaFields = client.ReadSchemaFields(_schemaUri, true, new ReadOptions()); if (schemaFields.MetadataFields.Any()) { var fields = Fields.ForMetadataOf(schemaFields, mmComponent); if (!string.IsNullOrEmpty(eclItem.MetadataXml)) { XNamespace ns = GetNamespace(eclItem.MetadataXml); XDocument metadata = XDocument.Parse(eclItem.MetadataXml); var children = metadata.Element(ns + "Metadata").Descendants(); for (int i = 0; i < children.Count(); i++) { fields.AddFieldElement(new ItemFieldDefinitionData { Name = "data" }); var embeddedFields = fields["data"].GetSubFields(i); embeddedFields.AddFieldElement(new ItemFieldDefinitionData { Name = "key" }); embeddedFields.AddFieldElement(new ItemFieldDefinitionData { Name = "value" }); embeddedFields["key"].Value = children.ElementAt(i).Name.LocalName; embeddedFields["value"].Value = children.ElementAt(i).Value; } } mmComponent.Metadata = fields.ToString(); } // find multimedia type var list = client.GetSystemWideList(new MultimediaTypesFilterData()); var multimediaType = list.OfType <MultimediaTypeData>().Single(mt => mt.FileExtensions.Contains(extension)); // set BinaryContent of a component mmComponent.BinaryContent = new BinaryContentData { UploadFromFile = tempPath, Filename = eclItem.Filename, MultimediaType = new LinkToMultimediaTypeData { IdRef = multimediaType.Id } }; // create (and save) component ComponentData data = (ComponentData)client.Create(mmComponent, new ReadOptions()); id = data.Id; } //string result = string.Format("created {0}, from {1}, in {2}, using {3}, for {4}", id, eclUri, _folderUri, _schemaUri, _username); return(id); }