SharePointVault ~/gotchas/rest-api ⌕ Search 404 pages, error messages, scripts…Ctrl K ★ Star
M↓ README.mdM↓ fieldvaluesastext-reencodes-underscores.md
sharepoint-vault›gotchas›rest-api›fieldvaluesastext-reencodes-underscores.mdhistoryedit on GitHub
● GOTCHArest-api· 1 min read

FieldValuesAsText re-encodes underscores in JSON keys

applies to: SharePoint Onlinereviewed: 2026-08-24#rest-api#lists#fields#spfx

In ListItemAllFields/FieldValuesAsText responses, every underscore of the field's internal name comes back as _x005f_ — A_x0020_B becomes A_x005f_x0020_x005f_B. Match keys by decoding _x005f_ → _, or you will "lose" values for any column whose name contains a space or diacritic.

V odpovědi ListItemAllFields/FieldValuesAsText se každé podtržítko interního názvu pole vrací jako _x005f_ – z A_x0020_B je A_x005f_x0020_x005f_B. Klíče porovnávej po dekódování _x005f_ → _, jinak „ztratíš“ hodnoty všech sloupců, jejichž název vznikl z textu s mezerou nebo diakritikou.

Symptom

You fetch formatted values for an item:

GET /_api/web/getfilebyserverrelativeurl('/sites/x/Docs/report.docx')/ListItemAllFields/FieldValuesAsText

and look up your column by its internal name (Document_x0020_Type). The key is missing from the JSON — even though the column clearly has a value in the UI. Columns with simple names (Category) work fine.

Cause

Internal names created from display names with spaces already contain OData-encoded sequences (_x0020_ for a space). When FieldValuesAsText serialises the response, it encodes the underscores of that encoding once more: each _ becomes _x005f_. Your Document_x0020_Type arrives as Document_x005f_x0020_x005f_Type.

Fix

Normalise the response keys before matching:

const dict: { [k: string]: string } = {};
Object.keys(resp).forEach(k => {
  const v = resp[k];
  if (typeof v !== 'string') return;
  dict[k.toLowerCase()] = v;                                   // raw key
  dict[k.split('_x005f_').join('_').toLowerCase()] = v;        // decoded key
});
const value = dict[internalName.toLowerCase()] || '';

Keeping both the raw and the decoded key makes the lookup safe regardless of whether a given tenant/endpoint applies the re-encoding.

Notes

  • FieldValuesAsText is otherwise a great fit for read-only detail views: it returns display-ready strings for every field type, including person, lookup and managed metadata — types you cannot easily render from raw item values.
  • The sibling endpoints FieldValuesAsHtml and FieldValuesForEdit show the same key encoding.
  • Raw item reads (/items?$select=Document_x0020_Type) are not affected — the re-encoding is specific to the FieldValues* family.
Was this helpful?ask or add your experience on GitHub →
Saved you an hour? A star on GitHub helps other SharePoint people find the vault.★ Star on GitHub
last reviewed: 2026-08-24last updated: 2026-09-24historyedit on GitHubview source