//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);
        }
        /// <summary>
        /// Gets list of CT's along with the associated schema & view
        /// </summary>
        /// <param>none</param>
        /// <returns></returns>
        public static string GetTemplatesInPublication(string pubID)
        {
            //note: this runs for about 1 minute
            StringBuilder sb = new StringBuilder();
            string        meta = string.Empty, ct = string.Empty, schema = string.Empty;

            byte[]       data = null;
            MemoryStream stm  = null;;
            XDocument    doc  = null;

            try
            {
                cs_client = CoreServiceProvider.CreateCoreService();

                // get the Id of the publication to import into
                RepositoryItemsFilterData templateFilter = SetTemplateFilterCriterias();
                XElement templates = cs_client.GetListXml(pubID, templateFilter);;

                foreach (XElement template in templates.Descendants())
                {
                    ComponentTemplateData t = (ComponentTemplateData)cs_client.Read(CheckAttributeValueOrEmpty(template, "ID"), null);

                    if (t.Metadata != "")
                    {
                        ct   = t.Title;
                        data = Encoding.ASCII.GetBytes(t.Metadata);
                        stm  = new MemoryStream(data, 0, data.Length);
                        doc  = XDocument.Load(stm);
                        meta = doc.Root.Value;

                        if (t.RelatedSchemas.Count() > 0)
                        {
                            schema = t.RelatedSchemas[0].Title;
                        }
                        else
                        {
                            schema = "No Schema Found";
                        }

                        sb.AppendLine(ct + "|" + schema + "|" + meta);
                    }
                }
            }
            catch (Exception ex)
            {
                // throw ex;
            }
            finally
            {
                cs_client.Close();
            }

            return(sb.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>());
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="folderID"></param>
        /// <param name="recursive"></param>
        /// <param name="srcSchemaID"></param>
        /// <param name="destSchemaID"></param>
        public static string UpdateSchemaForComponent(string folderID, bool recursive, string srcSchemaID, string destSchemaID, bool isMultiMediaComp)
        {
            cs_client = CoreServiceProvider.CreateCoreService();
            StringBuilder        sb          = new StringBuilder();
            FolderData           folder      = cs_client.Read(folderID, null) as FolderData;
            SchemaData           schema      = cs_client.Read(destSchemaID, null) as SchemaData;
            XNamespace           ns          = schema.NamespaceUri;
            XElement             items       = cs_client.GetListXml(folder.Id, SetComponenetFilterCriterias(isMultiMediaComp));
            List <ComponentData> failedItems = new List <ComponentData>();

            foreach (XElement item in items.Elements())
            {
                ComponentData component = cs_client.Read(item.Attribute("ID").Value, null) as ComponentData;

                if (!component.Schema.IdRef.Equals(srcSchemaID))
                {
                    // If the component is not of the schmea that we want to change from, do nothing...
                    return("");
                }

                if (component.Schema.IdRef.Equals(schema.Id))
                {
                    // If the component already has this schema, don't do anything.
                    return("");
                }

                component = cs_client.TryCheckOut(component.Id, new ReadOptions()) as ComponentData;


                if (component.IsEditable.Value)
                {
                    component.Schema.IdRef = destSchemaID;
                    component.Metadata     = new XElement(ns + "Metadata").ToString();
                    cs_client.Save(component, null);
                    cs_client.CheckIn(component.Id, null);
                }
                else
                {
                    sb.AppendLine("Schema Can not be updated for: " + component.Id);
                    sb.AppendLine("");
                }
            }
            return(sb.ToString());
        }
        /// <summary>
        /// Create Keywirds in a given category
        /// </summary>
        /// <param name="model">CategoryModel</param>
        /// <param name="CategoryId">tcm:xx-yy-zz</param>
        /// <returns></returns>
        public static string CreateKeywordsInCategory(CategoryModel model, string CategoryId)
        {
            var result = "true";

            cs_client = CoreServiceProvider.CreateCoreService();

            try
            {
                // open the category that is already created in Tridion
                CategoryData category = (CategoryData)cs_client.Read(CategoryId, null);

                var xmlCategoryKeywords = cs_client.GetListXml(CategoryId, new KeywordsFilterData());
                var keywordAny          = xmlCategoryKeywords.Elements()
                                          .Where(element => element.Attribute("Key").Value == model.Key)
                                          .Select(element => element.Attribute("ID").Value)
                                          .Select(id => (KeywordData)cs_client.Read(id, null)).FirstOrDefault();

                if (keywordAny == null)
                {
                    // create a new keyword
                    KeywordData keyword = (KeywordData)cs_client.GetDefaultData(Tridion.ContentManager.CoreService.Client.ItemType.Keyword, category.Id, new ReadOptions());
                    // set the id to 0 to notify Tridion that it is new
                    keyword.Id          = "tcm:0-0-0";
                    keyword.Title       = model.Title;
                    keyword.Key         = model.Key;
                    keyword.Description = model.Description;
                    keyword.IsAbstract  = false;

                    // create the keyword
                    cs_client.Create(keyword, null);
                    cs_client.Close();
                }
            }
            catch (Exception ex)
            {
                result = "Error: " + ex.Message;
            }
            finally
            {
                cs_client.Close();
            }

            return(result);
        }
        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);
        }
Beispiel #7
0
        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();
        }
Beispiel #8
0
 public XElement GetListXml(string id, SubjectRelatedListFilterData filter)
 {
     return(_client.GetListXml(id, filter));
 }
        static void Main(string[] args)
        {
            //args[0] = "tcm:11-403-8";
            if (!args.Any())
            {
                Log("Please pass the Schema Tcm Uri as a parameter.");
                return;
            }
            string schemaUri = args[0];
            if (!TcmUri.IsValid(schemaUri))
            {
                Log("The specified URI of " + schemaUri + " is not a valid URI, please pass the schema Tcm Uri as a parameter.");
                return;
            }

            SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient("netTcp_2013");
            if (!client.IsExistingObject(schemaUri))
            {
                Log("Could not find item with URI " + schemaUri + " in Tridion. Please pass the Schema Tcm Uri as a parameter.");
                return;
            }
            ReadOptions readOptions = new ReadOptions();
            UsingItemsFilterData whereUsedFilter = new UsingItemsFilterData { ItemTypes = new[] { ItemType.Component } };
            SchemaData schema = (SchemaData)client.Read(schemaUri, readOptions);
            SchemaFieldsData schemaFieldsData = client.ReadSchemaFields(schema.Id, true, readOptions);
            bool hasMeta = schemaFieldsData.MetadataFields.Any();
            string newNamespace = schema.NamespaceUri;

            if (schema.Purpose == SchemaPurpose.Metadata)
            {
                List<IdentifiableObjectData> items = new List<IdentifiableObjectData>();
                UsingItemsFilterData anyItem = new UsingItemsFilterData();
                foreach (XElement node in client.GetListXml(schema.Id, anyItem).Nodes())
                {
                    string uri = node.Attribute("ID").Value;
                    items.Add(client.Read(uri, readOptions));
                }
                Log("Found " + items.Count + " items using schema...");

                foreach (var item in items)
                {
                    if (item is PublicationData)
                    {
                        PublicationData pub = (PublicationData)item;
                        string meta = pub.Metadata;
                        XmlDocument xml = new XmlDocument();
                        xml.LoadXml(meta);
                        string oldnamespace = xml.DocumentElement.NamespaceURI;
                        if (oldnamespace != newNamespace)
                        {
                            Log("Replacing namespace for publication " + pub.Id + " (" + pub.Title + ") - Current Namespace: " + oldnamespace);
                            string metadata = meta.Replace(oldnamespace, newNamespace);
                            pub.Metadata = metadata;
                            client.Update(pub, readOptions);
                        }
                    }
                    else if (item is RepositoryLocalObjectData)
                    {
                        RepositoryLocalObjectData data = (RepositoryLocalObjectData)item;
                        string meta = data.Metadata;
                        XmlDocument xml = new XmlDocument();
                        xml.LoadXml(meta);
                        string oldnamespace = xml.DocumentElement.NamespaceURI;
                        if (oldnamespace != newNamespace)
                        {
                            Log("Replacing namespace for item " + data.Id + " (" + data.Title + ") - Current Namespace: " + oldnamespace);
                            string metadata = meta.Replace(oldnamespace, newNamespace);
                            data.Metadata = metadata;
                            client.Update(data, readOptions);
                        }

                    }
                }

                return;
            }

            List<ComponentData> components = new List<ComponentData>();
            foreach (XElement node in client.GetListXml(schema.Id, whereUsedFilter).Nodes())
            {
                string uri = node.Attribute("ID").Value;
                components.Add((ComponentData)client.Read(uri, readOptions));
            }
            Log("Found " + components.Count + " components.");

            Log("Current schema namespace set to " + newNamespace + ", checking for components with incorrect namespace.");
            int count = 0;
            foreach (var component in components)
            {
                if (schema.Purpose == SchemaPurpose.Multimedia)
                {
                    Log("Changing Multimedia Component");
                    string meta = component.Metadata;
                    XmlDocument metaXml = new XmlDocument();
                    metaXml.LoadXml(meta);
                    string metaOldnamespace = metaXml.DocumentElement.NamespaceURI;
                    if (metaOldnamespace != newNamespace)
                    {
                        Log("Replacing namespace for item " + component.Id + " (" + component.Title + ") - Current Namespace: " + metaOldnamespace);
                        string metadata = meta.Replace(metaOldnamespace, newNamespace);
                        component.Metadata = metadata;
                        client.Update(component, readOptions);
                    }
                    count++;
                    Log(components.Count - count + " components remaining...");

                    continue;
                }

                string content = component.Content;

                XmlDocument xml = new XmlDocument();
                xml.LoadXml(content);

                string oldnamespace = xml.DocumentElement.NamespaceURI;

                if (oldnamespace != newNamespace)
                {
                    Log("Replacing namespace for component " + component.Id + " (" + component.Title + ") - Current Namespace: " + oldnamespace);
                    content = content.Replace(oldnamespace, newNamespace);
                    try
                    {
                        ComponentData editableComponent = component;
                        editableComponent.Content = content;
                        if (hasMeta)
                        {
                            string metadata = editableComponent.Metadata.Replace(oldnamespace, newNamespace);

                            // Fix for new meta
                            if (string.IsNullOrEmpty(metadata))
                            {
                                metadata = string.Format("<Metadata xmlns=\"{0}\" />", newNamespace);
                                Log("Component had no metadata, but schema specifies it has. Adding empty metadata node");
                            }
                            editableComponent.Metadata = metadata;
                        }

                        if (!hasMeta && !(string.IsNullOrEmpty(editableComponent.Metadata)))
                        {
                            editableComponent.Metadata = string.Empty;
                        }

                        client.Update(editableComponent, readOptions);

                    }
                    catch (Exception ex)
                    {
                        Log("Error occurred trying to update component: " + component.Id + Environment.NewLine + ex);

                    }

                }
                count++;
                Log(components.Count - count + " components remaining...");
            }
        }
Beispiel #10
0
        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();
        }