Skip to content

Commit f4ac23d

Browse files
SergioVillalta-JimenezSergioVillalta-Jimenez
authored andcommitted
Add support for webAbsoluteUrl
1 parent 49998e6 commit f4ac23d

File tree

3 files changed

+1449
-1401
lines changed

3 files changed

+1449
-1401
lines changed

src/controls/dynamicForm/DynamicForm.tsx

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,38 @@ 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+
34+
sp.setup({
35+
sp: {
36+
headers: {
37+
Accept: "application/json;odata=verbose",
38+
},
39+
baseUrl: this.props.webAbsoluteUrl
40+
},
41+
});
42+
} else {
43+
sp.setup({
44+
spfxContext: this.props.context
45+
});
46+
47+
}
48+
3149
// Initialize state
3250
this.state = {
3351
fieldCollection: []
3452
};
3553
// Get SPService Factory
36-
this._spService = new SPservice(this.props.context);
54+
this._spService = this.props.webAbsoluteUrl ? new SPservice(this.props.context, this.props.webAbsoluteUrl) : new SPservice(this.props.context);
55+
3756
}
3857

3958
/**
@@ -58,10 +77,15 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
5877
{fieldCollection.map((v, i) => {
5978
return <DynamicField {...v} disabled={v.disabled || isSaving} />;
6079
})}
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>
80+
{
81+
!this.props.disabled &&
82+
<Stack className={styles.buttons} horizontal tokens={stackTokens}>
83+
<PrimaryButton disabled={isSaving} text={strings.Save} onClick={() => this.onSubmitClick()} />
84+
<DefaultButton disabled={isSaving} text={strings.Cancel} onClick={this.props.onCancelled} />
85+
</Stack>
86+
}
87+
88+
6589
</div>
6690
}
6791
</div>
@@ -303,7 +327,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
303327
let defaultContentType = await spList.contentTypes.select("Id", "Name").get();
304328
contentTypeId = defaultContentType[0]["Id"].StringValue;
305329
}
306-
const listFeilds = await this.getFormFields(listId, contentTypeId, context.pageContext.web.absoluteUrl);
330+
const listFeilds = await this.getFormFields(listId, contentTypeId, this.webURL);
307331
const tempFields: IDynamicFieldProps[] = [];
308332
let order: number = 0;
309333
const responseValue = listFeilds['value'];
@@ -340,7 +364,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
340364
lookupListId = field["LookupList"];
341365
lookupField = field["LookupField"];
342366
if (item !== null) {
343-
defaultValue = await this._spService.getLookupValue(listId, listItemId, field.InternalName, context.pageContext.web.absoluteUrl);
367+
defaultValue = await this._spService.getLookupValue(listId, listItemId, field.InternalName, this.webURL);
344368
}
345369
else {
346370
defaultValue = [];
@@ -351,14 +375,14 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
351375
lookupListId = field["LookupList"];
352376
lookupField = field["LookupField"];
353377
if (item !== null) {
354-
defaultValue = await this._spService.getLookupValues(listId, listItemId, field.InternalName, context.pageContext.web.absoluteUrl);
378+
defaultValue = await this._spService.getLookupValues(listId, listItemId, field.InternalName, this.webURL);
355379
}
356380
else {
357381
defaultValue = [];
358382
}
359383
}
360384
else if (fieldType === "TaxonomyFieldTypeMulti") {
361-
let response = await this._spService.getTaxonomyFieldInternalName(this.props.listId, field.InternalName, this.props.context.pageContext.web.absoluteUrl);
385+
let response = await this._spService.getTaxonomyFieldInternalName(this.props.listId, field.InternalName, this.webURL);
362386
hiddenName = response["value"];
363387
termSetId = field["TermSetId"];
364388
if (item !== null) {
@@ -414,7 +438,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
414438
}
415439
else if (fieldType === "UserMulti") {
416440
if (item !== null)
417-
defaultValue = await this._spService.getUsersUPNFromFieldValue(listId, listItemId, field.InternalName, context.pageContext.web.absoluteUrl);
441+
defaultValue = await this._spService.getUsersUPNFromFieldValue(listId, listItemId, field.InternalName, this.webURL);
418442
else {
419443
defaultValue = [];
420444
}
@@ -423,7 +447,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
423447
}
424448
else if (fieldType === "Thumbnail") {
425449
if (defaultValue !== null) {
426-
defaultValue = context.pageContext.web.absoluteUrl.split('/sites/')[0] + JSON.parse(defaultValue).serverRelativeUrl;
450+
defaultValue = this.webURL.split('/sites/')[0] + JSON.parse(defaultValue).serverRelativeUrl;
427451
}
428452
}
429453
else if (fieldType === "User") {
@@ -517,7 +541,7 @@ export class DynamicForm extends React.Component<IDynamicFormProps, IDynamicForm
517541
const {
518542
context
519543
} = this.props;
520-
const webAbsoluteUrl = !webUrl ? context.pageContext.web.absoluteUrl : webUrl;
544+
const webAbsoluteUrl = !webUrl ? this.webURL : webUrl;
521545
let apiUrl = '';
522546
if (contentTypeId !== undefined && contentTypeId !== '') {
523547
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)