%3CLINGO-SUB%20id%3D%22lingo-sub-510094%22%20slang%3D%22en-US%22%3EProblem%20of%20using%20TermStores.getByName()%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-510094%22%20slang%3D%22en-US%22%3E%3CP%3E%3CSTRONG%3EFirst%20published%20on%20TECHNET%20on%20Sep%2027%2C%202018%20%3C%2FSTRONG%3E%20%3CBR%20%2F%3EThis%20post%20is%20a%20contribution%20from%20Aaron%20Miao%2C%20an%20engineer%20with%20the%20SharePoint%20Developer%20Support%20team%20%3CBR%20%2F%3E%3CBR%20%2F%3EWe%20recently%20still%20see%20customers%20open%20support%20cases%20because%20code%20for%20fetching%20term%20store%20stopped%20working%2C%20especially%20from%20SharePoint%20Online%20customers.%20%3CBR%20%2F%3E%3CBR%20%2F%3EFetching%20term%20store%20by%20name%20is%20bad%20idea.%20The%20term%20store%20name%20is%20not%20actually%20persisted%20anywhere.%20It%E2%80%99s%20a%20calculated%20value%20and%20it%20changes.%20From%20the%20code%20like%20below%2C%20you%20probably%20can%20see%20the%20name%20of%20the%20term%20store%20passed%20to%20getByName()%20looks%20like%20a%20random%20value.%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CDIV%3E%3CBR%20%2F%3Evar%20termStore%20%3D%20termStores.getByName(%22Taxonomy_Dmxzz8tIBzk8wNVKQpJ%2BxA%3D%3D%22)%3B%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FDIV%3E%0A%3CP%3E%3CBR%20%2F%3EThe%20term%20store%20name%20could%20change%20due%20to%20various%20reasons%2C%20such%20as%20failover%2C%20tenant%20move%2C%20DB%20move%2C%20etc.%20events%20happened%20on%20SharePoint%20server%20side.%20%3CBR%20%2F%3E%3CBR%20%2F%3EHowever%2C%20this%20is%20not%20your%20fault.%20There%E2%80%99re%20no%20published%20official%20documents%20on%20this.%20Even%20worse%2C%20SharePoint%20client-side%20APIs%20provide%20the%20getByName().%20So%2C%20of%20course%2C%20customers%20start%20fetching%20their%20term%20store%20using%20that%20name%20in%20their%20custom%20code.%20Stop%20using%20getByName()%20method!%20%3CBR%20%2F%3E%3CBR%20%2F%3EThe%20correct%20way%20to%20fetch%20term%20store%2C%20like%20many%20good%20samples%20out%20there%2C%20is%20to%20use%20the%20following%20(CSOM)%20APIs%20are%3A%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CDIV%3E%3CBR%20%2F%3ETaxonomySession.GetDefaultSiteCollectionTermStore%20%3CBR%20%2F%3ETaxonomySession.GetDefaultKeywordsTermStore%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FDIV%3E%0A%3CP%3E%3CBR%20%2F%3EBoth%20methods%20above%20return%20%3CA%20href%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fmicrosoft.sharepoint.client.taxonomy.termstore.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20TermStore%20%3C%2FA%3E%20object.%20The%20DefaultSiteCollectionTermStore%20is%20a%20term%20store%20that%20has%20metadata%20local%20to%20the%20site%20collection%2C%20while%20the%20DefaultKeywordsTermStore%20is%20a%20global%20term%20store%20that%20contains%20enterprise%20keywords%2C%20as%20shown%20in%20%3CA%20href%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Flibrary%2Fmicrosoft.sharepoint.taxonomy.taxonomysession_members.aspx%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20TaxonomySession%20%3C%2FA%3E%20class%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20957px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F111657iA5C28A1CACC9F15D%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20%2F%3E%3C%2FSPAN%3E%20%3CBR%20%2F%3E%3CBR%20%2F%3EPlease%20be%20aware%20that%20both%20methods%20may%20return%20null%20value%2C%20as%20remarked%20in%20each%20API%20document%2C%20depends%20on%20your%20term%20store%20settings.%20%3CBR%20%2F%3E%3CBR%20%2F%3EHere%E2%80%99re%20the%20JSOM%20APIs%3A%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%0A%3CDIV%3E%3CBR%20%2F%3EgetDefaultSiteCollectionTermStore%20%3CBR%20%2F%3EgetDefaultKeywordsTermStore%20%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FDIV%3E%0A%3CP%3E%3CBR%20%2F%3E%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2FSharePoint%2Fgovernance%2Fmanaged-metadata-planning%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3E%20This%20document%20%3C%2FA%3E%20discusses%20difference%20between%20global%20and%20local%20(site%20collection%20specific)%20term%20stores.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-510094%22%20slang%3D%22en-US%22%3E%3CP%3EFirst%20published%20on%20TECHNET%20on%20Sep%2027%2C%202018%20This%20post%20is%20a%20contribution%20from%20Aaron%20Miao%2C%20an%20engineer%20with%20the%20SharePoint%20Developer%20Support%20teamWe%20recently%20still%20see%20customers%20open%20support%20cases%20because%20code%20for%20fetching%20term%20store%20stopped%20working%2C%20especially%20from%20SharePoint%20Online%20customers.%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-510094%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

First published on TECHNET on Sep 27, 2018
This post is a contribution from Aaron Miao, an engineer with the SharePoint Developer Support team

We recently still see customers open support cases because code for fetching term store stopped working, especially from SharePoint Online customers.

Fetching term store by name is bad idea. The term store name is not actually persisted anywhere. It’s a calculated value and it changes. From the code like below, you probably can see the name of the term store passed to getByName() looks like a random value.



var termStore = termStores.getByName("Taxonomy_Dmxzz8tIBzk8wNVKQpJ+xA==");


The term store name could change due to various reasons, such as failover, tenant move, DB move, etc. events happened on SharePoint server side.

However, this is not your fault. There’re no published official documents on this. Even worse, SharePoint client-side APIs provide the getByName(). So, of course, customers start fetching their term store using that name in their custom code. Stop using getByName() method!

The correct way to fetch term store, like many good samples out there, is to use the following (CSOM) APIs are:



TaxonomySession.GetDefaultSiteCollectionTermStore
TaxonomySession.GetDefaultKeywordsTermStore


Both methods above return TermStore object. The DefaultSiteCollectionTermStore is a term store that has metadata local to the site collection, while the DefaultKeywordsTermStore is a global term store that contains enterprise keywords, as shown in TaxonomySession class



Please be aware that both methods may return null value, as remarked in each API document, depends on your term store settings.

Here’re the JSOM APIs:



getDefaultSiteCollectionTermStore
getDefaultKeywordsTermStore


This document discusses difference between global and local (site collection specific) term stores.