Skip to content

Commit cb8e35b

Browse files
authored
Merge pull request #1244 from 6gal6ler6/svillaltav3
`DynamicForm` Add support for webAbsoluteUrl
2 parents a157cec + fbc285b commit cb8e35b

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

src/controls/dynamicForm/DynamicForm.tsx

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,36 @@ const stackTokens: IStackTokens = { childrenGap: 20 };
2121
* DynamicForm Class Control
2222
*/
2323
export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicFormState> {
24+
2425
private _spService: SPservice;
26+
private webURL = this.props.webAbsoluteUrl ? this.props.webAbsoluteUrl : this.props.context.pageContext.web.absoluteUrl;
27+
2528
constructor(props: IDynamicFormProps) {
2629
super(props);
2730
// Initialize pnp sp
28-
sp.setup({
29-
spfxContext: this.props.context
30-
});
31+
32+
if (this.props.webAbsoluteUrl) {
33+
sp.setup({
34+
sp: {
35+
headers: {
36+
Accept: "application/json;odata=verbose",
37+
},
38+
baseUrl: this.props.webAbsoluteUrl
39+
},
40+
});
41+
} else {
42+
sp.setup({
43+
spfxContext: this.props.context
44+
});
45+
}
46+
3147
// Initialize state
3248
this.state = {
3349
fieldCollection: []
3450
};
3551
// Get SPService Factory
36-
this._spService = new SPservice(this.props.context);
52+
this._spService = this.props.webAbsoluteUrl ? new SPservice(this.props.context, this.props.webAbsoluteUrl) : new SPservice(this.props.context);
53+
3754
}
3855

3956
/**
@@ -58,10 +75,15 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
5875
{fieldCollection.map((v, i) => {
5976
return <DynamicField {...v} disabled={v.disabled || isSaving} />;
6077
})}
61-
<Stack className={styles.buttons} horizontal tokens={stackTokens}>
62-
<PrimaryButton disabled={isSaving} text={strings.Save} onClick={() => this.onSubmitClick()} />
63-
<DefaultButton disabled={isSaving} text={strings.Cancel} onClick={this.props.onCancelled} />
64-
</Stack>
78+
{
79+
!this.props.disabled &&
80+
<Stack className={styles.buttons} horizontal tokens={stackTokens}>
81+
<PrimaryButton disabled={isSaving} text={strings.Save} onClick={() => this.onSubmitClick()} />
82+
<DefaultButton disabled={isSaving} text={strings.Cancel} onClick={this.props.onCancelled} />
83+
</Stack>
84+
}
85+
86+
6587
</div>
6688
}
6789
</div>
@@ -303,7 +325,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
303325
let defaultContentType = await spList.contentTypes.select("Id", "Name").get();
304326
contentTypeId = defaultContentType[0]["Id"].StringValue;
305327
}
306-
const listFeilds = await this.getFormFields(listId, contentTypeId, context.pageContext.web.absoluteUrl);
328+
const listFeilds = await this.getFormFields(listId, contentTypeId, this.webURL);
307329
const tempFields: IDynamicFieldProps[] = [];
308330
let order: number = 0;
309331
const responseValue = listFeilds['value'];
@@ -340,7 +362,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
340362
lookupListId = field["LookupList"];
341363
lookupField = field["LookupField"];
342364
if (item !== null) {
343-
defaultValue = await this._spService.getLookupValue(listId, listItemId, field.EntityPropertyName, lookupField, context.pageContext.web.absoluteUrl);
365+
defaultValue = await this._spService.getLookupValue(listId, listItemId, field.EntityPropertyName, lookupField, this.webURL);
344366
}
345367
else {
346368
defaultValue = [];
@@ -351,14 +373,14 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
351373
lookupListId = field["LookupList"];
352374
lookupField = field["LookupField"];
353375
if (item !== null) {
354-
defaultValue = await this._spService.getLookupValues(listId, listItemId, field.EntityPropertyName, lookupField, context.pageContext.web.absoluteUrl);
376+
defaultValue = await this._spService.getLookupValues(listId, listItemId, field.EntityPropertyName, lookupField, this.webURL);
355377
}
356378
else {
357379
defaultValue = [];
358380
}
359381
}
360382
else if (fieldType === "TaxonomyFieldTypeMulti") {
361-
let response = await this._spService.getTaxonomyFieldInternalName(this.props.listId, field.InternalName, this.props.context.pageContext.web.absoluteUrl);
383+
let response = await this._spService.getTaxonomyFieldInternalName(this.props.listId, field.InternalName, this.webURL);
362384
hiddenName = response["value"];
363385
termSetId = field["TermSetId"];
364386
if (item !== null) {
@@ -414,7 +436,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
414436
}
415437
else if (fieldType === "UserMulti") {
416438
if (item !== null)
417-
defaultValue = await this._spService.getUsersUPNFromFieldValue(listId, listItemId, field.InternalName, context.pageContext.web.absoluteUrl);
439+
defaultValue = await this._spService.getUsersUPNFromFieldValue(listId, listItemId, field.InternalName, this.webURL);
418440
else {
419441
defaultValue = [];
420442
}
@@ -423,7 +445,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
423445
}
424446
else if (fieldType === "Thumbnail") {
425447
if (defaultValue !== null) {
426-
defaultValue = context.pageContext.web.absoluteUrl.split('/sites/')[0] + JSON.parse(defaultValue).serverRelativeUrl;
448+
defaultValue = this.webURL.split('/sites/')[0] + JSON.parse(defaultValue).serverRelativeUrl;
427449
}
428450
}
429451
else if (fieldType === "User") {
@@ -517,7 +539,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
517539
const {
518540
context
519541
} = this.props;
520-
const webAbsoluteUrl = !webUrl ? context.pageContext.web.absoluteUrl : webUrl;
542+
const webAbsoluteUrl = !webUrl ? this.webURL : webUrl;
521543
let apiUrl = '';
522544
if (contentTypeId !== undefined && contentTypeId !== '') {
523545
apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/contenttypes('${contentTypeId}')/fields?@listId=guid'${encodeURIComponent(listId)}'&$filter=ReadOnlyField eq false and Hidden eq false and (FromBaseType eq false or StaticName eq 'Title')`;

src/controls/dynamicForm/IDynamicFormProps.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,10 @@ export interface IDynamicFormProps {
5252
/**
5353
* InternalName of fields that should be disabled
5454
*/
55-
disabledFields?: string[];
55+
disabledFields?: string[];
56+
57+
/**
58+
* Absolute Web Url of target site (user requires permissions)
59+
* */
60+
webAbsoluteUrl?: string;
5661
}

0 commit comments

Comments
 (0)