/// <summary> /// Creates a new term group, in the specified term store. /// </summary> /// <param name="termStore">the term store to use</param> /// <param name="groupName">Name of the term group</param> /// <param name="groupId">(Optional) ID of the group; if not provided a random GUID is used</param> /// <param name="groupDescription">(Optional) Description of the term group</param> /// <returns>The created term group</returns> public static TermGroup CreateTermGroup(this TermStore termStore, string groupName, Guid groupId = default(Guid), string groupDescription = null) { if (string.IsNullOrEmpty(groupName)) { throw new ArgumentNullException("groupName"); } var termGroup = default(TermGroup); groupName = NormalizeName(groupName); ValidateName(groupName, "groupName"); // Create Group if (groupId == Guid.Empty) { groupId = Guid.NewGuid(); } if (!termStore.IsObjectPropertyInstantiated("Name")) { // get instances to root web, since we are processing currently sub site termStore.Context.Load(termStore); termStore.Context.ExecuteQueryRetry(); } Log.Info(Constants.LOGGING_SOURCE, CoreResources.TaxonomyExtension_CreateTermGroup0InStore1, groupName, termStore.Name); termGroup = termStore.CreateGroup(groupName, groupId); termStore.Context.Load(termGroup, g => g.Name, g => g.Id, g => g.Description); termStore.Context.ExecuteQueryRetry(); // Apply description bool changed = false; if (groupDescription != null && !string.Equals(termGroup.Description, groupDescription)) { try { ValidateDescription(groupDescription, "groupDescription"); termGroup.Description = groupDescription; changed = true; } catch (Exception ex) { Log.Warning(Constants.LOGGING_SOURCE, CoreResources.TaxonomyExtension_ExceptionUpdateDescriptionGroup01, termGroup.Name, termGroup.Id, ex.Message); } } if (changed) { Log.Debug(Constants.LOGGING_SOURCE, "Updating term group"); termStore.Context.ExecuteQueryRetry(); //termStore.CommitAll(); } return termGroup; }
public static TermGroup GenerateGroup(this TermStore ts, string name, Guid guid) { var group = ts.Groups.FirstOrDefault(g => g.Name == name); if (group == null) group = ts.CreateGroup(name, guid); return group; }