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);
            }
        }
예제 #2
0
        protected override void ExecuteCmdlet()
        {
            var taxonomySession = TaxonomySession.GetTaxonomySession(ClientContext);
            // Get Term Store
            TermStore termStore = null;

            if (TermStore == null)
            {
                termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
            }
            else
            {
                termStore = TermStore.GetTermStore(taxonomySession);
            }
            termStore.EnsureProperty(ts => ts.DefaultLanguage);

            TermGroup termGroup = TermGroup.GetGroup(termStore);

            TermSet termSet = TermSet.GetTermSet(termGroup);

            if (Id == Guid.Empty)
            {
                Id = Guid.NewGuid();
            }
            var termName = TaxonomyExtensions.NormalizeName(Name);

            if (!ParameterSpecified(nameof(Lcid)))
            {
                Lcid = termStore.EnsureProperty(ts => ts.DefaultLanguage);
            }

            var term = termSet.CreateTerm(termName, Lcid, Id);

            ClientContext.Load(term);
            ClientContext.ExecuteQueryRetry();
            term.SetDescription(Description, Lcid);

            var customProperties = CustomProperties ?? new Hashtable();

            foreach (var key in customProperties.Keys)
            {
                term.SetCustomProperty(key as string, customProperties[key] as string);
            }

            var localCustomProperties = LocalCustomProperties ?? new Hashtable();

            foreach (var key in localCustomProperties.Keys)
            {
                term.SetLocalCustomProperty(key as string, localCustomProperties[key] as string);
            }
            termStore.CommitAll();
            ClientContext.Load(term);
            ClientContext.ExecuteQueryRetry();
            WriteObject(term);
        }
예제 #3
0
 public override void Delete(DivisionInfo entity)
 {
     if (SecurityContext.CanDelete(entity))
     {
         if (entity.DivisionTermID != null)
         {
             TaxonomyExtensions.DeleteTerm(entity.DivisionTermID.Value);
         }
         ModelContext.Remove(entity);
     }
 }
예제 #4
0
        public Term GetTerm(ClientContext clientContext, TermStore termStore, TermSet termSet, bool recursive, Expression <Func <Term, object> >[] expressions = null)
        {
            Term term = null;

            if (_id != Guid.Empty)
            {
                term = termStore.GetTerm(_id);
            }
            else if (!string.IsNullOrEmpty(_title) && termSet != null && termStore != null)
            {
                var termName = TaxonomyExtensions.NormalizeName(_title);
                if (!recursive)
                {
                    term = termSet.Terms.GetByName(termName);
                }
                else
                {
                    var lmi = new LabelMatchInformation(clientContext)
                    {
                        TrimUnavailable = true,
                        TermLabel       = termName
                    };

                    var termMatches = termSet.GetTerms(lmi);
                    clientContext.Load(termMatches);
                    clientContext.ExecuteQueryRetry();

                    if (termMatches.AreItemsAvailable)
                    {
                        term = termMatches.FirstOrDefault();
                    }
                }
            }
            else
            {
                throw new PSArgumentException("Not enough parameters specified to succesfully find the term");
            }
            if (expressions != null)
            {
                clientContext.Load(term, expressions);
            }
            else
            {
                clientContext.Load(term);
            }
            clientContext.ExecuteQueryRetry();
            return(term);
        }
예제 #5
0
        protected override void ExecuteCmdlet()
        {
            var taxonomySession = TaxonomySession.GetTaxonomySession(ClientContext);
            // Get Term Store
            TermStore termStore = null;

            if (TermStore == null)
            {
                termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
            }
            else
            {
                termStore = TermStore.GetTermStore(taxonomySession);
            }
            termStore.EnsureProperty(ts => ts.DefaultLanguage);

            var termGroup = TermGroup.GetGroup(termStore);
            var termSet   = TermSet.GetTermSet(termGroup);
            var term      = Identity.GetTerm(ClientContext, termStore, termSet, false, null);

            if (ParameterSpecified(nameof(Name)))
            {
                term.Name = TaxonomyExtensions.NormalizeName(Name);
            }
            if (ParameterSpecified(nameof(Description)))
            {
                if (!ParameterSpecified(nameof(Lcid)))
                {
                    Lcid = termStore.EnsureProperty(ts => ts.DefaultLanguage);
                }
                term.SetDescription(Description, Lcid);
            }
            if (ParameterSpecified(nameof(DeleteAllCustomProperties)))
            {
                term.DeleteAllCustomProperties();
            }
            if (ParameterSpecified(nameof(DeleteAllLocalCustomProperties)))
            {
                term.DeleteAllLocalCustomProperties();
            }
            if (ParameterSpecified(nameof(CustomProperties)))
            {
                var customProperties = CustomProperties ?? new Hashtable();
                foreach (var key in customProperties.Keys)
                {
                    term.SetCustomProperty(key as string, customProperties[key] as string);
                }
            }
            if (ParameterSpecified(nameof(LocalCustomProperties)))
            {
                var localCustomProperties = LocalCustomProperties ?? new Hashtable();
                foreach (var key in localCustomProperties.Keys)
                {
                    term.SetCustomProperty(key as string, localCustomProperties[key] as string);
                }
            }
            if (ParameterSpecified(nameof(Deprecated)))
            {
                term.Deprecate(Deprecated);
            }
            ClientContext.Load(term);
            termStore.CommitAll();
            ClientContext.ExecuteQueryRetry();
            WriteObject(term);
        }
예제 #6
0
        protected override void ExecuteCmdlet()
        {
            var taxonomySession = TaxonomySession.GetTaxonomySession(ClientContext);
            // Get Term Store
            var termStore = default(TermStore);

            if (TermStore != null)
            {
                if (TermStore.IdValue != Guid.Empty)
                {
                    termStore = taxonomySession.TermStores.GetById(TermStore.IdValue);
                }
                else if (!string.IsNullOrEmpty(TermStore.StringValue))
                {
                    termStore = taxonomySession.TermStores.GetByName(TermStore.StringValue);
                }
                else
                {
                    termStore = TermStore.Item;
                }
            }
            else
            {
                termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
            }

            TermGroup termGroup = null;

            if (TermGroup.Id != Guid.Empty)
            {
                termGroup = termStore.Groups.GetById(TermGroup.Id);
            }
            else if (!string.IsNullOrEmpty(TermGroup.Name))
            {
                termGroup = termStore.Groups.GetByName(TermGroup.Name);
            }

            TermSet termSet;

            if (TermSet.Id != Guid.Empty)
            {
                termSet = termGroup.TermSets.GetById(TermSet.Id);
            }
            else if (!string.IsNullOrEmpty(TermSet.Title))
            {
                termSet = termGroup.TermSets.GetByName(TermSet.Title);
            }
            else
            {
                termSet = TermSet.Item;
            }

            if (Id == Guid.Empty)
            {
                Id = Guid.NewGuid();
            }
            var termName = TaxonomyExtensions.NormalizeName(Name);
            var term     = termSet.CreateTerm(termName, Lcid, Id);

            ClientContext.Load(term);
            ClientContext.ExecuteQueryRetry();
            term.SetDescription(Description, Lcid);

            var customProperties = CustomProperties ?? new Hashtable();

            foreach (var key in customProperties.Keys)
            {
                term.SetCustomProperty(key as string, customProperties[key] as string);
            }

            var localCustomProperties = LocalCustomProperties ?? new Hashtable();

            foreach (var key in localCustomProperties.Keys)
            {
                term.SetLocalCustomProperty(key as string, localCustomProperties[key] as string);
            }
            termStore.CommitAll();
            ClientContext.Load(term);
            ClientContext.ExecuteQueryRetry();
            WriteObject(term);
        }
예제 #7
0
        protected override void ExecuteCmdlet()
        {
            DefaultRetrievalExpressions = new Expression <Func <Term, object> >[] { g => g.Name, g => g.TermsCount, g => g.Id };
            var taxonomySession = TaxonomySession.GetTaxonomySession(ClientContext);
            // Get Term Store
            TermStore termStore = null;

            if (TermStore == null)
            {
                termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
            }
            else
            {
                if (TermStore.StringValue != null)
                {
                    termStore = taxonomySession.TermStores.GetByName(TermStore.StringValue);
                }
                else if (TermStore.IdValue != Guid.Empty)
                {
                    termStore = taxonomySession.TermStores.GetById(TermStore.IdValue);
                }
                else
                {
                    if (TermStore.Item != null)
                    {
                        termStore = TermStore.Item;
                    }
                }
            }

            if (ParameterSetName == ParameterSet_TERM)
            {
                if (Identity.IdValue != Guid.Empty)
                {
                    term = termStore.GetTerm(Identity.IdValue);
                    ClientContext.Load(term, RetrievalExpressions);
                    ClientContext.ExecuteQueryRetry();
                    if (IncludeChildTerms.IsPresent && term.TermsCount > 0)
                    {
                        LoadChildTerms(term);
                    }
                    WriteObject(term);
                }
                else
                {
                    WriteError(new ErrorRecord(new Exception("Insufficient parameters"), "INSUFFICIENTPARAMETERS", ErrorCategory.SyntaxError, this));
                }
            }
            else
            {
                TermGroup termGroup = null;

                if (TermGroup != null && TermGroup.Id != Guid.Empty)
                {
                    termGroup = termStore.Groups.GetById(TermGroup.Id);
                }
                else if (TermGroup != null && !string.IsNullOrEmpty(TermGroup.Name))
                {
                    termGroup = termStore.Groups.GetByName(TermGroup.Name);
                }

                TermSet termSet = null;
                if (TermSet != null)
                {
                    if (TermSet.Id != Guid.Empty)
                    {
                        termSet = termGroup.TermSets.GetById(TermSet.Id);
                    }
                    else if (!string.IsNullOrEmpty(TermSet.Title))
                    {
                        termSet = termGroup.TermSets.GetByName(TermSet.Title);
                    }
                    else
                    {
                        termSet = TermSet.Item;
                    }
                }
                if (Identity != null)
                {
                    term = null;
                    if (Identity.IdValue != Guid.Empty)
                    {
                        term = termStore.GetTerm(Identity.IdValue);
                    }
                    else
                    {
                        var termName = TaxonomyExtensions.NormalizeName(Identity.StringValue);
                        if (!Recursive)
                        {
                            term = termSet.Terms.GetByName(termName);
                        }
                        else
                        {
                            var lmi = new LabelMatchInformation(ClientContext)
                            {
                                TrimUnavailable = true,
                                TermLabel       = termName
                            };

                            var termMatches = termSet.GetTerms(lmi);
                            ClientContext.Load(termMatches);
                            ClientContext.ExecuteQueryRetry();

                            if (termMatches.AreItemsAvailable)
                            {
                                term = termMatches.FirstOrDefault();
                            }
                        }
                    }
                    ClientContext.Load(term, RetrievalExpressions);
                    ClientContext.ExecuteQueryRetry();
                    if (IncludeChildTerms.IsPresent && term.TermsCount > 0)
                    {
                        LoadChildTerms(term);
                    }
                    WriteObject(term);
                }
                else
                {
                    var query = termSet.Terms.IncludeWithDefaultProperties(RetrievalExpressions);
                    var terms = ClientContext.LoadQuery(query);
                    ClientContext.ExecuteQueryRetry();
                    if (IncludeChildTerms.IsPresent)
                    {
                        foreach (var collectionTerm in terms)
                        {
                            if (collectionTerm.TermsCount > 0)
                            {
                                LoadChildTerms(collectionTerm);
                            }
                        }
                    }
                    WriteObject(terms, true);
                }
            }
        }
예제 #8
0
        protected override void ExecuteCmdlet()
        {
            DefaultRetrievalExpressions = new Expression <Func <Term, object> >[] { g => g.Name, g => g.Id };
            var taxonomySession = TaxonomySession.GetTaxonomySession(ClientContext);
            // Get Term Store
            TermStore termStore = null;

            if (TermStore == null)
            {
                termStore = taxonomySession.GetDefaultSiteCollectionTermStore();
            }
            else
            {
                if (TermStore.StringValue != null)
                {
                    termStore = taxonomySession.TermStores.GetByName(TermStore.StringValue);
                }
                else if (TermStore.IdValue != Guid.Empty)
                {
                    termStore = taxonomySession.TermStores.GetById(TermStore.IdValue);
                }
                else
                {
                    if (TermStore.Item != null)
                    {
                        termStore = TermStore.Item;
                    }
                }
            }

            TermGroup termGroup = null;

            if (TermGroup.Id != Guid.Empty)
            {
                termGroup = termStore.Groups.GetById(TermGroup.Id);
            }
            else if (!string.IsNullOrEmpty(TermGroup.Name))
            {
                termGroup = termStore.Groups.GetByName(TermGroup.Name);
            }

            TermSet termSet;

            if (TermSet.Id != Guid.Empty)
            {
                termSet = termGroup.TermSets.GetById(TermSet.Id);
            }
            else if (!string.IsNullOrEmpty(TermSet.Title))
            {
                termSet = termGroup.TermSets.GetByName(TermSet.Title);
            }
            else
            {
                termSet = TermSet.Item;
            }
            if (Identity != null)
            {
                Term term;
                if (Identity.IdValue != Guid.Empty)
                {
                    term = termSet.Terms.GetById(Identity.IdValue);
                }
                else
                {
                    var termName = TaxonomyExtensions.NormalizeName(Identity.StringValue);
                    term = termSet.Terms.GetByName(termName);
                }
                ClientContext.Load(term, RetrievalExpressions);
                ClientContext.ExecuteQueryRetry();
                WriteObject(term);
            }
            else
            {
                var query = termSet.Terms.IncludeWithDefaultProperties(RetrievalExpressions);
                var terms = ClientContext.LoadQuery(query);
                ClientContext.ExecuteQueryRetry();
                WriteObject(terms, true);
            }
        }
 public void GetSafeTermNameTest()
 {
     Assert.Equal("FNS", TaxonomyExtensions.GetSafeTermName("FNS", "Faculty of Natural Sciencies"));
     Assert.Equal("Natural Sciencies Faculty", TaxonomyExtensions.GetSafeTermName(string.Empty, "\"Natural Sciencies\" Faculty"));
 }