Deleting a Term Group needs deleting all Term Sets with in, before the group can be deleted.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.IO;
6 using System.Net;
7 using Microsoft.SharePoint.Client;
8 using Microsoft.SharePoint.Client.Taxonomy;
9
10 namespace DeleteSPTaxonomyTerms
11 {
12 class Program
13 {
14 static void Main(string[] args)
15 {
16 DeleteTermsFromTermSet("Managed Metadata Service", "TestGroup1", "TestG1S3", new string[] { "TestS3T2" });
17 DeleteTermSetFromTermGroup("Managed Metadata Service", "TestGroup1", "TestG1S3");
18 DeleteTermGroupFromTermStore("Managed Metadata Service", "TestGroup1");
19 }
20
21 static void DeleteTermsFromTermSet(string termStoreName, string termGroupName, string termSetName, string[] termsList)
22 {
23 using (ClientContext clientContext = new ClientContext(ConfigurationConstants.XYZSharepointSiteCollectionUrl))
24 {
25 NetworkCredential credentials = new NetworkCredential(ConfigurationConstants.XYZSharepointAdminUserName,
26 ConfigurationConstants.XYZSharepointAdminUserPassword, ConfigurationConstants.XYZSharepointDomain);
27 clientContext.Credentials = credentials;
28
29 TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
30 clientContext.Load(taxonomySession, ts => ts.TermStores);
31 clientContext.ExecuteQuery();
32
33 if (taxonomySession != null)
34 {
35 TermStore termStore = null; var cTermStore = -1;
36 if (string.IsNullOrEmpty(termStoreName)) return;
37 try
38 {
39 cTermStore = taxonomySession.TermStores.Where(s => s.Name == termStoreName).Count();
40 termStore = taxonomySession.TermStores.GetByName(termStoreName); clientContext.Load(termStore, tStore => tStore.Name, tStore => tStore.Groups); clientContext.ExecuteQuery();
41 }
42 catch { }
43
44 if (termStore != null && cTermStore > 0) // term store exists
45 {
46 TermGroup myTermGroup = null;
47 var cTermStoreGroups = -1;
48 if (string.IsNullOrEmpty(termGroupName)) return;
49 try
50 {
51 cTermStoreGroups = termStore.Groups.Where(s => s.Name == termGroupName).Count();
52 myTermGroup = termStore.Groups.GetByName(termGroupName); clientContext.Load(myTermGroup, gs => gs.TermSets); clientContext.ExecuteQuery();
53 }
54 catch { }
55
56 if (cTermStoreGroups > 0) //// if term group exists
57 {
58 TermSet myTermSet = null;
59 var cTermGroupSets = -1;
60 if (string.IsNullOrEmpty(termSetName)) return;
61 try
62 {
63 cTermGroupSets = myTermGroup.TermSets.Where(s => s.Name == termSetName).Count();
64 myTermSet = myTermGroup.TermSets.GetByName(termSetName); clientContext.Load(myTermSet, gs => gs.Terms); clientContext.ExecuteQuery();
65 }
66 catch { }
67
68 if (cTermGroupSets > 0) ////if term set exists
69 {
70 TermCollection myTermCollection = myTermSet.Terms;
71 clientContext.Load(myTermCollection, myTC => myTC.Include(termsAlreadyInSystem => termsAlreadyInSystem.Name, termsAlreadyInSystem => termsAlreadyInSystem.Id));
72 clientContext.ExecuteQuery();
73 //check if given terms already exists
74 bool atLeastOneTermValueExistsInTermSet = false;
75 if (termsList != null)
76 {
77 foreach (string givenTermValue in termsList)
78 {
79 //bool termValueAlreadyExistsWithInTermSet = false;
80 foreach (Term termInShareTermSet in myTermCollection)
81 {
82 if (givenTermValue.ToLower().Trim() == termInShareTermSet.Name.ToLower().Trim())
83 {
84 atLeastOneTermValueExistsInTermSet = true;
85 termInShareTermSet.DeleteObject();
86 LogMessages.Instance.LogMessageToTextFile(" deleting term with name " + givenTermValue + " term set: " + termSetName);
87 break;
88 }
89 }
90 }
91 }
92 if (atLeastOneTermValueExistsInTermSet)
93 {
94 clientContext.ExecuteQuery();
95 LogMessages.Instance.LogMessageToTextFile(" Successfully deleted existing terms to term set: " + termSetName);
96 }
97 }
98 }
99 }
100 else
101 {
102 LogMessages.Instance.LogMessageToTextFile(" Term store with name " + termStoreName + " not exists ");
103 }
104 }
105 }
106 }
107 static void DeleteTermSetFromTermGroup(string termStoreName, string termGroupName, string termSetName)
108 {
109 using (ClientContext clientContext = new ClientContext(ConfigurationConstants.XYZSharepointSiteCollectionUrl))
110 {
111 NetworkCredential credentials = new NetworkCredential(ConfigurationConstants.XYZSharepointAdminUserName,
112 ConfigurationConstants.XYZSharepointAdminUserPassword, ConfigurationConstants.XYZSharepointDomain);
113 clientContext.Credentials = credentials;
114
115 TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
116 clientContext.Load(taxonomySession, ts => ts.TermStores);
117 clientContext.ExecuteQuery();
118
119 if (taxonomySession != null)
120 {
121 TermStore termStore = null; var cTermStore = -1;
122 if (string.IsNullOrEmpty(termStoreName)) return;
123 try
124 {
125 cTermStore = taxonomySession.TermStores.Where(s => s.Name == termStoreName).Count();
126 termStore = taxonomySession.TermStores.GetByName(termStoreName); clientContext.Load(termStore, tStore => tStore.Name, tStore => tStore.Groups); clientContext.ExecuteQuery();
127 }
128 catch { }
129
130 if (termStore != null && cTermStore > 0) // term store exists
131 {
132 TermGroup myTermGroup = null;
133 var cTermStoreGroups = -1;
134 if (string.IsNullOrEmpty(termGroupName)) return;
135 try
136 {
137 cTermStoreGroups = termStore.Groups.Where(s => s.Name == termGroupName).Count();
138 myTermGroup = termStore.Groups.GetByName(termGroupName); clientContext.Load(myTermGroup, gs => gs.TermSets); clientContext.ExecuteQuery();
139 }
140 catch { }
141
142 if (cTermStoreGroups > 0) //// if term group exists
143 {
144 TermSet myTermSet = null;
145 var cTermGroupSets = -1;
146 if (string.IsNullOrEmpty(termSetName)) return;
147 try
148 {
149 cTermGroupSets = myTermGroup.TermSets.Where(s => s.Name == termSetName).Count();
150 myTermSet = myTermGroup.TermSets.GetByName(termSetName); clientContext.Load(myTermSet, gs => gs.Terms); clientContext.ExecuteQuery();
151 }
152 catch { }
153
154 if (cTermGroupSets > 0) ////if term set exists
155 {
156 myTermSet.DeleteObject();
157 clientContext.ExecuteQuery();
158 LogMessages.Instance.LogMessageToTextFile(" Successfully deleted existing term set " + termSetName + " from term group: " + termGroupName);
159 }
160 }
161 }
162 else
163 {
164 LogMessages.Instance.LogMessageToTextFile(" Term store with name " + termStoreName + " not exists ");
165 }
166 }
167 }
168 }
169 static void DeleteTermGroupFromTermStore(string termStoreName, string termGroupName)
170 {
171 using (ClientContext clientContext = new ClientContext(ConfigurationConstants.XYZSharepointSiteCollectionUrl))
172 {
173 NetworkCredential credentials = new NetworkCredential(ConfigurationConstants.XYZSharepointAdminUserName,
174 ConfigurationConstants.XYZSharepointAdminUserPassword, ConfigurationConstants.XYZSharepointDomain);
175 clientContext.Credentials = credentials;
176
177 TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
178 clientContext.Load(taxonomySession, ts => ts.TermStores);
179 clientContext.ExecuteQuery();
180
181 if (taxonomySession != null)
182 {
183 TermStore termStore = null; var cTermStore = -1;
184 if (string.IsNullOrEmpty(termStoreName)) return;
185 try
186 {
187 cTermStore = taxonomySession.TermStores.Where(s => s.Name == termStoreName).Count();
188 termStore = taxonomySession.TermStores.GetByName(termStoreName); clientContext.Load(termStore, tStore => tStore.Name, tStore => tStore.Groups); clientContext.ExecuteQuery();
189 }
190 catch { }
191
192 if (termStore != null && cTermStore > 0) // term store exists
193 {
194 TermGroup myTermGroup = null;
195 var cTermStoreGroups = -1;
196 if (string.IsNullOrEmpty(termGroupName)) return;
197 try
198 {
199 cTermStoreGroups = termStore.Groups.Where(s => s.Name == termGroupName).Count();
200 myTermGroup = termStore.Groups.GetByName(termGroupName); clientContext.Load(myTermGroup, gs => gs.TermSets); clientContext.ExecuteQuery();
201 }
202 catch { }
203
204 if (cTermStoreGroups > 0) //// if term group exists
205 {
206 //first delete all term sets with in the group, then only group can be deleted.
207 TermSetCollection givenGroupTermSetCollection = myTermGroup.TermSets;
208 foreach (TermSet groupTermSet in givenGroupTermSetCollection)
209 {
210 groupTermSet.DeleteObject();
211 }
212
213 myTermGroup.DeleteObject();
214 clientContext.ExecuteQuery();
215 LogMessages.Instance.LogMessageToTextFile(" Successfully deleted existing term group " + termGroupName + " from term store " + termStoreName);
216 }
217 }
218 else
219 {
220 LogMessages.Instance.LogMessageToTextFile(" Term store with name " + termStoreName + " not exists ");
221 }
222 }
223 }
224 }
225 }
226
227 public class ConfigurationConstants
228 {
229 public static string XYZLogFileLocation = @"C:\\LogFile.txt";
230 public static string XYZSharepointSiteCollectionUrl = "http://sharepointservername:100/";
231 public static string XYZSharepointDomain = "domainName";
232 public static string XYZSharepointAdminUserName = "UserName";
233 public static string XYZSharepointAdminUserPassword = "Password";
234 }
235 public class LogMessages
236 {
237 static LogMessages instance = null;
238 static readonly object padlock = new object();
239 static string FilePath = ConfigurationConstants.XYZLogFileLocation; //ConfigurationSettings.AppSettings["XYZLogFileLocation"];
240 LogMessages() // constructor, if log file not exists create it
241 {
242 string DirectoryName = Path.GetDirectoryName(FilePath);
243 if (!Directory.Exists(DirectoryName))
244 Directory.CreateDirectory(DirectoryName);
245 if (!System.IO.File.Exists(FilePath))
246 {
247 FileStream fs = System.IO.File.Create(FilePath);
248 fs.Close();
249 }
250 }
251 public static LogMessages Instance
252 {
253 get
254 {
255 lock (padlock)
256 {
257 if (instance == null)
258 {
259 instance = new LogMessages();
260 }
261 return instance;
262 }
263 }
264 }
265 public void LogMessageToTextFile(string message)
266 {
267 try
268 {
269 FileStream LogFile = new FileStream(FilePath, FileMode.Append, FileAccess.Write);
270 StreamWriter LogTextWriter = new StreamWriter(LogFile);
271 if (message == "-")
272 LogTextWriter.WriteLine("");
273 else
274 LogTextWriter.WriteLine(message + " , " + System.DateTime.Now.ToString());
275 LogTextWriter.Close();
276 LogFile.Close();
277 }
278 catch (System.IO.IOException ex)
279 {
280 throw ex;
281 }
282 }
283 }
284 }
No comments:
Post a Comment