Пример #1
0
        private static void ProcessGroup(XmlNode termGroup)
        {
            if (termGroup != null)
            {
                string strGroupName = termGroup.Attributes["Name"].Value;

                //Get TermStore Groups
                GroupCollection groups = termStore.Groups;

                //Find group that we want to Import to
                Group thisGroup = groups.Where(g => g.Name == strGroupName).SingleOrDefault();

                //Check that group exist
                if (thisGroup != null)
                {
                    //Get Current group
                    group = termStore.GetGroup(thisGroup.Id);
                }
                else //If group doesn't exist, create it
                {
                    group = termStore.CreateGroup(strGroupName);
                    termStore.CommitAll();
                }
                Console.WriteLine(strGroupName);

                ProcessTermSet(termGroup);
            }
        }
        public TermGroupInstance GetGroup(object id)
        {
            var guid  = GuidInstance.ConvertFromJsObjectToGuid(id);
            var group = m_termStore.GetGroup(guid);

            return(new TermGroupInstance(Engine.Object.InstancePrototype, group));
        }
Пример #3
0
        public static bool MakeTermSet(TermStore termstore, string _group, string _termset)
        {
            LogHelper logger = LogHelper.Instance;

            bool _result = false;

            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    Guid termSetId = Guid.NewGuid();
                    Guid uidStore = GetIdTermStoreGroup(termstore, _group);
                    termstore.GetGroup(uidStore).CreateTermSet(_termset, termSetId, termstore.DefaultLanguage);
                    termstore.CommitAll();
                });

                _result = true;
                logger.Log(string.Format("Set Termini {0} creato nel Gruppo {1}", _termset, _group, LogSeverity.Debug));
            }
            catch (Exception ex)
            {
                _result = false;
                logger.Log(ex.Message + " : " + string.Format("Set Termini {0} non creato nel Gruppo {1}", _termset, _group, LogSeverity.Error));
            }
            return _result;            
        }
Пример #4
0
        public static void CreateTaxonomy(ClientContext ctx)
        {
            TermStore store = ctx.Site.GetDefaultSiteCollectionTermStore();

            TermGroup group = store.GetGroup("{0671B184-F909-4DCA-B383-6E05C969BEF1}".ToGuid());

            ctx.Load(group);
            ctx.ExecuteQuery();
            if (group.ServerObjectIsNull())
            {
                group = store.CreateTermGroup("David1", "{0671B184-F909-4DCA-B383-6E05C969BEF1}".ToGuid());
            }

            TermSet ts = group.EnsureTermSet("Color", "{19419441-6982-4978-8800-F12A46E6FE37}".ToGuid(), 1033);

            // Term term = ts.CreateTerm("Green", 1033, "{FA6628FA-9790-4622-9B54-23D103A67FCD}".ToGuid());
            // Term term2 = ts.CreateTerm("Röd", 1053, "{24FA5756-25E8-4257-A4E5-D53C3D48B595}".ToGuid());
            // ctx.ExecuteQuery();

            // term2.EnsureLabel(1033, "Red", false);

            Term term2 = ts.CreateTerm("Blue", 1033, "{73E90F88-BD96-4066-8237-2D0DAAE3F11C}".ToGuid());

            ctx.ExecuteQuery();

            term2.EnsureLabel(1053, "Blå", false);
        }
Пример #5
0
        private TermSetItem getTerCollByID(Guid termsetID, string TermstoreName, Guid termgroupId)
        {
            Term retColl = null;

            try
            {
                //TermStore _TermStore = _TaxonomySession.TermStores[TermstoreName];

                Group gp = _TermStore.GetGroup(termgroupId);
                _TermStore.GetTermSets(new[] { "Legislation" });
                foreach (TermSet item in _TermStore.GetTermSets(new[] { "Legislation" })) //gp.TermSets)
                {
                    Term tr = findRecursive(item, termsetID);
                    if (tr != null)
                    {
                        return(tr);
                    }
                    string s = item.Name;
                }
                return(retColl);
            }
            catch (Exception w)
            {
                _isValidState = false;
                log.Error(string.Format("{0}.{1}", MethodBase.GetCurrentMethod().DeclaringType.FullName, MethodBase.GetCurrentMethod().Name), w);
                throw;
            }
        }
Пример #6
0
        public static Term LookupTerm(SPSite site, TermStore tesmStore, TaxonomyFieldDefinition taxFieldModel)
        {
            Group currentGroup = null;

            var termGroupName         = taxFieldModel.TermGroupName;
            var groupId               = taxFieldModel.TermGroupId;
            var isSiteCollectionGroup = taxFieldModel.IsSiteCollectionGroup;

            if (!string.IsNullOrEmpty(termGroupName))
            {
                currentGroup = tesmStore.Groups.FirstOrDefault(g => g.Name.ToUpper() == termGroupName.ToUpper());
            }
            else if (groupId != null && groupId.HasGuidValue())
            {
                currentGroup = tesmStore.GetGroup(groupId.Value);
            }
            else if (isSiteCollectionGroup.HasValue && isSiteCollectionGroup.Value)
            {
                currentGroup = tesmStore.GetSiteCollectionGroup(site);
            }

            // TODO
            // that should also check if the TermSet is there, so to scope the term

            if (currentGroup != null)
            {
                if (taxFieldModel.TermId.HasValue)
                {
                    return(tesmStore.GetTerm(taxFieldModel.TermId.Value));
                }

                if (!string.IsNullOrEmpty(taxFieldModel.TermName))
                {
                    return(tesmStore.GetTerms(taxFieldModel.TermName, taxFieldModel.TermLCID, false)
                           .FirstOrDefault(t => t.TermSet.Group.Name == currentGroup.Name));
                }
            }
            else
            {
                if (taxFieldModel.TermId.HasValue)
                {
                    return(tesmStore.GetTerm(taxFieldModel.TermId.Value));
                }

                if (!string.IsNullOrEmpty(taxFieldModel.TermName))
                {
                    return(tesmStore.GetTerms(taxFieldModel.TermName, taxFieldModel.TermLCID, false).FirstOrDefault());
                }
            }

            return(null);
        }
Пример #7
0
        protected Group FindGroup(TermStore termStore, TaxonomyTermGroupDefinition groupModel)
        {
            Group currentGroup = null;

            if (groupModel.Id.HasValue)
            {
                currentGroup = termStore.GetGroup(groupModel.Id.Value);
            }
            else if (!string.IsNullOrEmpty(groupModel.Name))
            {
                currentGroup = termStore.Groups.FirstOrDefault(g => g.Name.ToUpper() == groupModel.Name.ToUpper());
            }

            return(currentGroup);
        }
Пример #8
0
        private static TermGroup AddGroup(ClientContext clientContext, TermStore termStore)
        {
            var groupId = new Guid("8de44223-5a8f-41cd-b0e2-5634b0bb953b");
            var termGroup = termStore.GetGroup(groupId);
            clientContext.Load(termGroup);
            clientContext.ExecuteQuery();

            if (termGroup.ServerObjectIsNull.Value)
            {
                termGroup = termStore.CreateGroup("Taxonomy Navigation", groupId);
                clientContext.Load(termGroup);
                clientContext.ExecuteQuery();
            }

            return termGroup;
        }
        public static Term LookupTerm(SPSite site, TermStore tesmStore, TaxonomyFieldDefinition taxFieldModel)
        {
            Group currentGroup = null;

            var termGroupName = taxFieldModel.TermGroupName;
            var groupId = taxFieldModel.TermGroupId;
            var isSiteCollectionGroup = taxFieldModel.IsSiteCollectionGroup;

            if (!string.IsNullOrEmpty(termGroupName))
            {
                currentGroup = tesmStore.Groups.FirstOrDefault(g => g.Name.ToUpper() == termGroupName.ToUpper());
            }
            else if (groupId != null && groupId.HasGuidValue())
            {
                currentGroup = tesmStore.GetGroup(groupId.Value);
            }
            else if (isSiteCollectionGroup.HasValue && isSiteCollectionGroup.Value)
            {
                currentGroup = tesmStore.GetSiteCollectionGroup(site);
            }

            // TODO
            // that should also check if the TermSet is there, so to scope the term 

            if (currentGroup != null)
            {
                if (taxFieldModel.TermId.HasValue)
                    return tesmStore.GetTerm(taxFieldModel.TermId.Value);

                if (!string.IsNullOrEmpty(taxFieldModel.TermName))
                {
                    return tesmStore.GetTerms(taxFieldModel.TermName, taxFieldModel.TermLCID, false)
                                    .FirstOrDefault(t => t.TermSet.Group.Name == currentGroup.Name);
                }
            }
            else
            {
                if (taxFieldModel.TermId.HasValue)
                    return tesmStore.GetTerm(taxFieldModel.TermId.Value);

                if (!string.IsNullOrEmpty(taxFieldModel.TermName))
                    return tesmStore.GetTerms(taxFieldModel.TermName, taxFieldModel.TermLCID, false).FirstOrDefault();
            }

            return null;
        }
Пример #10
0
        protected void btnUploadTermSet_Click(object sender, EventArgs e)
        {
            // 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();
                TermGroup       group           = termStore.GetGroup(new Guid(drpGroups.SelectedValue));

                group.ImportTermSet(Server.MapPath("~/Resources/ImportTermSet.csv"),
                                    termSetId: SampleTermSetId, synchroniseDeletions: true);
            }
            // Update term setup drop down
            UpdateTermSetsBasedOnSelectedGroup(drpGroups.SelectedValue);
        }
Пример #11
0
        private static TermGroup AddGroup(ClientContext clientContext, TermStore termStore)
        {
            var groupId   = new Guid("8de44223-5a8f-41cd-b0e2-5634b0bb953b");
            var termGroup = termStore.GetGroup(groupId);

            clientContext.Load(termGroup);
            clientContext.ExecuteQuery();

            if (termGroup.ServerObjectIsNull.Value)
            {
                termGroup = termStore.CreateGroup("Taxonomy Navigation", groupId);
                clientContext.Load(termGroup);
                clientContext.ExecuteQuery();
            }

            return(termGroup);
        }
Пример #12
0
        public static TermSet LookupTermSet(
            TermStore tesmStore,
            string termGroupName, Guid?groupId, bool?isSiteCollectionGroup, SPSite site,
            string termSetName, Guid?termSetId, int termSetLCID)
        {
            Group currentGroup = null;

            if (!string.IsNullOrEmpty(termGroupName))
            {
                currentGroup = tesmStore.Groups.FirstOrDefault(g => g.Name.ToUpper() == termGroupName.ToUpper());
            }
            else if (groupId != null && groupId.HasGuidValue())
            {
                currentGroup = tesmStore.GetGroup(groupId.Value);
            }
            else if (isSiteCollectionGroup.HasValue && isSiteCollectionGroup.Value)
            {
                currentGroup = tesmStore.GetSiteCollectionGroup(site);
            }

            if (termSetId.HasGuidValue())
            {
                if (currentGroup != null)
                {
                    return(currentGroup.TermSets.FirstOrDefault(t => t.Id == termSetId.Value));
                }

                return(tesmStore.GetTermSet(termSetId.Value));
            }

            if (!string.IsNullOrEmpty(termSetName))
            {
                if (currentGroup != null)
                {
                    return(currentGroup.TermSets.FirstOrDefault(t => t.Name.ToUpper() == termSetName.ToUpper()));
                }

                return(tesmStore.GetTermSets(termSetName, termSetLCID).FirstOrDefault());
            }

            return(null);
        }
Пример #13
0
        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();
            }
        }
Пример #14
0
        public override Model.ProvisioningTemplate ExtractObjects(Web web, Model.ProvisioningTemplate template, ProvisioningTemplateCreationInformation creationInfo)
        {
            using (var scope = new PnPMonitoredScope(this.Name))
            {
                if (creationInfo.IncludeSiteCollectionTermGroup || creationInfo.IncludeAllTermGroups)
                {
                    // Find the site collection termgroup, if any
                    TaxonomySession session   = TaxonomySession.GetTaxonomySession(web.Context);
                    TermStore       termStore = null;

                    try
                    {
                        termStore = session.GetDefaultSiteCollectionTermStore();
                        web.Context.Load(termStore, t => t.Id, t => t.DefaultLanguage, t => t.OrphanedTermsTermSet);
                        web.Context.ExecuteQueryRetry();
                    }
                    catch (ServerException)
                    {
                        // Skip the exception and go to the next check
                    }

                    if (null == termStore || termStore.ServerObjectIsNull())
                    {
                        // If the GetDefaultSiteCollectionTermStore method call fails ... raise a specific Warning
                        WriteMessage(CoreResources.Provisioning_ObjectHandlers_TermGroups_Wrong_Configuration, ProvisioningMessageType.Warning);

                        // and exit skipping the current handler
                        return(template);
                    }

                    var orphanedTermsTermSetId = default(Guid);
                    if (!termStore.OrphanedTermsTermSet.ServerObjectIsNull())
                    {
                        termStore.OrphanedTermsTermSet.EnsureProperty(ts => ts.Id);
                        orphanedTermsTermSetId = termStore.OrphanedTermsTermSet.Id;
                        if (termStore.ServerObjectIsNull.Value)
                        {
                            termStore = session.GetDefaultKeywordsTermStore();
                            web.Context.Load(termStore, t => t.Id, t => t.DefaultLanguage);
                            web.Context.ExecuteQueryRetry();
                        }
                    }

                    var propertyBagKey = $"SiteCollectionGroupId{termStore.Id}";

                    // Ensure to grab the property from the rootweb
                    var site = (web.Context as ClientContext).Site;
                    web.Context.Load(site, s => s.RootWeb);
                    web.Context.ExecuteQueryRetry();

                    var siteCollectionTermGroupId = site.RootWeb.GetPropertyBagValueString(propertyBagKey, "");

                    Guid termGroupGuid;
                    Guid.TryParse(siteCollectionTermGroupId, out termGroupGuid);

                    List <TermGroup> termGroups = new List <TermGroup>();
                    if (creationInfo.IncludeAllTermGroups)
                    {
                        web.Context.Load(termStore.Groups, groups => groups.Include(tg => tg.Name,
                                                                                    tg => tg.Id,
                                                                                    tg => tg.Description,
                                                                                    tg => tg.TermSets.IncludeWithDefaultProperties(ts => ts.CustomSortOrder)));
                        web.Context.ExecuteQueryRetry();
                        termGroups = termStore.Groups.ToList();
                    }
                    else
                    {
                        if (termGroupGuid != Guid.Empty)
                        {
                            var termGroup = termStore.GetGroup(termGroupGuid);
                            web.Context.Load(termGroup,
                                             tg => tg.Name,
                                             tg => tg.Id,
                                             tg => tg.Description,
                                             tg => tg.TermSets.IncludeWithDefaultProperties(ts => ts.Description, ts => ts.CustomSortOrder));

                            web.Context.ExecuteQueryRetry();

                            termGroups = new List <TermGroup>()
                            {
                                termGroup
                            };
                        }
                    }

                    foreach (var termGroup in termGroups)
                    {
                        Boolean isSiteCollectionTermGroup = termGroupGuid != Guid.Empty && termGroup.Id == termGroupGuid;

                        var modelTermGroup = new Model.TermGroup
                        {
                            Name                    = isSiteCollectionTermGroup ? "{sitecollectiontermgroupname}" : termGroup.Name,
                            Id                      = isSiteCollectionTermGroup ? Guid.Empty : termGroup.Id,
                            Description             = termGroup.Description,
                            SiteCollectionTermGroup = isSiteCollectionTermGroup
                        };

#if !ONPREMISES
                        // If we need to include TermGroups security
                        if (creationInfo.IncludeTermGroupsSecurity)
                        {
                            termGroup.EnsureProperties(tg => tg.ContributorPrincipalNames, tg => tg.GroupManagerPrincipalNames);

                            // Extract the TermGroup contributors
                            modelTermGroup.Contributors.AddRange(
                                from c in termGroup.ContributorPrincipalNames
                                select new Model.User {
                                Name = c
                            });

                            // Extract the TermGroup managers
                            modelTermGroup.Managers.AddRange(
                                from m in termGroup.GroupManagerPrincipalNames
                                select new Model.User {
                                Name = m
                            });
                        }
#endif

                        web.EnsureProperty(w => w.Url);

                        foreach (var termSet in termGroup.TermSets)
                        {
                            // Do not include the orphan term set
                            if (termSet.Id == orphanedTermsTermSetId)
                            {
                                continue;
                            }

                            // Extract all other term sets
                            var modelTermSet = new Model.TermSet();
                            modelTermSet.Name = termSet.Name;
                            if (!isSiteCollectionTermGroup)
                            {
                                modelTermSet.Id = termSet.Id;
                            }
                            modelTermSet.IsAvailableForTagging = termSet.IsAvailableForTagging;
                            modelTermSet.IsOpenForTermCreation = termSet.IsOpenForTermCreation;
                            modelTermSet.Description           = termSet.Description;
                            modelTermSet.Terms.AddRange(GetTerms <TermSet>(web.Context, termSet, termStore.DefaultLanguage, isSiteCollectionTermGroup));
                            foreach (var property in termSet.CustomProperties)
                            {
                                if (property.Key.Equals("_Sys_Nav_AttachedWeb_SiteId", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    modelTermSet.Properties.Add(property.Key, "{sitecollectionid}");
                                }
                                else if (property.Key.Equals("_Sys_Nav_AttachedWeb_WebId", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    modelTermSet.Properties.Add(property.Key, "{siteid}");
                                }
                                else
                                {
                                    modelTermSet.Properties.Add(property.Key, Tokenize(property.Value, web.Url, web));
                                }
                            }
                            modelTermGroup.TermSets.Add(modelTermSet);
                        }

                        template.TermGroups.Add(modelTermGroup);
                    }
                }
            }
            return(template);
        }
Пример #15
0
        public static void ImportTerms(this Site site, string[] termLines, int lcid, string delimiter)
        {
            var             clientContext   = site.Context;
            TaxonomySession taxonomySession = TaxonomySession.GetTaxonomySession(clientContext);
            TermStore       termStore       = taxonomySession.GetDefaultSiteCollectionTermStore();

            clientContext.Load(termStore);
            clientContext.ExecuteQuery();
            foreach (string line in termLines)
            {
                // split up
                string[] items = line.Split(new string[] { delimiter }, StringSplitOptions.None);
                if (items.Count() > 0)
                {
                    string groupItem = items[0];
                    string groupName = groupItem;
                    Guid   groupId   = Guid.Empty;
                    if (groupItem.IndexOf(";#") > -1)
                    {
                        groupName = groupItem.Split(new string[] { ";#" }, StringSplitOptions.None)[0];
                        groupId   = new Guid(groupItem.Split(new string[] { ";#" }, StringSplitOptions.None)[1]);
                    }
                    TermGroup termGroup = null;
                    if (groupId != Guid.Empty)
                    {
                        termGroup = termStore.GetGroup(groupId);
                    }
                    else
                    {
                        termGroup = termStore.Groups.GetByName(NormalizeName(groupName));
                    }
                    try
                    {
                        clientContext.Load(termGroup);
                        clientContext.ExecuteQuery();
                    }
                    catch
                    {
                    }
                    if (termGroup.ServerObjectIsNull == null)
                    {
                        groupId   = Guid.NewGuid();
                        termGroup = termStore.CreateGroup(NormalizeName(groupName), groupId);
                        clientContext.Load(termGroup);
                        clientContext.ExecuteQuery();
                    }
                    if (items.Count() > 1)
                    {
                        // TermSet
                        if (termGroup.ServerObjectIsNull == false)
                        {
                            string termsetItem = items[1];
                            string termsetName = termsetItem;
                            Guid   termsetId   = Guid.Empty;
                            if (termsetItem.IndexOf(";#") > -1)
                            {
                                termsetName = termsetItem.Split(new string[] { ";#" }, StringSplitOptions.None)[0];
                                termsetId   = new Guid(termsetItem.Split(new string[] { ";#" }, StringSplitOptions.None)[1]);
                            }
                            TermSet termSet = null;
                            if (termsetId != Guid.Empty)
                            {
                                termSet = termGroup.TermSets.GetById(termsetId);
                            }
                            else
                            {
                                termSet = termGroup.TermSets.GetByName(NormalizeName(termsetName));
                            }
                            clientContext.Load(termSet);
                            try
                            {
                                clientContext.ExecuteQuery();
                            }
                            catch { }
                            if (termSet.ServerObjectIsNull == null)
                            {
                                termsetId = Guid.NewGuid();
                                termSet   = termGroup.CreateTermSet(NormalizeName(termsetName), termsetId, lcid);
                                clientContext.Load(termSet);
                                clientContext.ExecuteQuery();
                            }
                            if (items.Count() > 2)
                            {
                                // Term(s)

                                if (termSet.ServerObjectIsNull == false)
                                {
                                    string termItem = items[2];
                                    string termName = termItem;
                                    Guid   termId   = Guid.Empty;
                                    if (termItem.IndexOf(";#") > -1)
                                    {
                                        termName = termItem.Split(new string[] { ";#" }, StringSplitOptions.None)[0];
                                        termId   = new Guid(termItem.Split(new string[] { ";#" }, StringSplitOptions.None)[1]);
                                    }
                                    Term term = null;
                                    if (termId != Guid.Empty)
                                    {
                                        term = termSet.Terms.GetById(termId);
                                    }
                                    else
                                    {
                                        term = termSet.Terms.GetByName(NormalizeName(termName));
                                    }
                                    clientContext.Load(term);
                                    try
                                    {
                                        clientContext.ExecuteQuery();
                                    }
                                    catch { }
                                    if (term.ServerObjectIsNull == null)
                                    {
                                        termId = Guid.NewGuid();
                                        term   = termSet.CreateTerm(NormalizeName(termName), lcid, termId);
                                        clientContext.ExecuteQuery();
                                    }

                                    if (items.Count() > 3)
                                    {
                                        clientContext.Load(term);
                                        clientContext.ExecuteQuery();
                                        if (term.ServerObjectIsNull == false)
                                        {
                                            for (int q = 3; q < items.Count(); q++)
                                            {
                                                termName = items[q];
                                                termId   = Guid.Empty;
                                                if (termItem.IndexOf(";#") > -1)
                                                {
                                                    termName = termItem.Split(new string[] { ";#" }, StringSplitOptions.None)[0];
                                                    termId   = new Guid(termItem.Split(new string[] { ";#" }, StringSplitOptions.None)[1]);
                                                }
                                                term = term.AddTermToTerm(lcid, termName, termId);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #16
0
        public static TermSet LookupTermSet(
            TermStore tesmStore,
            string termGroupName, Guid? groupId, bool? isSiteCollectionGroup, SPSite site,
            string termSetName, Guid? termSetId, int termSetLCID)
        {
            Group currentGroup = null;

            if (!string.IsNullOrEmpty(termGroupName))
            {
                currentGroup = tesmStore.Groups.FirstOrDefault(g => g.Name.ToUpper() == termGroupName.ToUpper());
            }
            else if (groupId != null && groupId.HasGuidValue())
            {
                currentGroup = tesmStore.GetGroup(groupId.Value);
            }
            else if (isSiteCollectionGroup.HasValue && isSiteCollectionGroup.Value)
            {
                currentGroup = tesmStore.GetSiteCollectionGroup(site);
            }

            if (termSetId.HasGuidValue())
            {
                if (currentGroup != null)
                    return currentGroup.TermSets.FirstOrDefault(t => t.Id == termSetId.Value);

                return tesmStore.GetTermSet(termSetId.Value);
            }

            if (!string.IsNullOrEmpty(termSetName))
            {
                if (currentGroup != null)
                    return currentGroup.TermSets.FirstOrDefault(t => t.Name.ToUpper() == termSetName.ToUpper());

                return tesmStore.GetTermSets(termSetName, termSetLCID).FirstOrDefault();
            }

            return null;
        }
Пример #17
0
 public static Guid GetIdTermSet(TermStore termstore, string _group, string _termset)
 {
     Guid uidStore = new Guid(idStore);
     Group group = termstore.GetGroup(GetIdTermStoreGroup(termstore, _group));
     return (group.TermSets.Any(x => x.Name == _termset)) ? group.TermSets[_termset].Id : uidStore;
 }
Пример #18
0
 public static bool CheckTermSet(TermStore termstore, string _group, string _termset)
 {
     return (termstore.GetGroup(GetIdTermStoreGroup(termstore, _group)).TermSets.Any(x => x.Name == _termset)) ? true:false;
 }
Пример #19
0
        private void DeleteGroupIfExists(TermStore defaultSiteCollectionTermStore, Guid testGroupId)
        {
            Group existingTestGroup = defaultSiteCollectionTermStore.GetGroup(testGroupId);
            if (existingTestGroup != null)
            {
                foreach (var termSet in existingTestGroup.TermSets)
                {
                    termSet.Delete();
                }

                existingTestGroup.Delete();
                defaultSiteCollectionTermStore.CommitAll();
            }
        }
Пример #20
0
        public void ProcessChanges(ClientContext sourceClientContext, ClientContext targetClientContext, List <string> termSetNames)
        {
            List <TermOperation> _list      = new List <TermOperation>();
            DateTime             _startFrom = DateTime.Now.AddYears(-1);

            Console.WriteLine("Opening the taxonomy session");
            TaxonomySession _sourceTaxonomySession = TaxonomySession.GetTaxonomySession(sourceClientContext);
            TermStore       sourceTermStore        = _sourceTaxonomySession.GetDefaultKeywordsTermStore();

            sourceClientContext.Load(sourceTermStore);
            sourceClientContext.ExecuteQuery();

            Console.WriteLine("Reading the changes");
            ChangeInformation _ci = new ChangeInformation(sourceClientContext);

            _ci.StartTime = _startFrom;
            ChangedItemCollection _cic = sourceTermStore.GetChanges(_ci);

            sourceClientContext.Load(_cic);
            sourceClientContext.ExecuteQuery();

            if (_cic.Count > 0)
            {
                bool noError = true;
                // Load up the taxonomy item names.
                TaxonomySession targetTaxonomySession = TaxonomySession.GetTaxonomySession(targetClientContext);
                TermStore       targetTermStore       = targetTaxonomySession.GetDefaultKeywordsTermStore();
                targetClientContext.Load(targetTermStore,
                                         store => store.Name,
                                         store => store.DefaultLanguage,
                                         store => store.Groups.Include(
                                             group => group.Name, group => group.Id));
                targetClientContext.ExecuteQuery();

                foreach (ChangedItem _changeItem in _cic)
                {
                    Guid g = _changeItem.Id;
                    if (_changeItem.ItemType == ChangedItemType.Group)
                    {
                        TermGroup sourceTermGroup = sourceTermStore.GetGroup(_changeItem.Id);
                        sourceClientContext.Load(sourceTermGroup, group => group.Name, group => group.Id, group => group.IsSystemGroup, group => group.Description);
                        sourceClientContext.ExecuteQuery();
                        if (sourceTermGroup.ServerObjectIsNull.Value)
                        {
                            continue;
                        }
                        else
                        {
                            if (sourceTermGroup.IsSystemGroup)
                            {
                                Console.WriteLine("Group \"" + sourceTermGroup.Name + "\" is a system group");
                                continue;
                            }
                        }

                        if (_changeItem.Operation == ChangedOperationType.DeleteObject)
                        {
                            TermGroup targetTermGroup = targetTermStore.GetGroup(_changeItem.Id);
                            targetClientContext.Load(targetTermGroup,
                                                     group => group.Name,
                                                     group => group.Id,
                                                     group => group.TermSets.Include(
                                                         termSet => termSet.Name,
                                                         termSet => termSet.Id));
                            targetClientContext.ExecuteQuery();

                            foreach (TermSet ts in targetTermGroup.TermSets)
                            {
                                Console.WriteLine("Deleting termset: " + ts.Name);
                                TermOperation op = new TermOperation();
                                op.Term      = ts.Name;
                                op.Id        = ts.Id.ToString();
                                op.Operation = "Delete";
                                op.Type      = "TermSet";
                                _list.Add(op);
                                ts.DeleteObject();
                            }

                            Console.WriteLine("Deleting group: " + sourceTermGroup.Name);
                            targetTermGroup.DeleteObject();
                            TermOperation op2 = new TermOperation();
                            op2.Term      = sourceTermGroup.Name;
                            op2.Id        = _changeItem.Id.ToString();
                            op2.Operation = "Delete";
                            op2.Type      = "TermGroup";
                            _list.Add(op2);

                            targetClientContext.ExecuteQuery();
                        }
                        else if (_changeItem.Operation == ChangedOperationType.Add)
                        {
                            TermGroup targetTermGroup = targetTermStore.GetGroup(_changeItem.Id);
                            targetClientContext.Load(targetTermGroup, group => group.Name,
                                                     group => group.Id,
                                                     group => group.TermSets.Include(
                                                         termSet => termSet.Name,
                                                         termSet => termSet.Id));

                            targetClientContext.ExecuteQuery();

                            if (targetTermGroup.ServerObjectIsNull.Value)
                            {
                                TermGroup targetTermGroupTest = targetTermStore.Groups.GetByName(sourceTermGroup.Name);
                                targetClientContext.Load(targetTermGroupTest, group => group.Name);

                                try
                                {
                                    targetClientContext.ExecuteQuery();
                                    if (!targetTermGroupTest.ServerObjectIsNull.Value)
                                    {
                                        if (sourceTermGroup.Name.ToLower() == "system" || sourceTermGroup.Name.ToLower() == "people")
                                        {
                                            Console.WriteLine("Group: " + sourceTermGroup.Name + " already exists");
                                            continue;
                                        }
                                        else
                                        {
                                            InvalidOperationException uEx = new InvalidOperationException("A group named: \"" + sourceTermGroup.Name + "\" already exists but with a different ID. Please delete the term group from the target termstore");
                                            break;
                                        }
                                    }
                                }
                                catch
                                {
                                }

                                Console.WriteLine("Adding group: " + sourceTermGroup.Name);
                                TermGroup _targetTermGroup = targetTermStore.CreateGroup(sourceTermGroup.Name, _changeItem.Id);
                                if (!string.IsNullOrEmpty(sourceTermGroup.Description))
                                {
                                    _targetTermGroup.Description = sourceTermGroup.Description;
                                }
                                targetClientContext.ExecuteQuery();
                                targetTermStore.CommitAll();
                                targetTermStore.RefreshLoad();

                                TermOperation op = new TermOperation();
                                op.Term      = sourceTermGroup.Name;
                                op.Id        = _changeItem.Id.ToString();
                                op.Operation = "Add";
                                op.Type      = "TermGroup";

                                _list.Add(op);
                            }
                        }
                        else if (_changeItem.Operation == ChangedOperationType.Edit)
                        {
                            TermGroup targetTermGroup = targetTermStore.GetGroup(_changeItem.Id);
                            targetClientContext.Load(targetTermGroup,
                                                     group => group.Name,
                                                     group => group.Id,
                                                     group => group.TermSets.Include(
                                                         termSet => termSet.Name,
                                                         termSet => termSet.Id));

                            targetClientContext.ExecuteQuery();
                            if (targetTermGroup.ServerObjectIsNull.Value)
                            {
                                targetTermGroup = targetTermStore.Groups.GetByName(sourceTermGroup.Name);

                                targetClientContext.Load(targetTermGroup, group => group.Name);
                                targetClientContext.ExecuteQuery();
                                if (targetTermGroup.ServerObjectIsNull.Value)
                                {
                                    noError = false;
                                    break;
                                }
                            }

                            if (targetTermGroup.Name != sourceTermGroup.Name)
                            {
                                Console.WriteLine("Modifying group: " + sourceTermGroup.Name);

                                targetTermGroup.Name = sourceTermGroup.Name;
                                TermOperation op = new TermOperation();
                                op.Term      = sourceTermGroup.Name;
                                op.Id        = _changeItem.Id.ToString();
                                op.Operation = "Modify";
                                op.Type      = "TermGroup";

                                _list.Add(op);

                                targetClientContext.ExecuteQuery();
                            }
                        }
                    }

                    if (_changeItem.ItemType == ChangedItemType.TermSet)
                    {
                        TermSet sourceTermset = sourceTermStore.GetTermSet(_changeItem.Id);
                        sourceClientContext.Load(sourceTermset, termset => termset.Name,
                                                 termset => termset.Id,
                                                 termset => termset.Description,
                                                 termset => termset.Contact,
                                                 termset => termset.CustomProperties,
                                                 termset => termset.Group, group => group.Id);
                        sourceClientContext.ExecuteQuery();
                        if (sourceTermset.ServerObjectIsNull.Value)
                        {
                            continue;
                        }
                        if (!termSetNames.Contains(sourceTermset.Name))
                        {
                            continue;
                        }

                        if (_changeItem.Operation == ChangedOperationType.DeleteObject)
                        {
                            TermSet targetTermset = targetTermStore.GetTermSet(_changeItem.Id);
                            targetClientContext.Load(targetTermset);
                            targetClientContext.ExecuteQuery();

                            Console.WriteLine("Deleting termset: " + targetTermset.Name);

                            targetTermset.DeleteObject();
                            targetClientContext.ExecuteQuery();

                            TermOperation op = new TermOperation();
                            op.Term      = targetTermset.Name;
                            op.Id        = _changeItem.Id.ToString();
                            op.Operation = "Delete";
                            op.Type      = "TermSet";

                            _list.Add(op);
                        }
                        else if (_changeItem.Operation == ChangedOperationType.Add)
                        {
                            TermGroup targetTermGroup = targetTermStore.GetGroup(sourceTermset.Group.Id);
                            targetClientContext.Load(targetTermGroup,
                                                     group => group.Name,
                                                     group => group.IsSystemGroup,
                                                     group => group.TermSets.Include(
                                                         termSet => termSet.Name, termSet => termSet.Id));
                            targetClientContext.ExecuteQuery();
                            if (targetTermGroup.ServerObjectIsNull.Value)
                            {
                                //Group may exist with another name
                                targetTermGroup = targetTermStore.Groups.GetByName(sourceTermset.Group.Name);
                                targetClientContext.Load(targetTermGroup,
                                                         group => group.Name,
                                                         group => group.IsSystemGroup,
                                                         group => group.TermSets.Include(
                                                             termSet => termSet.Name, termSet => termSet.Id));
                                targetClientContext.ExecuteQuery();
                                if (targetTermGroup.ServerObjectIsNull.Value)
                                {
                                    noError = false;
                                    break;
                                }
                            }

                            TermSet targetTermSetCheck = targetTermGroup.TermSets.GetByName(sourceTermset.Name);
                            targetClientContext.Load(targetTermSetCheck);

                            try
                            {
                                targetClientContext.ExecuteQuery();
                                if (!targetTermSetCheck.ServerObjectIsNull.Value)
                                {
                                    Console.WriteLine("Termset: " + sourceTermset.Name + " already exists");
                                    continue;
                                }
                            }
                            catch
                            {
                            }
                            Console.WriteLine("Adding termset: " + sourceTermset.Name);
                            targetTermGroup.CreateTermSet(sourceTermset.Name, _changeItem.Id, targetTermStore.DefaultLanguage);
                            TermOperation op = new TermOperation();
                            op.Term      = sourceTermset.Name;
                            op.Id        = _changeItem.Id.ToString();
                            op.Operation = "Add";
                            op.Type      = "TermSet";
                            targetClientContext.ExecuteQuery();
                            targetTermStore.CommitAll();
                            targetTermStore.RefreshLoad();

                            _list.Add(op);
                        }
                        else if (_changeItem.Operation == ChangedOperationType.Edit)
                        {
                            TermGroup targetTermGroup = null;
                            TermSet   sourceTermSet   = sourceTermStore.GetTermSet(_changeItem.Id);
                            sourceClientContext.Load(sourceTermSet, termset => termset.Name);
                            sourceClientContext.ExecuteQuery();

                            TermSet targetTermSet = targetTermStore.GetTermSet(_changeItem.Id);
                            targetClientContext.Load(targetTermSet, termset => termset.Name);
                            targetClientContext.ExecuteQuery();

                            if (targetTermSet.ServerObjectIsNull.Value)
                            {
                                targetTermGroup = targetTermStore.GetGroup(sourceTermset.Group.Id);
                                targetClientContext.Load(targetTermGroup, group => group.Name, group => group.IsSystemGroup);
                                targetClientContext.ExecuteQuery();
                                if (!targetTermGroup.ServerObjectIsNull.Value)
                                {
                                    targetTermSet = targetTermGroup.TermSets.GetByName(sourceTermSet.Name);
                                    targetClientContext.Load(targetTermSet, termset => termset.Name);
                                    targetClientContext.ExecuteQuery();
                                }
                            }

                            if (!targetTermSet.ServerObjectIsNull.Value)
                            {
                                if (targetTermSet.Name != sourceTermSet.Name)
                                {
                                    Console.WriteLine("Modifying termset: " + sourceTermSet.Name);
                                    targetTermSet.Name = sourceTermSet.Name;
                                    TermOperation op = new TermOperation();
                                    op.Term      = sourceTermSet.Name;
                                    op.Id        = _changeItem.Id.ToString();
                                    op.Operation = "Modify";
                                    op.Type      = "TermSet";

                                    _list.Add(op);
                                }
                            }
                            else
                            {
                                Console.WriteLine("Termset: " + sourceTermset.Name + " not found, creating it");
                                targetTermGroup.CreateTermSet(sourceTermset.Name, _changeItem.Id, targetTermStore.DefaultLanguage);
                                TermOperation op = new TermOperation();
                                op.Term      = sourceTermset.Name;
                                op.Id        = _changeItem.Id.ToString();
                                op.Operation = "Add";
                                op.Type      = "TermSet";

                                _list.Add(op);
                            }
                        }
                    }

                    if (_changeItem.ItemType == ChangedItemType.Term)
                    {
                        Term sourceTerm = sourceTermStore.GetTerm(_changeItem.Id);

                        sourceClientContext.Load(sourceTerm,
                                                 term => term.Name,
                                                 term => term.Description,
                                                 term => term.Id,
                                                 term => term.TermSet,
                                                 termset => termset.Id);

                        sourceClientContext.ExecuteQuery();
                        if (!sourceTerm.ServerObjectIsNull.Value)
                        {
                            TermSet sourceTermSet = sourceTermStore.GetTermSet(sourceTerm.TermSet.Id);
                            sourceClientContext.Load(sourceTermSet,
                                                     termset => termset.Name,
                                                     termset => termset.Id,
                                                     termset => termset.Group);
                            sourceClientContext.ExecuteQuery();


                            if (!sourceTermSet.ServerObjectIsNull.Value)
                            {
                                if (!termSetNames.Contains(sourceTermSet.Name))
                                {
                                    continue;
                                }
                            }


                            TermSet targetTermSet = targetTermStore.GetTermSet(sourceTerm.TermSet.Id);
                            targetClientContext.Load(targetTermSet, termset => termset.Name);
                            targetClientContext.ExecuteQuery();
                            if (targetTermSet.ServerObjectIsNull.Value)
                            {
                                noError = false;
                                break;
                            }


                            if (_changeItem.Operation == ChangedOperationType.DeleteObject)
                            {
                                Term targetTerm = targetTermStore.GetTerm(_changeItem.Id);
                                targetClientContext.Load(targetTerm);
                                targetClientContext.ExecuteQuery();

                                Console.WriteLine("Deleting term: " + sourceTerm.Name);

                                targetTerm.DeleteObject();
                                TermOperation op = new TermOperation();
                                op.Term      = sourceTerm.Name;
                                op.Id        = _changeItem.Id.ToString();
                                op.Operation = "Delete";
                                op.Type      = "Term";
                                _list.Add(op);
                            }

                            else if (_changeItem.Operation == ChangedOperationType.Add)
                            {
                                Term targetTerm = targetTermStore.GetTerm(sourceTerm.Id);
                                targetClientContext.Load(targetTerm);
                                targetClientContext.ExecuteQuery();

                                if (targetTerm.ServerObjectIsNull.Value)
                                {
                                    Console.WriteLine("Creating term: " + sourceTerm.Name);

                                    Term _targetTerm = targetTermSet.CreateTerm(sourceTerm.Name, targetTermStore.DefaultLanguage, _changeItem.Id);
                                    if (!string.IsNullOrEmpty(sourceTerm.Description))
                                    {
                                        _targetTerm.SetDescription(sourceTerm.Description, targetTermStore.DefaultLanguage);
                                    }
                                    //GetLabels from Source



                                    targetClientContext.ExecuteQuery();
                                    TermOperation op = new TermOperation();
                                    op.Term      = sourceTerm.Name;
                                    op.Id        = _changeItem.Id.ToString();
                                    op.Operation = "Add";
                                    op.Type      = "Term";

                                    _list.Add(op);
                                }
                            }
                            else if (_changeItem.Operation == ChangedOperationType.Edit)
                            {
                                Term targetTerm = targetTermStore.GetTerm(_changeItem.Id);
                                targetClientContext.Load(targetTerm, term => term.Name);
                                targetClientContext.ExecuteQuery();

                                if (!targetTerm.ServerObjectIsNull.Value)
                                {
                                    if (targetTerm.Name != sourceTerm.Name)
                                    {
                                        targetTerm.Name = sourceTerm.Name;
                                        TermOperation op = new TermOperation();
                                        op.Term      = sourceTerm.Name;
                                        op.Id        = _changeItem.Id.ToString();
                                        op.Operation = "Modify";
                                        op.Type      = "Term";

                                        _list.Add(op);
                                    }
                                }
                                else
                                {
                                    try
                                    {
                                        Term _targetTerm = targetTermSet.CreateTerm(sourceTerm.Name, targetTermStore.DefaultLanguage, _changeItem.Id);
                                        if (!string.IsNullOrEmpty(sourceTerm.Description))
                                        {
                                            _targetTerm.SetDescription(sourceTerm.Description, targetTermStore.DefaultLanguage);
                                        }
                                        targetClientContext.ExecuteQuery();
                                        Console.WriteLine("Term: " + sourceTerm.Name + " not found, creating it");
                                        TermOperation op = new TermOperation();
                                        op.Term      = sourceTerm.Name;
                                        op.Id        = _changeItem.Id.ToString();
                                        op.Operation = "Add";
                                        op.Type      = "Term";
                                        _list.Add(op);
                                    }
                                    catch
                                    {
                                    }
                                }
                            }
                        }
                    }
                }
                if (noError)
                {
                    targetClientContext.ExecuteQuery();
                    targetTermStore.CommitAll();
                }
            }
        }