public void TestNoExceptionOnRenderingComponents() { string componentsOrgItemId = TestContext.Properties["ComponentsOrgItemId"].ToString(); if (!String.IsNullOrEmpty(componentsOrgItemId)) { OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData(); filter.ItemTypes = new ItemType[] { ItemType.Component }; filter.Recursive = true; foreach (XElement element in Client.GetListXml(componentsOrgItemId, filter).Nodes()) { ComponentData component = Client.Read(element.Attribute("ID").Value, null) as ComponentData; this.LogMessage(String.Format("Running template regression tests for component \"{0}\" ({1})", component.Title, component.Id), true); foreach (XElement componentTemplateElement in GetUsingComponentTemplates(component)) { ComponentTemplateData componentTemplate = Client.Read(componentTemplateElement.Attribute("ID").Value, null) as ComponentTemplateData; // Render the component with the component template this.AssertItemRendersWithoutException(component, componentTemplate); } this.renderedTemplates.Clear(); this.LogMessage("PASSED! :)"); } } }
public static IEnumerable <string> ItemURIsInOrganizationalItem(this CoreServiceClient client, string organizationalItemId) { var filter = new OrganizationalItemItemsFilterData(); var comps = client.GetListXml(organizationalItemId, filter); return(comps.Descendants().Select(itemElement => itemElement.Attribute("ID").Value)); }
private static ItemsFilterData GetFilter(GetCountParameters parameters) { ItemsFilterData filter; if (parameters.OrganizationalItemId.EndsWith("-1")) // is Publication { filter = new RepositoryItemsFilterData(); } else // is Folder or Structure Group { filter = new OrganizationalItemItemsFilterData(); } filter.Recursive = true; List<ItemType> itemTypesList = new List<ItemType>(); if (parameters.CountFolders) { itemTypesList.Add(ItemType.Folder); } if (parameters.CountComponents) { itemTypesList.Add(ItemType.Component); } if (parameters.CountSchemas) { itemTypesList.Add(ItemType.Schema); } if (parameters.CountComponentTemplates) { itemTypesList.Add(ItemType.ComponentTemplate); } if (parameters.CountPageTemplates) { itemTypesList.Add(ItemType.PageTemplate); } if (parameters.CountTemplateBuildingBlocks) { itemTypesList.Add(ItemType.TemplateBuildingBlock); } if (parameters.CountStructureGroups) { itemTypesList.Add(ItemType.StructureGroup); } if (parameters.CountPages) { itemTypesList.Add(ItemType.Page); } if (parameters.CountCategories) { itemTypesList.Add(ItemType.Category); } if (parameters.CountKeywords) { itemTypesList.Add(ItemType.Keyword); } filter.ItemTypes = itemTypesList.ToArray(); return filter; }
internal string GetComponentTemplateForSchema(string schemaId, string componentTemplateTitle, bool dynamic = false) { string container = GetUriInBlueprintContext(Configuration.TemplateFolderId, Configuration.TemplatePublicationId); OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData(); filter.ItemTypes = new[] { ItemType.ComponentTemplate }; foreach (XElement node in _client.GetListXml(container, filter).Nodes()) { if (node.Attribute("Title").Value.Equals(componentTemplateTitle)) { return(node.Attribute("ID").Value); } } ComponentTemplateData ct = (ComponentTemplateData)_client.GetDefaultData(ItemType.ComponentTemplate, container); ct.Title = componentTemplateTitle; List <LinkToSchemaData> schemaLinks = new List <LinkToSchemaData>(); schemaLinks.Add(new LinkToSchemaData { IdRef = GetUriInBlueprintContext(schemaId, Configuration.TemplatePublicationId) }); ct.RelatedSchemas = schemaLinks.ToArray(); ct.IsRepositoryPublishable = dynamic; ct = (ComponentTemplateData)_client.Save(ct, _readOptions); _client.CheckIn(ct.Id, null); return(ct.Id); }
public List <Person> GetPersons(bool refresh = false) { Stopwatch watch = new Stopwatch(); watch.Start(); //Console.WriteLine("Getting List of people..."); if (refresh) { _persons = null; } if (_persons == null) { _persons = new List <Person>(); //Folder people = (Folder)_session.GetObject(Constants.PersonLocationUrl); OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData { ItemTypes = new[] { ItemType.Component } }; foreach (XElement node in _client.GetListXml(Constants.PersonLocationUrl, filter).Nodes()) { _persons.Add(new Person((ComponentData)_client.Read(node.Attribute("ID").Value, _readOptions), _client)); } } watch.Stop(); //Console.WriteLine("Returning " + _persons.Count + " authors in " + watch.ElapsedMilliseconds + " milliseconds"); return(_persons); }
public ActionResult Index(FindAndReplaceViewModel model) { model.ServerErrorMessage = string.Empty; if (ModelState.IsValid) { ReadOptions ro = new ReadOptions { LoadFlags = LoadFlags.None }; OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData() { ItemTypes = new[] { ItemType.Component }, ComponentTypes = new[] { ComponentType.Normal }, Recursive = true, }; try { XElement xmlList = Client.GetListXml(model.FolderId, filter); // Load all components based on the folderId List <ComponentData> componentsList = (from itemId in xmlList.Elements().Attributes(XName.Get("ID", String.Empty)) select(ComponentData) Client.Read(itemId.Value, ro)).ToList <ComponentData>(); model.ComponentIdsMatchedListForFindAndReplace = new List <Component>(); foreach (var component in componentsList.Where(x => x.Content != "" && x.BluePrintInfo.IsShared == false && x.ComponentType.Value != ComponentType.Multimedia)) { if (FindText(component.Content, model.SearchText, model.Matchcase)) { log.Info(string.Format("Find and Replace Text Match component ID {0}, Title {1}", component.Id, component.Title)); model.ComponentIdsMatchedListForFindAndReplace.Add(new Component { Id = component.Id, Title = component.Title }); } } log.Info(string.Format("No of matched components {0}", model.ComponentIdsMatchedListForFindAndReplace.Count())); Session["ComponentsListReplace"] = model.ComponentIdsMatchedListForFindAndReplace; Session["SearchText"] = model.SearchText; Session["ReplaceText"] = model.ReplaceText; Session["Matchcase"] = model.Matchcase; model.IsSuccess = true; model.IsReplaceCompleted = false; this.SetModel <FindAndReplaceViewModel>(model); return(Redirect(Request.Url.AbsolutePath)); } catch (Exception ex) { //model.ServerErrorMessage = "Error processing the items..."; log.Error("Index - Find and Replace Post Method Error", ex); } } this.SetModel <FindAndReplaceViewModel>(model); return(Redirect(Request.Url.PathAndQuery)); }
public static XElement GetListXml(this TcmUri tcmUri, ItemType[] itemTypes, bool recursive) { var filter = new OrganizationalItemItemsFilterData { ItemTypes = itemTypes, Recursive = recursive }; return(Wrapper.Instance.GetListXml(tcmUri.ToString(), filter)); }
public IEnumerable <TemplateData> FindTemplatesForFolder(string folderId, bool pageTemplatesOnly = false) { log.Info("Called FindTemplatesForFolder with folderId " + folderId); OrganizationalItemItemsFilterData filterData = new OrganizationalItemItemsFilterData(); filterData.ItemTypes = pageTemplatesOnly ? new[] { ItemType.PageTemplate } : new[] { ItemType.PageTemplate, ItemType.ComponentTemplate }; filterData.Recursive = true; XElement resultXml = Client.GetListXml(folderId, filterData); return(GetConvertibleTemplates(resultXml)); }
public List <string> GetExistingKeywords(bool refresh = false) { if (_keywords == null || refresh) { _keywords = new List <string>(); OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData(); filter.ItemTypes = new[] { ItemType.Keyword }; foreach (XElement node in _client.GetListXml(ResolveUrl(Constants.ContentCategoryUrl), filter).Nodes()) { _keywords.Add(node.Attribute("Title").Value); } } return(_keywords); }
//public List<Page> GetNewsletterPages() //{ // List<Page> result = new List<Page>(); // StructureGroup sg = (StructureGroup)_session.GetObject(Constants.NewsletterStructureGroupUrl); // OrganizationalItemItemsFilter filter = new OrganizationalItemItemsFilter(_session) { ItemTypes = new[] { ItemType.Page } }; // foreach (Page page in sg.GetItems(filter)) // { // result.Add(page); // } // return result; //} public List <Article> GetArticlesForDate(DateTime date) { List <Article> result = new List <Article>(); string folderId = GetFolderForDate(date); OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData { ItemTypes = new[] { ItemType.Component } }; foreach (XElement node in _client.GetListXml(folderId, filter).Nodes()) { result.Add(new Article((ComponentData)_client.Read(node.Attribute("ID").Value, _readOptions), _client)); } return(result); }
public string GetListOfAllFolder(string tcmuri) { try { coreService = CoreServiceFactory.GetCoreServiceContext(new Uri(ConfigurationManager.AppSettings["CoreServiceURL"].ToString()), new NetworkCredential(ConfigurationManager.AppSettings["UserName"].ToString(), ConfigurationManager.AppSettings["Password"].ToString(), ConfigurationManager.AppSettings["Domain"].ToString())); string output = string.Empty; var rootFolderUri = "tcm:" + tcmuri; OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData(); var listXml = coreService.Client.GetListXml(rootFolderUri, filter); return(listXml.ToString()); } catch (Exception ex) { return(ex.Message.ToString()); } }
public HashSet <string> getComponentTitles(string folderUri) { OrganizationalItemItemsFilterData filterData = new OrganizationalItemItemsFilterData(); filterData.ItemTypes = new[] { ItemType.Component }; filterData.BaseColumns = ListBaseColumns.IdAndTitle; var result = client.GetListXml(folderUri, filterData); if (result != null && result.Descendants(TridionNamespaceManager.Tcm + "Item").Count() > 0) { return(result.Descendants(TridionNamespaceManager.Tcm + "Item") .Select(item => item.Attribute("Title").Value).ToHashSet()); } return(new HashSet <string>()); }
public static IEnumerable <string> ComponentURIsInFolder(this CoreServiceClient client, string folderId, string schemaId = null) { var filter = new OrganizationalItemItemsFilterData(); filter.ItemTypes = new ItemType[] { ItemType.Component }; if (schemaId != null) { filter.BasedOnSchemas = new[] { new LinkToSchemaData { IdRef = schemaId } } } ; var comps = client.GetListXml(folderId, filter); return(comps.Descendants().Select(itemElement => itemElement.Attribute("ID").Value)); }
/// <summary> /// Sets up the component filter /// </summary> /// <param>none</param> /// <returns></returns> private static OrganizationalItemItemsFilterData SetComponenetFilterCriterias(bool ismultimediaComp) { OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData(); filter.ItemTypes = new ItemType[] { ItemType.Component }; //if multimedia comoponent if (ismultimediaComp) { filter.ComponentTypes = new ComponentType[] { ComponentType.Multimedia } } ; filter.Recursive = true; return(filter); } }
public static IEnumerable <XElement> ItemsInFolder(this CoreServiceClient client, string folderId, string schemaId = null) { var filter = new OrganizationalItemItemsFilterData { ItemTypes = new [] { ItemType.Component } }; if (schemaId != null) { filter.BasedOnSchemas = new[] { new LinkToSchemaData { IdRef = schemaId } } } ; var comps = client.GetListXml(folderId, filter); return(comps.Descendants()); }
public IList<ViewModel> CoreCreateModels(string tcmId) { IList<ViewModel> result = new List<ViewModel>(); var client = GetClient(); OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData() { ItemTypes = new[] { ItemType.Schema }, Recursive = true, SchemaPurposes = new[] { SchemaPurpose.Component, SchemaPurpose.Embedded } //Filter doesn't work. Schema Purpose is always null }; try { if (!client.IsExistingObject(tcmId)) { throw new ArgumentException( String.Format("Could not find Tridion Organizational Item with ID {0}.", tcmId), "tcmId"); } else { //Nearly identical to CS 2012 but Tridion.ContentManager.CoreService.Client is not the same between 2011 and 2012 so code is not interchangeable without specifying namespace on everything XNode[] schemas = client.GetListXml(tcmId, filter).Nodes().ToArray(); foreach (XElement xSchema in client.GetListXml(tcmId, filter).Nodes()) { SchemaData schema = client.Read(xSchema.Attribute("ID").Value, expandedOptions) as SchemaData; var model = CoreCreateModel(schema, client); if (model != null) result.Add(model); } } } finally { //Cannot dispose of Client with "using" statement if a FaultException occurs. Implementation of IDisposable is flawed in ClientBase if (client.State == CommunicationState.Faulted) { client.Abort(); } if (client.State != CommunicationState.Closed) { client.Close(); } client = null; } return result; }
public IEnumerable <SchemaData> FindSchemasForFolder(string folderId) { log.Info("Called FindSchemasForFolder with folderId " + folderId); OrganizationalItemItemsFilterData filterData = new OrganizationalItemItemsFilterData(); filterData.ItemTypes = new[] { ItemType.Schema }; filterData.Recursive = true; XElement resultXml = Client.GetListXml(folderId, filterData); IList <SchemaData> convertibleSchemas = GetConvertibleSchemas(resultXml); for (var i = 0; i < convertibleSchemas.Count; i++) // using a for-loop (not a foreach) because the collection will be modified { var schema = convertibleSchemas[i]; AddUsedSchemas(schema, convertibleSchemas); } return(convertibleSchemas); }
public string GetKeywordByCategory(string tcmuri) { string xmljson = "xml"; try { coreService = CoreServiceFactory.GetCoreServiceContext(new Uri(ConfigurationManager.AppSettings["CoreServiceURL"].ToString()), new NetworkCredential(ConfigurationManager.AppSettings["UserName"].ToString(), ConfigurationManager.AppSettings["Password"].ToString(), ConfigurationManager.AppSettings["Domain"].ToString())); var filter = new OrganizationalItemItemsFilterData(); var category = "tcm:" + tcmuri; var keywords = coreService.Client.GetListXml(category, filter); return(xmljson.ToString().ToLower() == "json" ? JsonConvert.SerializeObject(keywords) : keywords.ToString()); } catch (Exception ex) { return(ex.Message.ToString()); } }
internal List <Source> GetListSources() { OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData { ItemTypes = new[] { ItemType .Component } }; List <Source> sources = new List <Source>(); foreach (XElement node in _client.GetListXml(Configuration.InformationSourceFolderId, filter).Nodes()) { ComponentData component = (ComponentData)_client.Read(node.Attribute("ID").Value, _readOptions); sources.Add(new Source(component, _client)); } return(sources); }
static void ForEachApplicationData(ICoreService client, string itemUri, ProcessApplicationData callback) { foreach (var appData in client.ReadAllApplicationData(itemUri)) { callback(itemUri, appData, client); } var context = client.Read(itemUri, DEFAULT_READ_OPTIONS); if (context is OrganizationalItemData) // TODO: also handle Publication { var filter = new OrganizationalItemItemsFilterData(); //filter.Recursive = true; // faster, but order is undefined var items = client.GetListXml(itemUri, filter).Elements(TRIDION_NAMESPACE + "Item"); foreach (var element in items) { var childUri = element.Attribute("ID").Value; ForEachApplicationData(client, childUri, callback); } } }
/// <summary> /// GetFilter - Creates a ItemFilter which is used to obtain a list of publishable items. /// </summary> /// <param name="parameters">PagePublisherParameters</param> /// <returns>ItemsFilterData object</returns> private ItemsFilterData GetFilter(PagePublisherParameters parameters) { ItemsFilterData filter = null; if (parameters.LocationId.EndsWith("-1")) // is Publication { filter = new RepositoryItemsFilterData(); } else // is Folder or Structure Group { filter = new OrganizationalItemItemsFilterData(); } filter.Recursive = parameters.Recursive; List <ItemType> itemTypesList = new List <ItemType>(); itemTypesList.Add(ItemType.Page); filter.ItemTypes = itemTypesList.ToArray(); return(filter); }
internal string GetFolder(string folderTitle, string parentFolderId) { //Console.WriteLine("Searching for folder with name " + folderTitle + " in folder " + parentFolderId); Stopwatch watch = new Stopwatch(); watch.Start(); string folderId = TcmUri.UriNull; OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData { ItemTypes = new[] { ItemType .Folder } }; foreach (XElement node in _client.GetListXml(parentFolderId, filter).Nodes()) { if (!node.Attribute("Title").Value.Equals(folderTitle)) { continue; } folderId = node.Attribute("ID").Value; Console.WriteLine("Found folder with ID " + folderId); break; } if (folderId.Equals(TcmUri.UriNull) && CreateIfNewItem) { // New folder FolderData folder = (FolderData)_client.GetDefaultData(ItemType.Folder, parentFolderId); //(FolderData)_client.GetDefaultData(ItemType.Folder, parentFolderId, _readOptions); folder.Title = folderTitle; folder = (FolderData)_client.Save(folder, _readOptions); folderId = folder.Id; Console.WriteLine("Created folder with ID " + folderId); } watch.Stop(); Console.WriteLine("GetFolder finished in " + watch.ElapsedMilliseconds + " milliseconds."); return(folderId); }
internal string GetSchema(string schemaTitle, string parentFolderId, string xsd = null, SchemaPurpose purpose = SchemaPurpose.UnknownByClient, string rootElementName = "Content") { Stopwatch watch = new Stopwatch(); watch.Start(); Console.WriteLine("Creating schema " + schemaTitle); string schemaId = TcmUri.UriNull; OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData { ItemTypes = new[] { ItemType .Schema } }; foreach (XElement node in _client.GetListXml(parentFolderId, filter).Nodes()) { if (!node.Attribute("Title").Value.Equals(schemaTitle)) { continue; } schemaId = node.Attribute("ID").Value; } if (schemaId.Equals(TcmUri.UriNull) && xsd != null && CreateIfNewItem && purpose != SchemaPurpose.UnknownByClient) { SchemaData schema = (SchemaData)_client.GetDefaultData(ItemType.Schema, parentFolderId); schema.Title = schemaTitle; schema.Description = schemaTitle; schema.Purpose = purpose; schema.RootElementName = rootElementName; schema.Xsd = xsd; schema = (SchemaData)_client.Save(schema, _readOptions); schema = (SchemaData)_client.CheckIn(schema.Id, _readOptions); schemaId = schema.Id; } watch.Stop(); Console.WriteLine("Returning Schema ID " + schemaId + " (" + watch.ElapsedMilliseconds + " milliseconds)"); return(schemaId); }
public string GetStructureGroup(string sgTitle, string parentStructureGroup) { OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData(); filter.ItemTypes = new[] { ItemType.StructureGroup }; foreach (XElement node in _client.GetListXml(parentStructureGroup, filter).Nodes()) { if (node.Attribute("Title").Value.Equals(sgTitle)) { return(node.Attribute("ID").Value); } } StructureGroupData sg = (StructureGroupData) _client.GetDefaultData(ItemType.StructureGroup, parentStructureGroup); sg.Title = sgTitle; sg.Directory = Regex.Replace(sgTitle, "\\W", "").ToLowerInvariant().Replace("á", "a").Replace("ó", "o"); sg = (StructureGroupData)_client.Save(sg, _readOptions); return(sg.Id); }
public string GetPage(string sg, string pageTitle) { OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData { ItemTypes = new[] { ItemType.Page } }; foreach (XElement node in _client.GetListXml(sg, filter).Nodes()) { if (node.Attribute("Title").Value.Equals(pageTitle)) { return(node.Attribute("ID").Value); } } PageData page = (PageData)_client.GetDefaultData(ItemType.Page, sg); page.Title = pageTitle; page.FileName = Regex.Replace(pageTitle, "\\W", "").ToLowerInvariant(); page = (PageData)_client.Save(page, _readOptions); _client.CheckIn(page.Id, null); return(GetVersionlessUri(page.Id)); }
public List <Source> GetSources() { Stopwatch watch = new Stopwatch(); watch.Start(); //Console.WriteLine("Getting list of sources..."); if (_sources == null) { _sources = new List <Source>(); //Folder sourceFolder = (Folder)_session.GetObject(Constants.SourceLocationUrl); OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData { ItemTypes = new[] { ItemType.Component } }; foreach (XElement node in _client.GetListXml(Constants.SourceLocationUrl, filter).Nodes()) { _sources.Add(new Source((ComponentData)_client.Read(node.Attribute("ID").Value, _readOptions), _client)); } } watch.Stop(); //Console.WriteLine("Returning " + _sources.Count + " sources in " + watch.ElapsedMilliseconds + " milliseconds"); return(_sources); }
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 void TestNoExceptionOnRenderingPages() { string pagesOrgItemId = TestContext.Properties["PagesOrgItemId"].ToString(); if (!String.IsNullOrEmpty(pagesOrgItemId)) { OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData(); filter.ItemTypes = new ItemType[] { ItemType.Page }; filter.Recursive = true; foreach (XElement element in Client.GetListXml(pagesOrgItemId, filter).Nodes()) { PageData page = Client.Read(element.Attribute("ID").Value, null) as PageData; PageTemplateData pageTemplate = Client.Read(page.PageTemplate.IdRef, null) as PageTemplateData; this.LogMessage(String.Format("Running template regression tests for page \"{0}\" ({1})", page.Title, page.Id), true); // Render the page with the page template this.AssertItemRendersWithoutException(page, pageTemplate); this.LogMessage("PASSED! :)"); } this.renderedTemplates.Clear(); } }
public static string GetItemTcmId(MappingInfo mapping, string tcmContainer, string itemTitle) { if (String.IsNullOrEmpty(tcmContainer)) return String.Empty; if (!EnsureValidClient(mapping)) return String.Empty; OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData(); foreach (XElement element in Client.GetListXml(tcmContainer, filter).Nodes()) { if (element.Attribute("Title").Value == itemTitle) return element.Attribute("ID").Value; } return String.Empty; }
public static bool ExistsItem(MappingInfo mapping, string tcmContainer, string itemTitle) { if (String.IsNullOrEmpty(tcmContainer)) return false; if (!EnsureValidClient(mapping)) return false; OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData(); return Client.GetList(tcmContainer, filter).Any(x => x.Title == itemTitle); }
private static void PurgeFolders(CoreServiceClient client, string folders, PurgeOldVersionsInstructionData purgeIntructions, UInt16 versionsToKeep, string pubUri) { int totalCompVersionsRemoved = 0; string[] folderUris = folders.Split(','); List <LinkToIdentifiableObjectData> itemsToPurge = new List <LinkToIdentifiableObjectData>(); for (int i = 0; i < folderUris.Length; i++) { FolderData subFolder = null; // Add sub and subsub-folders to list to prevent timeouts... try { string localUri = GetLocalUri(pubUri.Trim(), folderUris[i]); subFolder = (FolderData)client.Read(localUri, null); } catch (Exception ex) { continue; } LinkToIdentifiableObjectData subfolderLink = new LinkToIdentifiableObjectData(); subfolderLink.IdRef = subFolder.Id; itemsToPurge.Add(subfolderLink); var itemTypes = new List <ItemType>(); //itemTypes.Add(ItemType.Component); itemTypes.Add(ItemType.Folder); var filter = new OrganizationalItemItemsFilterData(); filter.Recursive = true; filter.ItemTypes = itemTypes.ToArray(); purgeIntructions.VersionsToKeep = versionsToKeep; purgeIntructions.Recursive = false; IdentifiableObjectData[] allSubFolders = client.GetList(subFolder.Id, filter); foreach (var aFolder in allSubFolders) { LinkToIdentifiableObjectData folderLink = new LinkToIdentifiableObjectData(); folderLink.IdRef = aFolder.Id; itemsToPurge.Add(folderLink); purgeIntructions.Containers = itemsToPurge.ToArray(); try { int versionsCleaned = client.PurgeOldVersions(purgeIntructions); totalCompVersionsRemoved = totalCompVersionsRemoved + versionsCleaned; WriteOutput("Folder " + folderLink.IdRef + " removed " + versionsCleaned.ToString()); } catch (Exception ex) { throw; } itemsToPurge.Clear(); } } WriteOutput("Total Component versions removed is " + totalCompVersionsRemoved.ToString()); }
private static void PurgeStructureGroups(CoreServiceClient client, string structureGroups, PurgeOldVersionsInstructionData purgeIntructions, UInt16 versionsToKeep, string pubUri) { int totalPageVersionsRemoved = 0; string[] structureGroupUris = structureGroups.Split(','); List <LinkToIdentifiableObjectData> itemsToPurge = new List <LinkToIdentifiableObjectData>(); for (int i = 0; i < structureGroupUris.Length; i++) { StructureGroupData structureGroup = null; try { string localUri = GetLocalUri(pubUri.Trim(), structureGroupUris[i]); // Add sub and subsub-folders to list to prevent timeouts... structureGroup = (StructureGroupData)client.Read(localUri, null); } catch (Exception ex) { continue; } LinkToIdentifiableObjectData subfolderLink = new LinkToIdentifiableObjectData(); subfolderLink.IdRef = structureGroup.Id; itemsToPurge.Add(subfolderLink); var itemTypes = new List <ItemType>(); itemTypes.Add(ItemType.StructureGroup); var filter = new OrganizationalItemItemsFilterData(); filter.Recursive = true; filter.ItemTypes = itemTypes.ToArray(); purgeIntructions.VersionsToKeep = versionsToKeep; purgeIntructions.Recursive = false; IdentifiableObjectData[] subSGs = client.GetList(structureGroup.Id, filter); foreach (var subSubSG in subSGs) { LinkToIdentifiableObjectData structureGroupLink = new LinkToIdentifiableObjectData(); structureGroupLink.IdRef = subSubSG.Id; itemsToPurge.Add(structureGroupLink); purgeIntructions.Containers = itemsToPurge.ToArray(); try { int versionsCleaned = client.PurgeOldVersions(purgeIntructions); totalPageVersionsRemoved = totalPageVersionsRemoved + versionsCleaned; WriteOutput("Removed " + versionsCleaned.ToString() + " Page Versions"); } catch (Exception ex) { throw; } itemsToPurge.Clear(); } } WriteOutput("Total Page versions removed is " + totalPageVersionsRemoved.ToString()); }
public List<Person> GetPersons(bool refresh = false) { //Stopwatch watch = new Stopwatch(); //watch.Start(); if (refresh) _persons = null; if (_persons == null) { Console.WriteLine("Loading people..."); _persons = new List<Person>(); //Folder people = (Folder)_session.GetObject(Constants.PersonLocationUrl); OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData { ItemTypes = new[] { ItemType.Component } }; foreach (var xNode in _client.GetListXml(Constants.PersonLocationUrl, filter).Nodes()) { var node = (XElement) xNode; _persons.Add(new Person((ComponentData)_client.Read(node.Attribute("ID").Value, _readOptions), _client)); Console.Write("."); } Console.WriteLine(Environment.NewLine); } //watch.Stop(); //Console.WriteLine("Returning " + _persons.Count + " authors in " + watch.ElapsedMilliseconds + " milliseconds"); return _persons; }
public string GetFolderForDate(DateTime date) { Stopwatch watch = new Stopwatch(); watch.Start(); //Console.WriteLine("Searching folder for date " + date.ToString("yyyy-MM-dd")); //Folder start = (Folder)_session.GetObject(Constants.ArticleLocationUrl); OrganizationalItemItemsFilterData folders = new OrganizationalItemItemsFilterData { ItemTypes = new[] { ItemType.Folder } }; string year = null; string month = null; string day = null; string yearName = date.Year.ToString(CultureInfo.InvariantCulture); string monthName; if (date.Month < 10) { monthName = "0" + date.Month; } else { monthName = date.Month.ToString(CultureInfo.InvariantCulture); } monthName += " " + date.ToString("MMMM"); string dayName; if (date.Day < 10) { dayName = "0" + date.Day; } else { dayName = date.Day.ToString(CultureInfo.InvariantCulture); } foreach (XElement folderElement in _client.GetListXml(Constants.ArticleLocationUrl, folders).Nodes()) //start.GetListItems(folders)) { if (folderElement.Attribute("Title").Value.Equals(yearName)) { year = folderElement.Attribute("ID").Value; break; } } if (year == null) { FolderData f = (FolderData)_client.GetDefaultData(ItemType.Folder, Constants.ArticleLocationUrl); f.Title = yearName; f = (FolderData)_client.Save(f, _readOptions); year = f.Id; } foreach (XElement monthFolder in _client.GetListXml(year, folders).Nodes())//year.GetListItems(folders)) { if (monthFolder.Attribute("Title").Value.Equals(monthName)) { month = monthFolder.Attribute("ID").Value; break; } } if (month == null) { FolderData f = (FolderData)_client.GetDefaultData(ItemType.Folder, year); f.Title = monthName; f = (FolderData)_client.Save(f, _readOptions); month = f.Id; } foreach (XElement dayFolder in _client.GetListXml(month, folders).Nodes())//month.GetListItems(folders)) { if (dayFolder.Attribute("Title").Value.Equals(dayName)) { day = dayFolder.Attribute("ID").Value; break; } } if (day == null) { FolderData f = (FolderData)_client.GetDefaultData(ItemType.Folder, month); f.Title = dayName; f = (FolderData)_client.Save(f, _readOptions); day = f.Id; } watch.Stop(); //Console.WriteLine("Returning folder " + day + " in " + watch.ElapsedMilliseconds + " milliseconds"); return(day); }
public List<Article> GetArticlesForDate(DateTime date) { List<Article> result = new List<Article>(); string folderId = GetFolderForDate(date); OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData { ItemTypes = new[] { ItemType.Component } }; foreach (var xNode in _client.GetListXml(folderId, filter).Nodes()) { var node = (XElement) xNode; result.Add(new Article((ComponentData)_client.Read(node.Attribute("ID").Value, _readOptions), _client)); } return result; }
public string GetPage(string sg, string pageTitle) { OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData { ItemTypes = new[] { ItemType.Page } }; foreach (var xNode in _client.GetListXml(sg, filter).Nodes()) { var node = (XElement) xNode; if (node.Attribute("Title").Value.Equals(pageTitle)) return node.Attribute("ID").Value; } PageData page = (PageData)_client.GetDefaultData(ItemType.Page, sg, _readOptions); page.Title = pageTitle; page.FileName = Regex.Replace(pageTitle, "\\W", "").ToLowerInvariant(); page = (PageData)_client.Save(page, _readOptions); _client.CheckIn(page.Id, null); return GetVersionlessUri(page.Id); }
public string GetStructureGroup(string sgTitle, string parentStructureGroup) { OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData { ItemTypes = new[] {ItemType.StructureGroup} }; foreach (var xNode in _client.GetListXml(parentStructureGroup, filter).Nodes()) { var node = (XElement) xNode; if (node.Attribute("Title").Value.Equals(sgTitle)) return node.Attribute("ID").Value; } StructureGroupData sg = (StructureGroupData) _client.GetDefaultData(ItemType.StructureGroup, parentStructureGroup, _readOptions); sg.Title = sgTitle; sg.Directory = Regex.Replace(sgTitle, "\\W", "").ToLowerInvariant().Replace("á", "a").Replace("ó", "o"); sg = (StructureGroupData)_client.Save(sg, _readOptions); return sg.Id; }
public HashSet<string> getComponentTitles(string folderUri) { OrganizationalItemItemsFilterData filterData = new OrganizationalItemItemsFilterData(); filterData.ItemTypes = new[] { ItemType.Component }; filterData.BaseColumns = ListBaseColumns.IdAndTitle; var result = client.GetListXml(folderUri, filterData); if (result != null && result.Descendants(TridionNamespaceManager.Tcm + "Item").Count() > 0) return result.Descendants(TridionNamespaceManager.Tcm + "Item") .Select(item => item.Attribute("Title").Value).ToHashSet(); return new HashSet<string>(); }
public string GetFolderForDate(DateTime date) { Stopwatch watch = new Stopwatch(); watch.Start(); //Console.WriteLine("Searching folder for date " + date.ToString("yyyy-MM-dd")); //Folder start = (Folder)_session.GetObject(Constants.ArticleLocationUrl); OrganizationalItemItemsFilterData folders = new OrganizationalItemItemsFilterData { ItemTypes = new[] { ItemType.Folder } }; string year = null; string month = null; string day = null; string yearName = date.Year.ToString(CultureInfo.InvariantCulture); string monthName; if (date.Month < 10) monthName = "0" + date.Month; else monthName = date.Month.ToString(CultureInfo.InvariantCulture); monthName += " " + date.ToString("MMMM"); string dayName; if (date.Day < 10) dayName = "0" + date.Day; else dayName = date.Day.ToString(CultureInfo.InvariantCulture); foreach (var xNode in _client.GetListXml(Constants.ArticleLocationUrl, folders).Nodes()) //start.GetListItems(folders)) { var folderElement = (XElement) xNode; if (!folderElement.Attribute("Title").Value.Equals(yearName)) continue; year = folderElement.Attribute("ID").Value; break; } if (year == null) { FolderData f = (FolderData)_client.GetDefaultData(ItemType.Folder, Constants.ArticleLocationUrl, _readOptions); f.Title = yearName; f = (FolderData)_client.Save(f, _readOptions); year = f.Id; } foreach (var xNode in _client.GetListXml(year, folders).Nodes())//year.GetListItems(folders)) { var monthFolder = (XElement) xNode; if (monthFolder.Attribute("Title").Value.Equals(monthName)) { month = monthFolder.Attribute("ID").Value; break; } } if (month == null) { FolderData f = (FolderData)_client.GetDefaultData(ItemType.Folder, year, _readOptions); f.Title = monthName; f = (FolderData)_client.Save(f, _readOptions); month = f.Id; } foreach (var xNode in _client.GetListXml(month, folders).Nodes())//month.GetListItems(folders)) { var dayFolder = (XElement) xNode; if (dayFolder.Attribute("Title").Value.Equals(dayName)) { day = dayFolder.Attribute("ID").Value; break; } } if (day == null) { FolderData f = (FolderData)_client.GetDefaultData(ItemType.Folder, month, _readOptions); f.Title = dayName; f = (FolderData)_client.Save(f, _readOptions); day = f.Id; } watch.Stop(); //Console.WriteLine("Returning folder " + day + " in " + watch.ElapsedMilliseconds + " milliseconds"); return day; }
private static void PurgeFolders(CoreServiceClient client, string folders, PurgeOldVersionsInstructionData purgeIntructions, UInt16 versionsToKeep, string pubUri) { int totalCompVersionsRemoved = 0; string[] folderUris = folders.Split(','); List<LinkToIdentifiableObjectData> itemsToPurge = new List<LinkToIdentifiableObjectData>(); for (int i = 0; i < folderUris.Length; i++) { FolderData subFolder = null; // Add sub and subsub-folders to list to prevent timeouts... try { string localUri = GetLocalUri(pubUri.Trim(), folderUris[i]); subFolder = (FolderData)client.Read(localUri, null); } catch(Exception ex) { continue; } LinkToIdentifiableObjectData subfolderLink = new LinkToIdentifiableObjectData(); subfolderLink.IdRef = subFolder.Id; itemsToPurge.Add(subfolderLink); var itemTypes = new List<ItemType>(); //itemTypes.Add(ItemType.Component); itemTypes.Add(ItemType.Folder); var filter = new OrganizationalItemItemsFilterData(); filter.Recursive = true; filter.ItemTypes = itemTypes.ToArray(); purgeIntructions.VersionsToKeep = versionsToKeep; purgeIntructions.Recursive = false; IdentifiableObjectData[] allSubFolders = client.GetList(subFolder.Id, filter); foreach (var aFolder in allSubFolders) { LinkToIdentifiableObjectData folderLink = new LinkToIdentifiableObjectData(); folderLink.IdRef = aFolder.Id; itemsToPurge.Add(folderLink); purgeIntructions.Containers = itemsToPurge.ToArray(); try { int versionsCleaned = client.PurgeOldVersions(purgeIntructions); totalCompVersionsRemoved = totalCompVersionsRemoved + versionsCleaned; WriteOutput("Folder " + folderLink.IdRef + " removed " + versionsCleaned.ToString()); } catch (Exception ex) { throw; } itemsToPurge.Clear(); } } WriteOutput("Total Component versions removed is " + totalCompVersionsRemoved.ToString()); }
private static void PurgeStructureGroups(CoreServiceClient client, string structureGroups, PurgeOldVersionsInstructionData purgeIntructions, UInt16 versionsToKeep, string pubUri) { int totalPageVersionsRemoved = 0; string[] structureGroupUris = structureGroups.Split(','); List<LinkToIdentifiableObjectData> itemsToPurge = new List<LinkToIdentifiableObjectData>(); for (int i = 0; i < structureGroupUris.Length; i++) { StructureGroupData structureGroup = null; try { string localUri = GetLocalUri(pubUri.Trim(), structureGroupUris[i]); // Add sub and subsub-folders to list to prevent timeouts... structureGroup = (StructureGroupData)client.Read(localUri, null); } catch(Exception ex) { continue; } LinkToIdentifiableObjectData subfolderLink = new LinkToIdentifiableObjectData(); subfolderLink.IdRef = structureGroup.Id; itemsToPurge.Add(subfolderLink); var itemTypes = new List<ItemType>(); itemTypes.Add(ItemType.StructureGroup); var filter = new OrganizationalItemItemsFilterData(); filter.Recursive = true; filter.ItemTypes = itemTypes.ToArray(); purgeIntructions.VersionsToKeep = versionsToKeep; purgeIntructions.Recursive = false; IdentifiableObjectData[] subSGs = client.GetList(structureGroup.Id, filter); foreach (var subSubSG in subSGs) { LinkToIdentifiableObjectData structureGroupLink = new LinkToIdentifiableObjectData(); structureGroupLink.IdRef = subSubSG.Id; itemsToPurge.Add(structureGroupLink); purgeIntructions.Containers = itemsToPurge.ToArray(); try { int versionsCleaned = client.PurgeOldVersions(purgeIntructions); totalPageVersionsRemoved = totalPageVersionsRemoved + versionsCleaned; WriteOutput("Removed " + versionsCleaned.ToString() + " Page Versions"); } catch (Exception ex) { throw; } itemsToPurge.Clear(); } } WriteOutput("Total Page versions removed is " + totalPageVersionsRemoved.ToString()); }
public List<string> GetExistingKeywords(bool refresh = false) { if (_keywords == null || refresh) { _keywords = new List<string>(); OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData { ItemTypes = new[] {ItemType.Keyword} }; foreach (var xNode in _client.GetListXml(ResolveUrl(Constants.ContentCategoryUrl), filter).Nodes()) { var node = (XElement) xNode; _keywords.Add(node.Attribute("Title").Value); } } return _keywords; }
/// <summary> /// GetFilter - Creates a ItemFilter which is used to obtain a list of publishable items. /// </summary> /// <param name="parameters">PagePublisherParameters</param> /// <returns>ItemsFilterData object</returns> private ItemsFilterData GetFilter(PagePublisherParameters parameters) { ItemsFilterData filter = null; if (parameters.LocationId.EndsWith("-1")) // is Publication { filter = new RepositoryItemsFilterData(); } else // is Folder or Structure Group { filter = new OrganizationalItemItemsFilterData(); } filter.Recursive = parameters.Recursive; List<ItemType> itemTypesList = new List<ItemType>(); itemTypesList.Add(ItemType.Page); filter.ItemTypes = itemTypesList.ToArray(); return filter; }
public List<Source> GetSources() { Stopwatch watch = new Stopwatch(); watch.Start(); Console.WriteLine("Getting list of sources..."); if (_sources == null) { _sources = new List<Source>(); //Folder sourceFolder = (Folder)_session.GetObject(Constants.SourceLocationUrl); OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData { ItemTypes = new[] { ItemType.Component } }; foreach (var xNode in _client.GetListXml(Constants.SourceLocationUrl, filter).Nodes()) { var node = (XElement) xNode; _sources.Add(new Source((ComponentData)_client.Read(node.Attribute("ID").Value, _readOptions), _client)); Console.Write("."); } Console.WriteLine(Environment.NewLine); } watch.Stop(); //Console.WriteLine("Returning " + _sources.Count + " sources in " + watch.ElapsedMilliseconds + " milliseconds"); return _sources; }
static void Main(string[] args) { SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient("netTcp_2011"); ContentManager cm = new ContentManager(client); List <Source> sources = cm.GetSources(); int countSources = sources.Count; Console.WriteLine("Loaded " + countSources + " sources. Starting to process."); XmlNamespaceManager nm = new XmlNamespaceManager(new NameTable()); nm.AddNamespace("dc", "http://purl.org/dc/elements/1.1/"); Dictionary <Source, List <Article> > addedContent = new Dictionary <Source, List <Article> >(); foreach (var source in sources) { Console.WriteLine("Loading content for source " + source.Title); XmlDocument feedXml = null; WebRequest wrq = WebRequest.Create(source.RssFeedUrl); wrq.Proxy = WebRequest.DefaultWebProxy; wrq.Proxy.Credentials = CredentialCache.DefaultCredentials; XmlTextReader reader = null; try { reader = new XmlTextReader(wrq.GetResponse().GetResponseStream()); } catch (Exception ex) { Console.WriteLine("Could not read response from source" + ex.Message); } if (reader == null) { continue; } SyndicationFeed feed = SyndicationFeed.Load(reader); int countItems = feed.Items.Count(); Console.WriteLine("Loaded " + countItems + " items from source. Processing"); int count = 0; List <Article> newArticles = new List <Article>(); foreach (var item in feed.Items) { count++; Person author = null; if (item.Authors.Count == 0) { //Console.WriteLine("Could not find an author in feed source, checking for default"); if (source.DefaultAuthor != null) { author = source.DefaultAuthor; //Console.WriteLine("Using default author " + author.Name); } else { //Console.WriteLine("Could not find default author, being creative"); if (feedXml == null) { try { feedXml = new XmlDocument(); feedXml.Load(source.RssFeedUrl); }catch (Exception ex) { Console.WriteLine("Something went wrong loading " + source.RssFeedUrl); Console.WriteLine(ex.ToString()); } } if (feedXml != null) { string xpath = "/rss/channel/item[" + count + "]/dc:creator"; if (feedXml.SelectSingleNode(xpath, nm) != null) { author = cm.FindPersonByNameOrAlternate(feedXml.SelectSingleNode(xpath, nm).InnerText); if (author == null) { author = new Person(client) { Name = feedXml.SelectSingleNode(xpath, nm).InnerText }; author.Save(); author = cm.FindPersonByNameOrAlternate( feedXml.SelectSingleNode(xpath, nm).InnerText, true); } } } } } else { string nameOrAlternate; SyndicationPerson syndicationPerson = item.Authors.First(); if (string.IsNullOrEmpty(syndicationPerson.Name)) { nameOrAlternate = syndicationPerson.Email; } else { nameOrAlternate = syndicationPerson.Name; } author = cm.FindPersonByNameOrAlternate(nameOrAlternate); } if (author == null) { string name = string.Empty; if (item.Authors.Count > 0) { if (item.Authors[0].Name != null) { name = item.Authors[0].Name; } else { name = item.Authors[0].Email; } } author = new Person(client) { Name = name }; if (source.IsStackOverflow) { author.StackOverflowId = item.Authors[0].Uri; } author.Save(); author = cm.FindPersonByNameOrAlternate(name, true); } List <Person> authors = new List <Person>(); authors.Add(author); //Console.WriteLine("Using author: " + author.Name); if (source.IsStackOverflow) { if (string.IsNullOrEmpty(author.StackOverflowId)) { author.StackOverflowId = item.Authors[0].Uri; author.Save(true); } } if (item.PublishDate.DateTime > DateTime.MinValue) { // Organize content by Date // Year // Month // Day string store = cm.GetFolderForDate(item.PublishDate.DateTime); string articleTitle; if (string.IsNullOrEmpty(item.Title.Text)) { articleTitle = "No title specified"; } else { articleTitle = item.Title.Text; } articleTitle = articleTitle.Trim(); OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData(); bool alreadyExists = false; foreach (XElement node in client.GetListXml(store, filter).Nodes()) { if (!node.Attribute("Title").Value.Equals(articleTitle)) { continue; } alreadyExists = true; break; } if (!alreadyExists) { //Console.WriteLine(articleTitle + " is a new article. Saving"); Console.Write("."); Article article = new Article(client, new TcmUri(store)); string content = ""; string summary = ""; try { if (item.Content != null) { content = ((TextSyndicationContent)item.Content).Text; } if (item.Summary != null) { summary = item.Summary.Text; } if (!string.IsNullOrEmpty(content)) { try { content = Utilities.ConvertHtmlToXhtml(content); } catch (Exception) { content = null; } } if (!string.IsNullOrEmpty(summary)) { try { summary = Utilities.ConvertHtmlToXhtml(summary); } catch (Exception) { summary = null; } } if (string.IsNullOrEmpty(summary)) { summary = !string.IsNullOrEmpty(content) ? content : "Could not find summary"; } if (string.IsNullOrEmpty(content)) { content = !string.IsNullOrEmpty(summary) ? summary : "Could not find content"; } } catch (Exception ex) { content = "Could not convert source description to XHtml. " + ex.Message; content += ((TextSyndicationContent)item.Content).Text; } article.Authors = authors; article.Body = content; article.Date = item.PublishDate.DateTime; article.DisplayTitle = item.Title.Text; article.Title = articleTitle; article.Summary = summary; article.Url = item.Links.First().Uri.AbsoluteUri; List <string> categories = new List <string>(); foreach (var category in item.Categories) { categories.Add(category.Name); } article.Categories = categories; article.Source = source; article.Save(); newArticles.Add(article); } } } if (newArticles.Count > 0) { addedContent.Add(source, newArticles); } } List <string> idsToPublish = new List <string>(); if (addedContent.Count > 0) { Console.WriteLine("============================================================"); Console.WriteLine("Added content"); foreach (Source source in addedContent.Keys) { string sg = cm.GetStructureGroup(source.Title, cm.ResolveUrl(Constants.RootStructureGroup)); Console.WriteLine("Source: " + source.Content.Title + "(" + addedContent[source].Count + ")"); foreach (Article article in addedContent[source]) { string yearSg = cm.GetStructureGroup(article.Date.Year.ToString(CultureInfo.InvariantCulture), sg); string pageId = cm.AddToPage(yearSg, article); if (!idsToPublish.Contains(pageId)) { idsToPublish.Add(pageId); } Console.WriteLine(article.Title + ", " + article.Authors[0].Name); } Console.WriteLine("-------"); } Console.WriteLine("============================================================"); } // Publishing //cm.Publish(idsToPublish.ToArray(), "tcm:0-2-65537"); Console.WriteLine("Finished, press any key to exit"); Console.Read(); }
static void Main() { SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient("netTcp_2013"); ContentManager cm = new ContentManager(client); List<Source> sources = cm.GetSources(); int countSources = sources.Count; Console.WriteLine("Loaded " + countSources + " sources. Starting to process."); XmlNamespaceManager nm = new XmlNamespaceManager(new NameTable()); nm.AddNamespace("dc", "http://purl.org/dc/elements/1.1/"); Dictionary<Source, List<Article>> addedContent = new Dictionary<Source, List<Article>>(); foreach (var source in sources) { Console.WriteLine("Loading content for source " + source.Title); XmlDocument feedXml = null; WebRequest wrq = WebRequest.Create(source.RssFeedUrl); wrq.Proxy = WebRequest.DefaultWebProxy; wrq.Proxy.Credentials = CredentialCache.DefaultCredentials; XmlTextReader reader = null; SyndicationFeed feed = null; try { reader = new XmlTextReader(wrq.GetResponse().GetResponseStream()); feed = SyndicationFeed.Load(reader); } catch (Exception ex) { Console.WriteLine("Could not read response from source" + ex.Message); } if (reader == null || feed == null) continue; int countItems = feed.Items.Count(); Console.WriteLine("Loaded " + countItems + " items from source. Processing"); int count = 0; List<Article> newArticles = new List<Article>(); foreach (var item in feed.Items) { count++; Person author = null; if (item.Authors.Count == 0) { //Console.WriteLine("Could not find an author in feed source, checking for default"); if (source.DefaultAuthor != null) { author = source.DefaultAuthor; //Console.WriteLine("Using default author " + author.Name); } else { //Console.WriteLine("Could not find default author, being creative"); if (feedXml == null) { try { feedXml = new XmlDocument(); feedXml.Load(source.RssFeedUrl); } catch (Exception ex) { Console.WriteLine("Something went wrong loading " + source.RssFeedUrl); Console.WriteLine(ex.ToString()); } } if (feedXml != null) { string xpath = "/rss/channel/item[" + count + "]/dc:creator"; if (feedXml.SelectSingleNode(xpath, nm) != null) { author = cm.FindPersonByNameOrAlternate(feedXml.SelectSingleNode(xpath, nm).InnerText); if (author == null) { author = new Person(client) { Name = feedXml.SelectSingleNode(xpath, nm).InnerText }; author.Save(); author = cm.FindPersonByNameOrAlternate( feedXml.SelectSingleNode(xpath, nm).InnerText, true); } } } } } else { string nameOrAlternate; SyndicationPerson syndicationPerson = item.Authors.First(); if (string.IsNullOrEmpty(syndicationPerson.Name)) nameOrAlternate = syndicationPerson.Email; else nameOrAlternate = syndicationPerson.Name; author = cm.FindPersonByNameOrAlternate(nameOrAlternate); } if (author == null) { string name = string.Empty; if (item.Authors.Count > 0) { if (item.Authors[0].Name != null) name = item.Authors[0].Name; else name = item.Authors[0].Email; } author = new Person(client) { Name = name }; if (source.IsStackOverflow) { author.StackOverflowId = item.Authors[0].Uri; } author.Save(); author = cm.FindPersonByNameOrAlternate(name, true); } List<Person> authors = new List<Person> { author }; //Console.WriteLine("Using author: " + author.Name); string stackOverflowId = null; if (source.IsStackOverflow) { if (string.IsNullOrEmpty(author.StackOverflowId)) { author.StackOverflowId = item.Authors[0].Uri; author.Save(true); } } if (source.IsTridionStackExchange) { stackOverflowId = item.Id; } if (item.PublishDate.DateTime > DateTime.MinValue) { // Organize content by Date // Year // Month // Day string store = cm.GetFolderForDate(item.PublishDate.DateTime); string articleTitle; if (string.IsNullOrEmpty(item.Title.Text)) { articleTitle = "No title specified"; } else { articleTitle = item.Title.Text; } articleTitle = articleTitle.Trim(); OrganizationalItemItemsFilterData filter = new OrganizationalItemItemsFilterData(); bool alreadyExists = false; XElement items = client.GetListXml(store, filter); foreach (XElement node in items.Nodes()) { if (source.IsTridionStackExchange) { Article a = new Article(new TcmUri(node.Attribute("ID").Value), client); if (a.StackOverFlowQuestionId == stackOverflowId) { alreadyExists = true; if (a.Title != node.Attribute("Title").Value) { a.Title = node.Attribute("Title").Value; a.Save(); Console.WriteLine("Modified title of article with TrEx ID: " + stackOverflowId); } continue; } } if (!node.Attribute("Title").Value.Equals(articleTitle)) continue; alreadyExists = true; break; } // Loop differently if this is Tridion on StackExchange (titles tend to change as people fix the content) if (!alreadyExists) { //Console.WriteLine(articleTitle + " is a new article. Saving"); Console.Write("."); Article article = new Article(client, new TcmUri(store)); string content = ""; string summary = ""; try { if (item.Content != null) { content = ((TextSyndicationContent)item.Content).Text; } if (item.Summary != null) { summary = item.Summary.Text; } if (!string.IsNullOrEmpty(content)) { try { content = Utilities.ConvertHtmlToXhtml(content); } catch (Exception) { content = null; } } if (!string.IsNullOrEmpty(summary)) { try { summary = Utilities.ConvertHtmlToXhtml(summary); } catch (Exception) { summary = null; } } if (string.IsNullOrEmpty(summary)) { summary = !string.IsNullOrEmpty(content) ? content : "Could not find summary"; } if (string.IsNullOrEmpty(content)) { content = !string.IsNullOrEmpty(summary) ? summary : "Could not find content"; } } catch (Exception ex) { content = "Could not convert source description to XHtml. " + ex.Message; content += ((TextSyndicationContent)item.Content).Text; } article.Authors = authors; article.Body = content; article.Date = item.PublishDate.DateTime; article.DisplayTitle = item.Title.Text; article.Title = articleTitle; article.Summary = summary; article.Url = item.Links.First().Uri.AbsoluteUri; //if (stackOverflowId != null) article.StackOverFlowQuestionId = stackOverflowId; List<string> categories = new List<string>(); foreach (var category in item.Categories) { categories.Add(category.Name); } article.Categories = categories; article.Source = source; article.Save(); Console.Write("#"); newArticles.Add(article); } else { Console.Write("."); } } } if (newArticles.Count > 0) { addedContent.Add(source, newArticles); } } List<string> idsToPublish = new List<string>(); if (addedContent.Count > 0) { Console.WriteLine("============================================================"); Console.WriteLine("Added content"); foreach (Source source in addedContent.Keys) { string sg = cm.GetStructureGroup(source.Title, cm.ResolveUrl(Constants.RootStructureGroup)); Console.WriteLine("Source: " + source.Content.Title + "(" + addedContent[source].Count + ")"); foreach (Article article in addedContent[source]) { string yearSg = cm.GetStructureGroup(article.Date.Year.ToString(CultureInfo.InvariantCulture), sg); string pageId = cm.AddToPage(yearSg, article); if (!idsToPublish.Contains(pageId)) idsToPublish.Add(pageId); Console.WriteLine(article.Title + ", " + article.Authors[0].Name); } Console.WriteLine("-------"); } Console.WriteLine("============================================================"); } //Publishing cm.Publish(idsToPublish.ToArray(), "tcm:0-2-65537"); Console.WriteLine("Finished, press any key to exit"); Console.Read(); }