/// <summary> /// Tokenizes the taxonomy field. /// </summary> /// <param name="web">The web.</param> /// <param name="element">The element.</param> /// <returns></returns> protected string TokenizeTaxonomyField(Web web, XElement element) { // Replace Taxonomy field references to SspId, TermSetId with tokens TaxonomySession session = TaxonomySession.GetTaxonomySession(web.Context); TermStore store = session.GetDefaultSiteCollectionTermStore(); var sspIdElement = element.XPathSelectElement("./Customization/ArrayOfProperty/Property[Name = 'SspId']/Value"); if (sspIdElement != null) { sspIdElement.Value = "{sitecollectiontermstoreid}"; } var termSetIdElement = element.XPathSelectElement("./Customization/ArrayOfProperty/Property[Name = 'TermSetId']/Value"); if (termSetIdElement != null) { Guid termSetId = Guid.Parse(termSetIdElement.Value); if (termSetId != Guid.Empty) { Microsoft.SharePoint.Client.Taxonomy.TermSet termSet = store.GetTermSet(termSetId); store.Context.ExecuteQueryRetry(); if (!termSet.ServerObjectIsNull()) { termSet.EnsureProperties(ts => ts.Name, ts => ts.Group); termSetIdElement.Value = String.Format("{{termsetid:{0}:{1}}}", termSet.Group.IsSiteCollectionGroup ? "{sitecollectiontermgroupname}" : termSet.Group.Name, termSet.Name); } } } return(element.ToString()); }
public static void CreateTermSets(TokenParser _TokenParser, XElement _Branding, ClientContext clientContext, string termGroupName) { Console.WriteLine("Processing Term Sets . . . "); // This code assumes: // managed metadata service is running on the farm // default termstore exists for site collection // permission to managed metadata service have been granted // start a taxonomy session and connect to the Term Store TaxonomySession taxonomySession = TaxonomyExtensions.GetTaxonomySession(clientContext.Site); TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore(); // connect to Site Collection Terms (aka Term Group with Site Collection Name) Microsoft.SharePoint.Client.Taxonomy.TermGroup termGroup = termStore.GetTermGroupByName(termGroupName); //process each termset foreach (var termset in _Branding.GetDescendants("termsets", "termset")) { // fetch file path string termSetFilePath = termset.GetAttributeValue(_TokenParser, "termSetFilePath"); Console.WriteLine("Creating Term Set from contents of: {0}", termSetFilePath); // Create TermSet via File Import Microsoft.SharePoint.Client.Taxonomy.TermSet termSet = TaxonomyExtensions.ImportTermSet(termGroup, termSetFilePath); } }
// Retrieved target xml data is not matching with source xml navigation types so changing navigation settings to get correct data. public void ChangeNavigationSettings(ClientContext cc, StandardNavigationSource gSource, StandardNavigationSource cSource) { TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(cc); taxonomySession.UpdateCache(); cc.Load(taxonomySession, ts => ts.TermStores); cc.ExecuteQueryRetry(); var navigationSettings = new WebNavigationSettings(cc, cc.Web); navigationSettings.GlobalNavigation.Source = gSource; navigationSettings.CurrentNavigation.Source = cSource; navigationSettings.Update(taxonomySession); try { cc.ExecuteQueryRetry(); } catch (Exception) // if termset not found then set newly created termset to managed navigation { TermStore termStore = taxonomySession.GetDefaultSiteCollectionTermStore(); Microsoft.SharePoint.Client.Taxonomy.TermGroup group = termStore.GetTermGroupByName("TG_1"); // TG_1 is a term group mentioned in navigation_add_1605.xml Microsoft.SharePoint.Client.Taxonomy.TermSet termset = group.TermSets.GetByName("TS_1_1"); // TS_1_1 is a term set mentioned in navigation_add_1605.xml cc.Load(termStore); cc.Load(group, g => g.TermSets); cc.Load(termset); cc.ExecuteQuery(); if (StandardNavigationSource.TaxonomyProvider == gSource) { navigationSettings.GlobalNavigation.TermStoreId = termStore.Id; navigationSettings.GlobalNavigation.TermSetId = termset.Id; } if (StandardNavigationSource.TaxonomyProvider == cSource) { navigationSettings.CurrentNavigation.TermStoreId = termStore.Id; navigationSettings.CurrentNavigation.TermSetId = termset.Id; } navigationSettings.GlobalNavigation.Source = gSource; navigationSettings.CurrentNavigation.Source = cSource; navigationSettings.Update(taxonomySession); cc.ExecuteQueryRetry(); } }
static void CreateTerms(ClientContext ctx) { TermStore store = ctx.Site.GetDefaultSiteCollectionTermStore(); Microsoft.SharePoint.Client.Taxonomy.TermGroup group = store.GetTermGroupByName("Tim"); if (group == null) { group = store.CreateTermGroup("Tim", "{9285FBFD-F1B1-44CA-ACFF-8CF4B271A5C2}".ToGuid()); } Microsoft.SharePoint.Client.Taxonomy.TermSet term = group.EnsureTermSet("Animals", "{FCB857B8-8F82-4EDD-B49A-5A5A5D492174}".ToGuid(), 1033); term.CreateTerm("Dog", 1033, "{E6251A5B-8341-4FC9-9544-30670C5E115B}".ToGuid()); Microsoft.SharePoint.Client.Taxonomy.Term cat = term.CreateTerm("Cat", 1033, "{F1D30452-0DD1-4B74-89CA-62167065BFF6}".ToGuid()); term.CreateTerm("Horse", 1033, "{25E73090-CFEF-4A83-997D-56FB127F0B82}".ToGuid()); ctx.ExecuteQuery(); cat.CreateLabel("Katt", 1053, false); cat.CreateLabel("Feline", 1033, false); ctx.ExecuteQuery(); }
static void CreateTaxonomyField(ClientContext ctx) { //ctx.Web.GetFieldById("{4306F426-A772-4D1A-91D1-07F4CAA8884D}".ToGuid()).DeleteObject(); TermStore store = ctx.Site.GetDefaultSiteCollectionTermStore(); //guid is from the termset i created below Microsoft.SharePoint.Client.Taxonomy.TermSet term = store.GetTermSet("{FCB857B8-8F82-4EDD-B49A-5A5A5D492174}".ToGuid()); ctx.Load(term); ctx.ExecuteQuery(); TaxonomyFieldCreationInformation info = new TaxonomyFieldCreationInformation(); info.DisplayName = "Animal"; //field term id. new guid info.Id = "{4306F426-A772-4D1A-91D1-07F4CAA8884D}".ToGuid(); info.InternalName = "TIM_TaxAnimal"; //connect it to the termset we created below info.TaxonomyItem = term; info.Group = "Tims Fields"; ctx.Web.CreateTaxonomyField(info); }
public override ProvisioningTemplate ExtractObjects(Web web, ProvisioningTemplate template, ProvisioningTemplateCreationInformation creationInfo) { using (var scope = new PnPMonitoredScope(this.Name)) { // if this is a sub site then we're not creating field entities. if (web.IsSubSite()) { scope.LogDebug(CoreResources.Provisioning_ObjectHandlers_Fields_Context_web_is_subweb__skipping_site_columns); return(template); } var existingFields = web.Fields; web.Context.Load(web, w => w.ServerRelativeUrl); web.Context.Load(existingFields, fs => fs.Include(f => f.Id, f => f.SchemaXml, f => f.TypeAsString, f => f.InternalName, f => f.Title)); web.Context.Load(web.Lists, ls => ls.Include(l => l.Id, l => l.Title)); web.Context.ExecuteQueryRetry(); var taxTextFieldsToMoveUp = new List <Guid>(); foreach (var field in existingFields) { if (!BuiltInFieldId.Contains(field.Id)) { var fieldXml = field.SchemaXml; XElement element = XElement.Parse(fieldXml); // Check if the field contains a reference to a list. If by Guid, rewrite the value of the attribute to use web relative paths var listIdentifier = element.Attribute("List") != null?element.Attribute("List").Value : null; if (!string.IsNullOrEmpty(listIdentifier)) { var listGuid = Guid.Empty; fieldXml = ParseFieldSchema(fieldXml, web.Lists); element = XElement.Parse(fieldXml); //if (Guid.TryParse(listIdentifier, out listGuid)) //{ // fieldXml = ParseListSchema(fieldXml, web.Lists); //if (newfieldXml == fieldXml) //{ // var list = web.Lists.GetById(listGuid); // web.Context.Load(list, l => l.RootFolder.ServerRelativeUrl); // web.Context.ExecuteQueryRetry(); // var listUrl = list.RootFolder.ServerRelativeUrl.Substring(web.ServerRelativeUrl.Length).TrimStart('/'); // element.Attribute("List").SetValue(listUrl); // fieldXml = element.ToString(); //} //} } // Check if the field is of type TaxonomyField if (field.TypeAsString.StartsWith("TaxonomyField")) { var taxField = (TaxonomyField)field; web.Context.Load(taxField, tf => tf.TextField, tf => tf.Id); web.Context.ExecuteQueryRetry(); taxTextFieldsToMoveUp.Add(taxField.TextField); // Replace Taxonomy field references to SspId, TermSetId with tokens TaxonomySession session = TaxonomySession.GetTaxonomySession(web.Context); TermStore store = session.GetDefaultSiteCollectionTermStore(); var sspIdElement = element.XPathSelectElement("./Customization/ArrayOfProperty/Property[Name = 'SspId']/Value"); if (sspIdElement != null) { sspIdElement.Value = "{sitecollectiontermstoreid}"; } var termSetIdElement = element.XPathSelectElement("./Customization/ArrayOfProperty/Property[Name = 'TermSetId']/Value"); if (termSetIdElement != null) { Guid termSetId = Guid.Parse(termSetIdElement.Value); if (termSetId != Guid.Empty) { Microsoft.SharePoint.Client.Taxonomy.TermSet termSet = store.GetTermSet(termSetId); if (!termSet.ServerObjectIsNull()) { termSet.EnsureProperties(ts => ts.Name, ts => ts.Group); termSetIdElement.Value = String.Format("{{termsetid:{0}:{1}}}", termSet.Group.Name, termSet.Name); // TODO } } } } // Check if we have version attribute. Remove if exists if (element.Attribute("Version") != null) { element.Attributes("Version").Remove(); fieldXml = element.ToString(); } if (element.Attribute("Type").Value == "Calculated") { fieldXml = TokenizeFieldFormula(fieldXml); } template.SiteFields.Add(new Field() { SchemaXml = fieldXml }); } } // move hidden taxonomy text fields to the top of the list foreach (var textFieldId in taxTextFieldsToMoveUp) { var field = template.SiteFields.First(f => Guid.Parse(f.SchemaXml.ElementAttributeValue("ID")).Equals(textFieldId)); template.SiteFields.RemoveAll(f => Guid.Parse(f.SchemaXml.ElementAttributeValue("ID")).Equals(textFieldId)); template.SiteFields.Insert(0, field); } // If a base template is specified then use that one to "cleanup" the generated template model if (creationInfo.BaseTemplate != null) { template = CleanupEntities(template, creationInfo.BaseTemplate); } } return(template); }