コード例 #1
0
        public static string GenerateComponent(ICoreServiceFrameworkContext coreService, string xml, string schemaID, helper.SchemaType schemaType, string folderUri, string ext_Id, string ext_Name)
        {
            try
            {
                string          Title        = string.Empty;
                string          Tcmuri       = string.Empty;
                string          ReturnTcmuri = string.Empty;
                SearchQueryData filter       = new SearchQueryData();
                filter.FullTextQuery = "title";
                filter.ItemTypes     = new ItemType[] { ItemType.Component };

                BasedOnSchemaData basedSchema = new BasedOnSchemaData();
                basedSchema.Schema = new LinkToSchemaData()
                {
                    IdRef = schemaID
                };
                basedSchema.Field      = "title";
                basedSchema.FieldValue = ext_Id;


                filter.BasedOnSchemas = new BasedOnSchemaData[] { basedSchema };
                XElement results = coreService.Client.GetSearchResultsXml(filter);
                for (IEnumerator <XElement> e = results.Descendants().GetEnumerator(); e.MoveNext();)
                {
                    Title  = e.Current.Attribute(XName.Get("Title")).Value;
                    Tcmuri = e.Current.FirstAttribute.Value != null ? e.Current.FirstAttribute.Value : null;
                }
                Title = Title != string.Empty ? Title : ext_Name;
                ComponentData componentData = GetNewComponent(folderUri, schemaID, schemaType, Title);

                componentData.ComponentType = ComponentType.Normal;
                SchemaData sd = coreService.Client.Read(schemaID, null) as SchemaData;

                var content = XElement.Parse(xml);
                var xmlns   = UpdateNodesWithDefaultNamespace(content.ToString(), "xmlns=" + "\"" + @"" + sd.NamespaceUri.ToString() + @"""");

                componentData.Content = xmlns.ToString().Replace("Article", "Content");
                TridionObjectInfo tridionObjectInfo = helper.GetTridionObject(coreService, ItemType.Component, folderUri, Title);
                if (tridionObjectInfo.TcmUri != null)
                {
                    componentData.Id = tridionObjectInfo.TcmUri;
                    var data = (ComponentData)coreService.Client.Read(tridionObjectInfo.TcmUri, new ReadOptions());
                    componentData = (ComponentData)coreService.Client.Update(componentData, new ReadOptions());

                    ReturnTcmuri = componentData.Id.ToString();
                }
                else
                {
                    componentData = (ComponentData)coreService.Client.Create(componentData, new ReadOptions());

                    ReturnTcmuri = componentData.Id.ToString();
                }
                return(ReturnTcmuri);
            }
            catch (Exception ex)
            {
                return("");
            }
        }
コード例 #2
0
        public static string GenerateComponent(ICoreServiceFrameworkContext coreService, string xml, string schemaID, EnumType.SchemaType schemaType, string folderUri, string title, string ext_Name)
        {
            try
            {
                string          Title  = string.Empty;
                string          Tcmuri = string.Empty;
                SearchQueryData filter = new SearchQueryData();
                filter.FullTextQuery = "title";
                filter.ItemTypes     = new ItemType[] { ItemType.Component };

                BasedOnSchemaData basedSchema = new BasedOnSchemaData();
                basedSchema.Schema = new LinkToSchemaData()
                {
                    IdRef = schemaID
                };
                basedSchema.Field      = "title";
                basedSchema.FieldValue = title;


                filter.BasedOnSchemas = new BasedOnSchemaData[] { basedSchema };
                XElement results = coreService.Client.GetSearchResultsXml(filter);
                for (IEnumerator <XElement> e = results.Descendants().GetEnumerator(); e.MoveNext();)
                {
                    Title  = e.Current.Attribute(XName.Get("Title")).Value;
                    Tcmuri = e.Current.FirstAttribute.Value != null ? e.Current.FirstAttribute.Value : null;
                }
                Title = Title != string.Empty ? Title : title;
                ComponentData componentData = GetNewComponent(folderUri, schemaID, schemaType, Title);



                componentData.ComponentType = ComponentType.Normal;
                SchemaData sd = coreService.Client.Read(schemaID, null) as SchemaData;

                var content = XElement.Parse(xml);
                var xmlns   = content;//UpdateNodesWithDefaultNamespace(content.ToString(), "xmlns=" + "\"" + @"" + sd.NamespaceUri.ToString() + @"""");

                componentData.Content = xmlns.ToString();


                TridionObjectInfo tridionObjectInfo = Helper.GetTridionObject(coreService, ItemType.Component, folderUri, Title);
                if (tridionObjectInfo.TcmUri != null)
                {
                    componentData.Id = tridionObjectInfo.TcmUri;
                    componentData    = (ComponentData)(coreService.Client.SynchronizeWithSchema(
                                                           componentData,
                                                           new SynchronizeOptions
                    {
                        SynchronizeFlags = SynchronizeFlags.All
                    }
                                                           ).SynchronizedItem);
                    //var data = (ComponentData)coreService.Client.Read(tridionObjectInfo.TcmUri, new ReadOptions());
                    // var component = (ComponentData)coreService.Client.Read(tridionObjectInfo.TcmUri, new ReadOptions());
                    //SchemaFieldsData schemaFieldData = coreService.Client.ReadSchemaFields(schemaID, true, new ReadOptions());
                    //Fields fields = Fields.ForContentOf(schemaFieldData);
                    //fields = Fields.ForMetadataOf(schemaFieldData, data);
                    //fields["title"].Value = "Value of MetadataField";
                    //fields["para"]["subheading"].Value = "Value of MetadataField";
                    //componentData.Metadata = fields.ToString();
                    componentData = (ComponentData)coreService.Client.Update(componentData, new ReadOptions());
                }
                else
                {
                    componentData = (ComponentData)(coreService.Client.SynchronizeWithSchema(
                                                        componentData,
                                                        new SynchronizeOptions
                    {
                        SynchronizeFlags = SynchronizeFlags.All
                    }
                                                        ).SynchronizedItem);
                    componentData = (ComponentData)coreService.Client.Create(componentData, new ReadOptions());
                }
                return(componentData.Id.ToString());
            }
            catch (Exception ex)
            {
                return("");
            }
        }