Example #1
0
        static void Main(string[] args)
        {
            SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient("netTcp_2013");

            const string primaryAccountName = "Chris Summers";
            List<string> alternateNames = new List<string> { "[email protected] (chris)" };

            ContentManager cm = new ContentManager(client);
            List<Person> persons = cm.GetPersons();
            Person primary = null;

            foreach (Person p in persons)
            {
                if (p.Name == primaryAccountName)
                {
                    primary = p;
                    break;
                }
            }
            if (primary == null)
            {
                Console.WriteLine("Could not find primary account with name " + primaryAccountName);
                return;
            }

            List<Person> duplicates = new List<Person>();
            foreach (string name in alternateNames)
            {
                foreach (Person p in persons)
                {
                    if(p.Name == name)
                        duplicates.Add(p);
                }
            }

            // 1. Add alternate names to primary account
            // 2. Move author of alternate content to primary account
            // 3. Republish content
            // 4. Delete alternate person accounts

            List<string> currentNames = primary.AlternateNames;
            bool changedAuthorName = false;
            foreach (string name in alternateNames)
            {
                if (!currentNames.Contains(name))
                {
                    currentNames.Add(name);
                    changedAuthorName = true;
                }
            }

            if (changedAuthorName)
            {
                primary.AlternateNames = currentNames;
                primary.Save(true);
            }
            List<string> articles = new List<string>();
            foreach (Person p in duplicates)
            {
                foreach (Article a in cm.GetArticlesForPerson(p))
                {
                    // BUG in a.Authors!!
                    // It is not replacing the values, just adding.
                    a.Authors = new List<Person>{primary};
                    a.Save(true);
                    articles.Add(a.Id.GetVersionlessUri().ToString().Replace("tcm:4-", "tcm:5-"));
                }
            }

            cm.Publish(articles.ToArray(), Constants.TargetUri);

            foreach (Person p in duplicates)
            {
                try
                {
                    client.Delete(p.Id);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Could not delete component with id " + p.Id + ". Exception: " + ex);
                }
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            const string titleOfSourceToRemove = "Meet John Song";
            SessionAwareCoreServiceClient client = new SessionAwareCoreServiceClient("netTcp_2013");

            ContentManager cm = new ContentManager(client);
            List<Source> sources = cm.GetSources();
            Source sourceToRemove = sources.FirstOrDefault(source => source.Title == titleOfSourceToRemove);
            if (sourceToRemove == null)
            {
                Console.WriteLine("Could not find source with name " + titleOfSourceToRemove);
                return;
            }

            // find all usages of the source
            // Delete all pages for the source
            // Unpublish all pages & Components
            // Delete
            List<Article> articles = cm.GetArticlesForSource(sourceToRemove);
            List<string> ids = new List<string>();
            List<string> pageIds = new List<string>();
            foreach (Article article in articles)
            {
                ids.Add(article.Id);
                string pageId = cm.GetPageIdForArticle(article);
                if (pageId != null)
                {
                    pageIds.Add(pageId);
                }
            }

            // need pages
            cm.Unpublish(ids.ToArray(), "tcm:0-2-65537");
            cm.Unpublish(pageIds.ToArray(), "tcm:0-2-65537");
            Console.WriteLine("Wait for unpublish to finish...");
            Console.Read();
            // Must delete pages first

            foreach (string pageId in pageIds)
            {
                try
                {
                    if(client.IsExistingObject(pageId))
                        client.Delete(pageId);
                }
                catch (Exception ex)
                { Console.WriteLine("Could not delete item with ID: " + pageId + ex.ToString()); }

            }

            foreach (string id in ids)
            {
                try
                {
                    if(client.IsExistingObject(id))
                        client.Delete(id);
                }
                catch(Exception ex)
                { Console.WriteLine("Could not delete item with ID: " + id + ex.ToString());}

            }
        }