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(""); } }
public static ComponentData GetNewComponent(string folderUri, string schemaUri, helper.SchemaType schemaType, string title = null) { return(new ComponentData { LocationInfo = new LocationInfo { OrganizationalItem = new LinkToOrganizationalItemData { IdRef = folderUri // WebDavUrl = folderUri }, }, ComponentType = schemaType == helper.SchemaType.Multimedia ? ComponentType.Multimedia : ComponentType.Normal, Title = title, Schema = new LinkToSchemaData { IdRef = schemaUri //schemaData.IdRef //WebDavUrl = schemaUri }, IsBasedOnMandatorySchema = false, IsBasedOnTridionWebSchema = true, ApprovalStatus = new LinkToApprovalStatusData { IdRef = "tcm:0-0-0" }, Id = "tcm:0-0-0" }); }