1

In Edit mode, I can add categories to an Enterprise Wiki page by just clicking the button and entering some text. The keywords are added to the page but also to a site pool so they can be selected and reused on other pages.

Since I am migrating a Wiki with existing tags to Sharepoint, I would like to ask: how can I do the same via code, using the Client Object Model?

Basically, I want to do what they describe here: https://www.metalogix.com/help/Content%20Matrix%20Console/Blogs%20and%20Wikis%20Edition/002_HowTo/002_MigrationActions/016_MigratePageTagsAsCategories.htm

0

First retrieve the (empty) term set for the wiki category terms. This can be cached, since no-one else will be adding terms while the migration is ongoing.

        private TermSet wikiCategoryTermSet;
        const int localeId = 1033;

        private void GetWikiCategoryTermSet(ClientRuntimeContext ctx)
        {
            // Belongs to the site collection. we can cache this...
            TaxonomySession session = TaxonomySession.GetTaxonomySession(ctx);
            TermStore store = session.GetDefaultSiteCollectionTermStore();
            TermGroup group = store.Groups.GetByName("Site Collection - COMPANYNAME.sharepoint.com-sites-WIKISITENAME");

            wikiCategoryTermSet = group.TermSets.GetByName("Wiki Categories");
            ctx.Load(wikiCategoryTermSet);
            ctx.Load(wikiCategoryTermSet.Terms);
            ctx.ExecuteQuery();

        }

This code creates the terms and adds them to the common term set:

        private void CreateWikiCategoryTerms(ClientRuntimeContext ctx, string[] termNames) 
        {                      
            TermCollection terms = wikiCategoryTermSet.GetAllTerms();
            ctx.Load(terms); //ctx.Load(terms, a => a.Where(t => t.Name == termName));
            ctx.ExecuteQuery();


            foreach (var termName in termNames)
            {               
                Term term = terms.FirstOrDefault(t => t.Name == termName);
                if (term == null)
                {
                    // create                  
                    term = wikiCategoryTermSet.CreateTerm(termName, localeId, Guid.NewGuid());

                }
            }

            // commit term store changes
            wikiCategoryTermSet.TermStore.CommitAll();

            GetWikiCategoryTermSet(ctx);

        }

This method assigns the wiki categories to a page:

        public void AssignPageCategories(ClientRuntimeContext context, ListItem pageItem, string[] tagNames)
        {
            Field categoryField;
            var list = pageItem.ParentList;
            FieldCollection fields = list.Fields;

            List<Term> terms = new List<Term>();
            // get the terms corresponding to the page tags:
            foreach (var tagName in tagNames)
            {

                Term term = wikiCategoryTermSet.Terms.FirstOrDefault(t => t.Name == tagName);
                if (term == null)
                {
                    throw new Exception("Term not found.");
                }

                terms.Add(term);
            }

            // get the field that contains the category terms for this page:
            context.Load(fields, f => f.Where(fd => fd.InternalName == "Wiki_x0020_Page_x0020_Categories"));
            context.ExecuteQuery();

            categoryField = fields.FirstOrDefault();


            //make sure the TypeAsString property is loaded
            if (categoryField.IsObjectPropertyInstantiated("TypeAsString") == false)
            {
                categoryField.Context.Load(categoryField, f => f.TypeAsString);
                categoryField.Context.ExecuteQuery();
            }
            //if a multi-select managed metadata field
            if (categoryField.TypeAsString == "TaxonomyFieldTypeMulti")
            {
                //Cast Field to TaxonomyField
                TaxonomyField taxField = categoryField.Context.CastTo<TaxonomyField>(categoryField);


                string value = ""; 
                string delim = "";
                foreach (var term in terms)
                {
                    value += delim;       
                    value += $"-1;#{term.Name}|{term.Id}"; 

                    delim = ";#";
                }


                TaxonomyFieldValueCollection taxonomyFieldValueCollection = new TaxonomyFieldValueCollection(categoryField.Context, value, categoryField);

                taxField.SetFieldValueByValueCollection(pageItem, taxonomyFieldValueCollection);
            }

            pageItem.Update();
            context.ExecuteQuery();
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.