Пример #1
0
        public static TridionObjectInfo GetTridionObject(ICoreServiceFrameworkContext coreService, ItemType itemtype, string parentTcmUri, string searchtext)
        {
            TridionObjectInfo tridionObjectInfo = new TridionObjectInfo();

            try
            {
                IdentifiableObjectData tridionObject = null;
                var filter = new OrganizationalItemItemsFilterData
                {
                    Recursive = true,
                    ItemTypes = new ItemType[] { itemtype }
                };
                var pageList = coreService.Client.GetListXml(parentTcmUri, filter);

                int objectCount = (from e in pageList.Elements()
                                   where e.Attribute("Title").Value.ToLower().Equals(searchtext.ToLower()) || e.Attribute("ID").Value.ToLower() == searchtext.ToLower()
                                   select e.Attribute("ID").Value).Count();

                tridionObjectInfo.ObjectCount = objectCount;
                if (objectCount > 0)
                {
                    var objectUri = (from e in pageList.Elements()
                                     where e.Attribute("Title").Value.ToLower().Equals(searchtext.ToLower()) || e.Attribute("ID").Value.ToLower() == searchtext.ToLower()
                                     select e.Attribute("ID").Value).First();

                    tridionObject = coreService.Client.Read(objectUri, new ReadOptions
                    {
                        LoadFlags = LoadFlags.None
                    });
                    tridionObjectInfo.TridionObject = tridionObject;
                    tridionObjectInfo.TcmUri        = objectUri;
                }
            }
            catch (Exception ex)
            {
            }
            return(tridionObjectInfo);
        }
        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("");
            }
        }