Skip to content

Commit

Permalink
Merge branch 'tavikukko-get-folder-default-values' into version-2
Browse files Browse the repository at this point in the history
  • Loading branch information
juliemturner committed Nov 13, 2020
2 parents fe13bfb + 8297d27 commit 518c87d
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions packages/sp/column-defaults/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ declare module "../lists/types" {

/**
* Replaces all the column defaults with the supplied values
*
* @param defaults
*
* @param defaults
*/
setDefaultColumnValues(defaults: IFieldDefault[]): Promise<void>;
}
Expand Down Expand Up @@ -64,13 +64,11 @@ _List.prototype.getDefaultColumnValues = async function (this: _List): Promise<I
// now we need to turn these tags of form into objects
// <a href="/sites/dev/My%20Title"><DefaultValue FieldName="TextField">Test</DefaultValue></a>

return tags.map(t => {
const m = /<a href="(.*?)"><DefaultValue FieldName="(.*?)">(.*?)<\/DefaultValue>/ig.exec(t);
// if things worked our captures are:
return tags.reduce((defVals, t) => {
const m = /<a href="(.*?)">/ig.exec(t);
// if things worked out captures are:
// 0: whole string
// 1: ENCODED server relative path
// 2: Field internal name
// 3: Default value as string

if (m.length < 1) {
// this indicates an error somewhere, but we have no way to meaningfully recover
Expand All @@ -80,13 +78,30 @@ _List.prototype.getDefaultColumnValues = async function (this: _List): Promise<I
}

// return the parsed out values
return {
name: m[2],
path: decodeURIComponent(m[1]),
value: m[3],
};

}).filter(v => v !== null);
const subMatches = t.match(/<DefaultValue.*?<\/DefaultValue>/ig);
const subTags = subMatches === null ? [] : subMatches.map(st => st.trim());

subTags.map(st => {
const sm = /<DefaultValue FieldName="(.*?)">(.*?)<\/DefaultValue>/ig.exec(st);
// if things worked out captures are:
// 0: whole string
// 1: Field internal name
// 2: Default value as string

if (sm.length < 1) {
Logger.write(`Could not parse default column value from '${st}'`, LogLevel.Warning);
} else {
defVals.push({
name: sm[1],
path: decodeURIComponent(m[1]),
value: sm[2],
});
}
});

return defVals;

}, []).filter(v => v !== null);
};

_List.prototype.setDefaultColumnValues = async function (this: _List, defaults: IFieldDefault[]): Promise<void> {
Expand Down

0 comments on commit 518c87d

Please sign in to comment.