diff --git a/src/controls/dynamicForm/DynamicForm.tsx b/src/controls/dynamicForm/DynamicForm.tsx index eb3a2e609..2bb9d76ae 100644 --- a/src/controls/dynamicForm/DynamicForm.tsx +++ b/src/controls/dynamicForm/DynamicForm.tsx @@ -488,8 +488,9 @@ export class DynamicForm extends React.Component< try { const spList = await sp.web.lists.getById(listId); let item = null; + const isEditingItem = listItemId !== undefined && listItemId !== null && listItemId !== 0; let etag: string | undefined = undefined; - if (listItemId !== undefined && listItemId !== null && listItemId !== 0) { + if (isEditingItem) { item = await spList.items.getById(listItemId).get(); if (onListItemLoaded) { @@ -510,6 +511,7 @@ export class DynamicForm extends React.Component< const listFields = await this.getFormFields( listId, contentTypeId, + isEditingItem, this.webURL ); const tempFields: IDynamicFieldProps[] = []; @@ -706,6 +708,9 @@ export class DynamicForm extends React.Component< defaultValue = JSON.parse(defaultValue); } else if (fieldType === "Boolean") { defaultValue = Boolean(Number(defaultValue)); + } else if (fieldType === "File") { + const fileName = await this._spService.getFileName(listId, listItemId); + defaultValue = fileName; } tempFields.push({ @@ -802,6 +807,7 @@ export class DynamicForm extends React.Component< private getFormFields = async ( listId: string, contentTypeId: string | undefined, + isEditingItem: boolean, webUrl?: string // eslint-disable-next-line @typescript-eslint/no-explicit-any ): Promise => { @@ -818,7 +824,7 @@ export class DynamicForm extends React.Component< } else { 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')`; + )}'&$filter=ReadOnlyField eq false and Hidden eq false and (FromBaseType eq false or StaticName eq 'Title' ${isEditingItem ? "or StaticName eq 'FileLeafRef'" : ''})`; } } else { apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/fields?@listId=guid'${encodeURIComponent( diff --git a/src/controls/dynamicForm/dynamicField/DynamicField.tsx b/src/controls/dynamicForm/dynamicField/DynamicField.tsx index b8641731a..c0d7ccbf9 100644 --- a/src/controls/dynamicForm/dynamicField/DynamicField.tsx +++ b/src/controls/dynamicForm/dynamicField/DynamicField.tsx @@ -525,6 +525,24 @@ export class DynamicField extends React.Component; + + case 'File': + return
+
+ + {labelEl} +
+ { this.onChange(newText); }} + disabled={disabled} + onBlur={this.onBlur} + errorMessage={errorText} + /> + {descriptionEl} +
; } return null; diff --git a/src/services/SPService.ts b/src/services/SPService.ts index 466d785c8..12ae505ac 100644 --- a/src/services/SPService.ts +++ b/src/services/SPService.ts @@ -793,4 +793,24 @@ export default class SPService implements ISPService { resolve(listData); }); } + + public async getFileName(listId: string, itemId: number, webUrl?: string): Promise { + try { + const webAbsoluteUrl = !webUrl ? this._context.pageContext.web.absoluteUrl : webUrl; + + const apiUrl = `${webAbsoluteUrl}/_api/web/lists(@listId)/items(${itemId})?@listId=guid'${encodeURIComponent(listId)}'&$select=FileLeafRef`; + const data = await this._context.spHttpClient.get(apiUrl, SPHttpClient.configurations.v1); + if (data.ok) { + const results = await data.json(); + if (results) { + return results.FileLeafRef; + } + } + + return null; + } catch (error) { + console.dir(error); + return Promise.reject(error); + } + } }