1: using System;
2: using System.Security.Principal;
3: using System.IO;
4: using Microsoft.ContentManagement.Publishing;
5: using System.Collections;
6:
7: namespace RenameTheChannels
8: { 9: /// <summary>
10: /// Summary description for Class1.
11: /// </summary>
12: class Class1
13: { 14: /// <summary>
15: /// The main entry point for the application.
16: /// </summary>
17: public static CmsApplicationContext appContext = null;
18: public static TextWriter oTextWriter = null;
19: [STAThread]
20: static void Main(string[] args)
21: { 22: try
23: { 24: oTextWriter = new StreamWriter("FixDuplicateEntries.txt"); 25: appContext = new CmsApplicationContext();
26: WindowsIdentity ident = WindowsIdentity.GetCurrent();
27: appContext.AuthenticateUsingUserHandle((System.IntPtr)ident.Token.ToInt32(),PublishingMode.Update);
28: Console.WriteLine("********************************************Renaming Postings*********************************************"); 29: IterateAllChannels(appContext.RootChannel);
30: Console.WriteLine("******************************************** End of Renaming Postings*************************************"); 31: Console.WriteLine("******************************************** Renaming Resources*******************************************"); 32: IterateResourceGallery(appContext.RootResourceGallery);
33: Console.WriteLine("******************************************** End of Renaming Resources************************************"); 34:
35: }
36: catch(Exception ex)
37: { 38: Console.WriteLine(ex.Message);
39: }
40: finally
41: { 42: appContext.Dispose();
43: oTextWriter.Close();
44: Console.WriteLine("Operation has been completed !"); 45: Console.ReadLine();
46: }
47: }
48:
49: public static void IterateResourceGallery(ResourceGallery oRootResourceGallery)
50: { 51: foreach(ResourceGallery oChildResourceGallery in oRootResourceGallery.ResourceGalleries)
52: { 53: string[] childResources = FinalResources(oChildResourceGallery);
54: IterateResourceGallery(oChildResourceGallery);
55: }
56: }
57:
58: public static string[] FinalResources(ResourceGallery oChildResourceGallery)
59: { 60: string[] Resources = new string[oChildResourceGallery.Resources.Count];
61:
62: //bool hasChange = false;
63:
64: if(oChildResourceGallery.Resources.Count >0)
65: { 66: ArrayList oResourceList = new ArrayList();
67: for(int i=0;i<oChildResourceGallery.Resources.Count;i++)
68: { 69: oResourceList.Add(oChildResourceGallery.Resources[i]);
70: }
71:
72: int iCount = 1;
73:
74: foreach(Resource oCompareResource in oChildResourceGallery.Resources)
75: { 76: int i=0;
77: foreach(Resource oResource in oResourceList)
78: { 79: if(oResource.Name == oCompareResource.Name )
80: { 81: i++;
82: if(i>1)
83: { 84: string[] strName = oResource.Name.Split('.'); 85:
86: string strOldName = oResource.Name;
87: string strNewName = strName[0] + i.ToString() +"." + strName[1];;
88:
89: oResource.Name = strNewName;
90: oResource.DisplayName = strNewName;
91: appContext.CommitAll();
92: Console.WriteLine("Resource Gallery : " + oChildResourceGallery.Name + "old name: " + strOldName + " New Name :" + strNewName ); 93: oTextWriter.WriteLine("Resource Gallery : " + oChildResourceGallery.Name + "old name: " + strOldName + " New Name :" + strNewName ); 94: }
95:
96: }
97: }
98: iCount ++;
99: }
100: }
101: return Resources;
102: }
103:
104: public static void IterateAllChannels(Channel oRootChannel)
105: { 106:
107: foreach(Channel oChildChannel in oRootChannel.Channels)
108: { 109: string[] childrenPosting = FinalPosting(oChildChannel);
110: IterateAllChannels(oChildChannel);
111: }
112: }
113:
114: public static string[] FinalPosting(Channel oChildChannel)
115: { 116: string[] Postings = new string[oChildChannel.Postings.Count];
117:
118:
119: if(oChildChannel.Postings.Count >0)
120: { 121: ArrayList oPostingList = new ArrayList();
122: for(int i=0;i<oChildChannel.Postings.Count;i++)
123: { 124: oPostingList.Add(oChildChannel.Postings[i]);
125: }
126:
127: int iCount = 1;
128:
129: foreach(Posting oComparePosting in oChildChannel.Postings)
130: { 131: int i=0;
132: foreach(Posting oPosting in oPostingList)
133: { 134: if(oPosting.Name == oComparePosting.Name )
135: { 136: i++;
137: if(i>1)
138: { 139:
140: string strOldName = oPosting.Name;
141: string strNewName = strOldName + i.ToString();
142:
143: oPosting.Name = strNewName;
144: oPosting.DisplayName = strNewName;
145: oPosting.Approve();
146:
147: appContext.CommitAll();
148: Console.WriteLine("Channel : " + oChildChannel.Name + "old name: " + strOldName + " New Name :" + strNewName ); 149: oTextWriter.WriteLine("Channel : " + oChildChannel.Name + "old name: " + strOldName + " New Name :" + strNewName ); 150: }
151: }
152: }
153: iCount ++;
154: }
155: }
156: return Postings;
157: }
158:
159: }
160: }
161:
162:
163: