/// <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());
        }
Example #2
0
        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);
        }