//gavdcodeend 14 //gavdcodebegin 17 static void SpCsPnpcoreFindTermSet(ClientContext spCtx) { TermSetCollection myTermSet = spCtx.Site.GetTermSetsByName( "CsPnpcoreTermSetEns"); Console.WriteLine(myTermSet[0].Id); }
/// <summary> /// Function to map metadata columns with specific term set /// </summary> /// <param name="clientContext">SP client context</param> /// <param name="fieldCol">Field Collection object</param> internal static void MapMetadataColumns(ClientContext clientContext, FieldCollection fieldCol) { string termsetName = ConfigurationManager.AppSettings["DefaultTermSetName"]; string taxonomyFieldType = ConfigurationManager.AppSettings["TaxonomyFieldType"]; TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext); TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore(); TermSetCollection termSetCollection = termStore.GetTermSetsByName(termsetName, 1033); // Setting LCID=1033, as the default language is English clientContext.Load(termStore); clientContext.Load(termSetCollection); clientContext.ExecuteQuery(); string termStoreId = Convert.ToString(termStore.Id, CultureInfo.InvariantCulture); string termSetId = Convert.ToString(termSetCollection[0].Id, CultureInfo.InvariantCulture); TaxonomyField taxonomyField = null; foreach (Field field in fieldCol) { if (field.TypeAsString.Equals(taxonomyFieldType, StringComparison.OrdinalIgnoreCase)) { taxonomyField = clientContext.CastTo <TaxonomyField>(field); taxonomyField.SspId = new Guid(termStoreId); taxonomyField.TermSetId = new Guid(termSetId); taxonomyField.AnchorId = Guid.Empty; taxonomyField.Update(); } } }
public static TermSet GetByName(this TermSetCollection termSets, string name) { if (String.IsNullOrEmpty(name)) { throw new ArgumentException("Term set name cannot be empty", "name"); } return(termSets.SingleOrDefault(termSet => string.Equals(termSet.Name, name))); }
public TermSetCollectionInstance(ObjectInstance prototype, TermSetCollection termSetCollection) : this(prototype) { if (termSetCollection == null) { throw new ArgumentNullException("termSetCollection"); } m_termSetCollection = termSetCollection; }
public static void SearchTermSetsByTermLabels(SPSite site, string[] termLabels) { TaxonomySession session = new TaxonomySession(site); // Returns all TermSet instances from all TermStores that contain terms // with matching labels for all specified strings. TermSetCollection termSets = session.GetTermSets(termLabels); Console.WriteLine("The number of matching Term Sets is " + termSets.Count); }
private void GetTaxonomyFieldInfo(ClientContext clientContext, out Guid termStoreId, out Guid termSetId) { TaxonomySession session = TaxonomySession.GetTaxonomySession(clientContext); TermStore termStore = session.GetDefaultSiteCollectionTermStore(); TermSetCollection termSets = termStore.GetTermSetsByName("Department", 1033); clientContext.Load(termSets, tsc => tsc.Include(ts => ts.Id)); clientContext.Load(termStore); clientContext.ExecuteQuery(); termStoreId = termStore.Id; termSetId = termSets.FirstOrDefault().Id; }
public static void SearchTermSetsByName(SPSite site, string termSetName) { TaxonomySession session = new TaxonomySession(site); // Search all TermSets that are using the provided name in current // UI LCID from all TermStores associated with the provided site. TermSetCollection termSets = session.GetTermSets(termSetName, CultureInfo.CurrentUICulture.LCID); Console.WriteLine("The number of matching Term Sets is " + termSets.Count); }
private static TermSet GetCorrectTermSet(ClientContext ctx, string termName, TermSetCollection termSets) { //This is a challenge when there is more than one if (termSets.Count == 1) { return(termSets[0]); } if (termSets.Count == 0) { return(null); } TermSet bestMatch = null; var maxTermCount = 0; foreach (var termSet in termSets) { var terms = termSet.GetAllTerms(); ctx.Load(terms); ctx.Load(termSet.Group, g => g.Name); try { ctx.ExecuteQueryRetry(); if (!termSet.Group.Name.StartsWith("Site Collection")) { if (!string.IsNullOrEmpty(termName)) { if (terms.FirstOrDefault(t => t.Name == termName) != null) { return(termSet); } } else { if (terms.Count > maxTermCount || bestMatch == null) { maxTermCount = terms.Count; bestMatch = termSet; } } } } catch { //ignored } } return(bestMatch); }
static void GetTaxonomyFieldInfo(ClientContext clientContext, string TermsetName, out Guid termStoreId, out Guid termSetId) { termStoreId = Guid.Empty; termSetId = Guid.Empty; TaxonomySession session = TaxonomySession.GetTaxonomySession(clientContext); TermStore termStore = session.GetDefaultSiteCollectionTermStore(); TermSetCollection termSets = termStore.GetTermSetsByName(TermsetName, 1033); clientContext.Load(termSets, tsc => tsc.Include(ts => ts.Id)); clientContext.Load(termStore, ts => ts.Id); clientContext.ExecuteQuery(); termStoreId = termStore.Id; termSetId = termSets.FirstOrDefault().Id; }
/// <summary> /// Exports the full list of terms from all termsets in all termstores. /// </summary> /// <param name="includeId">if true, Ids of the the taxonomy items will be included</param> /// <param name="clientContext"></param> /// <param name="delimiter">if specified, this delimiter will be used. Notice that IDs will be delimited with ;# from the label</param> /// <returns></returns> public static List <string> ExportAllTerms(this Site site, bool includeId, string delimiter = "|") { var clientContext = site.Context; List <string> termsString = new List <string>(); TaxonomySession taxonomySession = taxonomySession = TaxonomySession.GetTaxonomySession(clientContext); clientContext.ExecuteQuery(); TermStoreCollection termStores = taxonomySession.TermStores; clientContext.Load(termStores, t => t.IncludeWithDefaultProperties(s => s.Groups)); clientContext.ExecuteQuery(); foreach (TermStore termStore in termStores) { foreach (TermGroup termGroup in termStore.Groups) { TermSetCollection termSets = termGroup.TermSets; clientContext.Load(termSets, t => t.IncludeWithDefaultProperties(s => s.Terms)); clientContext.ExecuteQuery(); string termGroupName = DenormalizeName(termGroup.Name); string groupPath = string.Format("{0}{1}", termGroupName, (includeId) ? string.Format(";#{0}", termGroup.Id.ToString()) : ""); foreach (TermSet set in termSets) { string setName = DenormalizeName(set.Name); string termsetPath = string.Format("{0}{3}{1}{2}", groupPath, setName, (includeId) ? string.Format(";#{0}", set.Id.ToString()) : "", delimiter); foreach (Term term in set.Terms) { string termName = DenormalizeName(term.Name); string termPath = string.Format("{0}{3}{1}{2}", termsetPath, termName, (includeId) ? string.Format(";#{0}", term.Id.ToString()) : "", delimiter); termsString.Add(termPath); if (term.TermsCount > 0) { string subTermPath = string.Format("{0}{3}{1}{3}{2}", groupPath, termsetPath, termName, delimiter); termsString.AddRange(ParseSubTerms(subTermPath, term, includeId, delimiter, clientContext)); } } } } } return(termsString.Distinct().ToList <string>()); }
private void UpdateTermSetsBasedOnSelectedGroup(string groupId) { // Update Term set drop down for the taxonomy field creation. var spContext = SharePointContextProvider.Current.GetSharePointContext(Context); using (var ctx = spContext.CreateUserClientContextForSPHost()) { TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(ctx); TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore(); ctx.Load(termStore); TermGroup group = termStore.GetGroup(new Guid(groupId)); ctx.Load(group); TermSetCollection termSets = group.TermSets; ctx.Load(termSets); ctx.ExecuteQuery(); drpTermSets.DataTextField = "Name"; drpTermSets.DataValueField = "Id"; drpTermSets.DataSource = termSets.OrderBy(ts => ts.Name); drpTermSets.DataBind(); } }
/// <summary> /// Returns Taxonomy store and set details /// </summary> /// <param name="clientContext"></param> /// <param name="logger"></param> /// <param name="termSetName"></param> /// <param name="cultureId"></param> /// <returns>NULL if an Exception is thrown</returns> public static SPOTaxonomyModel GetTaxonomyFieldInfo(this ClientContext clientContext, ITraceLogger logger, string termSetName, int cultureId = 1033) { TaxonomySession session = TaxonomySession.GetTaxonomySession(clientContext); TermStore termStore = session.GetDefaultSiteCollectionTermStore(); TermSetCollection termSets = termStore.GetTermSetsByName(termSetName, cultureId); SPOTaxonomyTermSetModel modelTermSet = null; SPOTaxonomyTermStoreModel modelTermStore = null; try { clientContext.Load(termSets, tsc => tsc.Include( tctx => tctx.Id, tctx => tctx.CustomSortOrder, tctx => tctx.IsAvailableForTagging, tctx => tctx.Owner, tctx => tctx.CreatedDate, tctx => tctx.LastModifiedDate, tctx => tctx.Name, tctx => tctx.Description, tctx => tctx.TermStore, tctx => tctx.Group, tctx => tctx.IsOpenForTermCreation)); clientContext.Load(termStore, tctx => tctx.Id, tctx => tctx.Name, tctx => tctx.IsOnline, tctx => tctx.DefaultLanguage, tctx => tctx.ContentTypePublishingHub, tctx => tctx.WorkingLanguage); clientContext.ExecuteQueryRetry(); modelTermStore = new SPOTaxonomyTermStoreModel() { Id = termStore.Id, Name = termStore.Name, IsOnline = termStore.IsOnline, DefaultLanguage = termStore.DefaultLanguage, ContentTypePublishingHub = termStore.ContentTypePublishingHub, WorkingLanguage = termStore.WorkingLanguage }; TermSet termSet = termSets.FirstOrDefault(); modelTermSet = new SPOTaxonomyTermSetModel() { Id = termSet.Id, IsAvailableForTagging = termSet.IsAvailableForTagging, IsOpenForTermCreation = termSet.IsOpenForTermCreation, CustomSortOrder = termSet.CustomSortOrder, Owner = termSet.Owner, CreatedDate = termSet.CreatedDate, LastModifiedDate = termSet.LastModifiedDate, Name = termSet.Name, Description = termSet.Description }; if (termSet.TermStore != null && termSet.TermStore.Id != null) { var tempStore = termSet.TermStore; modelTermSet.TermStoreId = tempStore.Id; } if (termSet.Group != null) { var termGroup = termSet.Group; modelTermSet.Group = new SPOTaxonomyItemModel() { Id = termGroup.Id, Name = termGroup.Name, CreatedDate = termGroup.CreatedDate, LastModifiedDate = termGroup.LastModifiedDate }; } } catch (Exception ex) { logger.LogError(ex, "Failed to retreive TermStore session {0} with message {1}", termSetName, ex.Message); return(null); } // Build model var termsetModel = new SPOTaxonomyModel() { TermSetName = termSetName, TermStore = modelTermStore, TermSet = modelTermSet }; return(termsetModel); }
private void CreateTargetNewTermGroup(ClientContext sourceClientContext, ClientContext targetClientContext, TermGroup sourceTermGroup, TermStore targetTermStore) { try { this._destinationTermGroup = targetTermStore.CreateGroup(sourceTermGroup.Name, sourceTermGroup.Id); if (!string.IsNullOrEmpty(sourceTermGroup.Description)) { this._destinationTermGroup.Description = sourceTermGroup.Description; } TermOperation _op = new TermOperation(); _op.Term = sourceTermGroup.Name; _op.Id = sourceTermGroup.Id.ToString(); _op.Operation = "Add"; _op.Type = "TermGroup"; this._termStoreOperations.Add(_op); TermSetCollection _sourceTermSetCollection = sourceTermGroup.TermSets; if (_sourceTermSetCollection.Count > 0) { foreach (TermSet _sourceTermSet in _sourceTermSetCollection) { sourceClientContext.Load(_sourceTermSet, set => set.Name, set => set.Description, set => set.Id, set => set.Terms.Include( term => term.Name, term => term.Id), term => term.Description, term => term.Contact); sourceClientContext.ExecuteQuery(); TermSet _targetTermSet = _destinationTermGroup.CreateTermSet(_sourceTermSet.Name, _sourceTermSet.Id, targetTermStore.DefaultLanguage); if (!string.IsNullOrEmpty(_sourceTermSet.Description)) { _targetTermSet.Description = _sourceTermSet.Description; } foreach (Term _sourceTerm in _sourceTermSet.Terms) { Term _targetTerm = _targetTermSet.CreateTerm(_sourceTerm.Name, targetTermStore.DefaultLanguage, _sourceTerm.Id); _op = new TermOperation(); _op.Term = _sourceTerm.Name; _op.Id = _sourceTerm.Id.ToString(); _op.Operation = "Add"; _op.Type = "Term"; this._termStoreOperations.Add(_op); } } } try { targetClientContext.ExecuteQuery(); targetTermStore.CommitAll(); } catch { throw; } } catch { throw; } }
static void Main(string[] args) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Enter your user name"); // Console.ForegroundColor = defaultForeground; string userName = Console.ReadLine(); Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Enter your password."); // Console.ForegroundColor = defaultForeground; SecureString password = GetPasswordFromConsoleInput(); using (context = new ClientContext("https://microsoft.sharepoint.com/teams/USDXISVCJ/")) { context.Credentials = new SharePointOnlineCredentials(userName, password); Web currentWeb = context.Web; context.Load(currentWeb); context.ExecuteQuery(); TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(currentWeb.Context); TermStoreCollection termStores = taxonomySession.TermStores; currentWeb.Context.Load(termStores); currentWeb.Context.ExecuteQuery(); TermStore termStore = termStores[0]; currentWeb.Context.Load(termStore); currentWeb.Context.ExecuteQuery(); TermGroupCollection termGroups = termStore.Groups; currentWeb.Context.Load(termGroups); currentWeb.Context.ExecuteQuery(); TermGroup group = termGroups.GetByName("Site Collection - microsoft.sharepoint.com-teams-USDXISVCJ"); TermSetCollection termSets = group.TermSets; currentWeb.Context.Load(termSets); currentWeb.Context.ExecuteQuery(); TermCollection terms = termSets.GetByName("Workload").Terms; currentWeb.Context.Load(terms); currentWeb.Context.ExecuteQuery(); Console.WriteLine(terms.Count); foreach (Term term in terms) { Console.WriteLine(term.Name); } } }