public void CreateFields() { Web web = ClientContext.Web; FieldCollection webFieldCollection = web.Fields; ClientContext.Load(webFieldCollection); ClientContext.ExecuteQuery(); var termStoreId = new TaxonomyManager(null).GetTermStoreId(ClientContext); foreach (ShField field in Fields) { var existingField = webFieldCollection.SingleOrDefault(item => item.InternalName == field.InternalName); if (existingField == null) { //Creating new field Log.Debug("Attempting to create field " + field.DisplayName); if (field.Type.StartsWith("TaxonomyFieldType")) { field.SspId = termStoreId; DeleteHiddenFieldForTaxonomyField(webFieldCollection, field.ID); CreateTaxonomyField(field, webFieldCollection); } else { CreateField(field, webFieldCollection); } } else { //Updating existing field UpdateExistingField(field, existingField); } } }
private void InstallTaxonomyFromSingleFile(ClientContext context, string pathToFile) { Log.Info("Starting installation of taxonomy based on " + pathToFile); var taxPersistanceProvider = new FilePersistanceProvider<ShTermGroup>(pathToFile); var taxonomyManager = new TaxonomyManager(taxPersistanceProvider.Load()); taxonomyManager.WriteTaxonomyToTermStore(context); }
private void ExportTaxonomyGroup(string groupName) { Log.Info("Starting export of taxonomy group " + groupName); using (var context = new ClientContext(_urlToSite)) { context.Credentials = _credentials; var outputDirectoryPath = Path.Combine(_rootPath, "export"); Directory.CreateDirectory(outputDirectoryPath); var taxPersistanceProvider = new FilePersistanceProvider<ShTermGroup>(Path.Combine(outputDirectoryPath, groupName.ToLower().Replace(" ", "") + "taxonomy.json")); var taxonomyManager = new TaxonomyManager(); var groupConfig = taxonomyManager.ExportTaxonomyGroupToConfig(context, groupName); if (groupConfig != null) { taxPersistanceProvider.Save(groupConfig); Log.Info("Completed export of taxonomy group " + groupName); } } }
private Guid GetTermSetId(ShField field) { if (field.TermSetId != Guid.Empty) return field.TermSetId; if (string.IsNullOrEmpty(field.TermSetName)) { throw new Exception("Invalid taxonomy configuration settings for field " + field.DisplayName); } var manager = new TaxonomyManager(); return manager.GetTermSetId(ClientContext, field.TermSetName); }