public string ImportItem(string eclOrTcmUri, string folderUri, string schemaUri) { _folderUri = folderUri; _schemaUri = schemaUri; _username = ServiceSecurityContext.Current.WindowsIdentity.Name; // create new tcm session for current user, so we can create an ecl session to get the ecl item and read its content using (Session tcmSession = new Session(_username)) using (IEclSession eclSession = SessionFactory.CreateEclSession(tcmSession)) { IEclUri eclUri = eclOrTcmUri.StartsWith("tcm:") ? eclSession.TryGetEclUriFromTcmUri(eclOrTcmUri) : eclSession.HostServices.CreateEclUri(eclOrTcmUri); if (eclUri != null) { using (_eclContentLibraryContext = eclSession.GetContentLibrary(eclUri)) { if (eclUri.ItemType == EclItemTypes.Folder) { // loop over all ECL items in this folder IFolderContent items = _eclContentLibraryContext.GetFolderContent(eclUri, 0, EclItemTypes.File); IList <string> ids = items.ChildItems.Select(item => ImportSingleItem(item.Id)).ToList(); return(string.Join(",", ids.ToArray())); } return(ImportSingleItem(eclUri)); } } } throw new FaultException(string.Format("{0} does not exist.", eclOrTcmUri)); }
internal EclProcessor(Engine engine, Tridion.ContentManager.TcmUri binariesStructureGroupId) { _engine = engine; _binariesStructureGroup = (binariesStructureGroupId == null) ? null : (StructureGroup)engine.GetObject(binariesStructureGroupId); _eclSession = SessionFactory.CreateEclSession(engine.GetSession()); }
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); }
/// <summary> /// Constructor. /// </summary> /// <param name="pipeline">The context Model Builder Pipeline.</param> internal ExternalContentLibrary(DataModelBuilderPipeline pipeline) { _pipeline = pipeline; _eclSession = SessionFactory.CreateEclSession(pipeline.Session); }
private void SetOrUpdateMetadata(Component subject, EventArgs args, EventPhases phase) { // quick first test for ECL stub Component if (!subject.Title.StartsWith("ecl:") || subject.ComponentType != ComponentType.Multimedia) { return; } using (IEclSession eclSession = SessionFactory.CreateEclSession(subject.Session)) { // determine if subject is an ECL stub Component from the list of available mountpoints IEclUri eclUri = eclSession.TryGetEclUriFromTcmUri(subject.Id); if (eclUri != null && MountPointIds.Contains(eclUri.MountPointId)) { // check if metadata field exists ItemFields metadataFields = new ItemFields(subject.Metadata, subject.MetadataSchema); if (metadataFields.Contains(_metadataXmlFieldName)) { // only set value when update is true or metadata is not set string metadata = ((SingleLineTextField)metadataFields[_metadataXmlFieldName]).Value; if (_update || string.IsNullOrEmpty(metadata)) { using (IContentLibraryContext context = eclSession.GetContentLibrary(eclUri)) { // load actual ECL item so you can access its properties and metadata IContentLibraryMultimediaItem eclItem = (IContentLibraryMultimediaItem)context.GetItem(eclUri); try { // implement your custom code here to set the metadata value // currently this reads the configured ECL metadata field and sets its value in the stub metadata if (!string.IsNullOrEmpty(eclItem.MetadataXml)) { XNamespace ns = GetNamespace(eclItem.MetadataXml); XDocument eclMetadata = XDocument.Parse(eclItem.MetadataXml); XElement field = (from xml in eclMetadata.Descendants(ns + _metadataXmlFieldName) select xml).FirstOrDefault(); if (field != null) { string value = field.Value; // only save value when metadata is empty or update is true and value differs if (string.IsNullOrEmpty(metadata) || (_update && !metadata.Equals(value))) { // update metadata if (_asynchronous) { subject.CheckOut(); } ((SingleLineTextField)metadataFields[_metadataXmlFieldName]).Value = value; subject.Metadata = metadataFields.ToXml(); subject.Save(); if (_asynchronous) { subject.CheckIn(); } Logger.Write(string.Format("added {0} to metadata of {1}", value, eclUri), "EclStubComponentEventHandlerExtension", LoggingCategory.General, TraceEventType.Information); } } } } catch (Exception e) { Logger.Write(e, "EclStubComponentEventHandlerExtension", LoggingCategory.General); } } } } } } }