Пример #1
0
        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);
        }
Пример #2
0
        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);
                                }
                            }
                        }
                    }
                }
            }
        }