From ca866d88754edb7417b6193e3f2a4621f4e5bbf0 Mon Sep 17 00:00:00 2001 From: Beau Cameron Date: Thu, 21 Sep 2023 10:39:00 -0600 Subject: [PATCH 1/2] Bug FIx for 2783 Updated FieldType Mapping Added Documentation Added new AddImageField Test --- docs/sp/fields.md | 25 +++++++++++++++++++++++++ packages/sp/fields/types.ts | 2 +- test/sp/fields.ts | 5 +++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/docs/sp/fields.md b/docs/sp/fields.md index 48449e729..9eeaef2ee 100644 --- a/docs/sp/fields.md +++ b/docs/sp/fields.md @@ -254,6 +254,31 @@ const r = await field.field.select("Id")(); console.log(r.Id); ``` +### Add an Image Field + +Use the addImageField method to create a new image field. + +```TypeScript +import { spfi } from "@pnp/sp"; +import { IFieldAddResult, FieldTypes } from "@pnp/sp/fields/types"; +import "@pnp/sp/webs"; +import "@pnp/sp/lists"; +import "@pnp/sp/fields"; + +const sp = spfi(...); + +// create a new image field called 'My Field' in web. +const field: IFieldAddResult = await sp.web.fields.addImageField("My Field", { FieldTypeKind:FieldTypes.Image }); +// create a new image field called 'My Field' in the list 'My List'. +const field2: IFieldAddResult = await sp.web.lists.getByTitle("My List").fields.addImageField("My Field", { FieldTypeKind:FieldTypes.Image }); + +// we can use this 'field' variable to run more queries on the field: +const r = await field.field.select("Id")(); + +// log the field Id to console +console.log(r.Id); +``` + ### Add a Multi-line Text Field Use the addMultilineText method to create a new multi-line text field. diff --git a/packages/sp/fields/types.ts b/packages/sp/fields/types.ts index 3b801afb6..bc013ec9d 100644 --- a/packages/sp/fields/types.ts +++ b/packages/sp/fields/types.ts @@ -485,7 +485,7 @@ const FieldTypeClassMapping = { [FieldTypes.DateTime]:"SP.FieldDateTime", [FieldTypes.GridChoice]:"SP.FieldRatingScale", [FieldTypes.Guid]: "SP.FieldGuid", - [FieldTypes.Image]:"FieldMultiLineText", + [FieldTypes.Image]:"SP.FieldMultiLineText", [FieldTypes.Integer]:"SP.FieldNumber", [FieldTypes.Location]:"SP.FieldLocation", [FieldTypes.Lookup]:"SP.FieldLookup", diff --git a/test/sp/fields.ts b/test/sp/fields.ts index 2684e3843..0de9a3c99 100644 --- a/test/sp/fields.ts +++ b/test/sp/fields.ts @@ -64,6 +64,11 @@ describe("Fields", function () { const field = await this.pnp.sp.web.fields.addText(testFieldNameRand, { Group: testFieldGroup }); return expect(field.data.Title).to.be.equal(testFieldNameRand); }); + it("addImageField", async function () { + const testFieldNameRand = `${testFieldName}_${getRandomString(10)}`; + const field = await this.pnp.sp.web.fields.addImageField(testFieldNameRand, { Group: testFieldGroup, FieldTypeKind:FieldTypes.Image }); + return expect(field.data.Title).to.be.equal(testFieldNameRand); + }); it("addNumber", async function () { const testFieldNameRand = `${testFieldName}_${getRandomString(10)}`; const field = await this.pnp.sp.web.fields.addNumber(testFieldNameRand, { Group: testFieldGroup }); From 00307c2e29efb4f45c8cecc472dd12e7e13a9427 Mon Sep 17 00:00:00 2001 From: Beau Cameron Date: Fri, 22 Sep 2023 07:46:57 -0600 Subject: [PATCH 2/2] Updating Documentation Sample and Test Passing in FieldTypeKind is not required as the addImageField method already does so for you. --- docs/sp/fields.md | 4 ++-- test/sp/fields.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/sp/fields.md b/docs/sp/fields.md index 9eeaef2ee..8ec6ce5f8 100644 --- a/docs/sp/fields.md +++ b/docs/sp/fields.md @@ -268,9 +268,9 @@ import "@pnp/sp/fields"; const sp = spfi(...); // create a new image field called 'My Field' in web. -const field: IFieldAddResult = await sp.web.fields.addImageField("My Field", { FieldTypeKind:FieldTypes.Image }); +const field: IFieldAddResult = await sp.web.fields.addImageField("My Field"); // create a new image field called 'My Field' in the list 'My List'. -const field2: IFieldAddResult = await sp.web.lists.getByTitle("My List").fields.addImageField("My Field", { FieldTypeKind:FieldTypes.Image }); +const field2: IFieldAddResult = await sp.web.lists.getByTitle("My List").fields.addImageField("My Field"); // we can use this 'field' variable to run more queries on the field: const r = await field.field.select("Id")(); diff --git a/test/sp/fields.ts b/test/sp/fields.ts index 0de9a3c99..1ff44b14e 100644 --- a/test/sp/fields.ts +++ b/test/sp/fields.ts @@ -66,7 +66,7 @@ describe("Fields", function () { }); it("addImageField", async function () { const testFieldNameRand = `${testFieldName}_${getRandomString(10)}`; - const field = await this.pnp.sp.web.fields.addImageField(testFieldNameRand, { Group: testFieldGroup, FieldTypeKind:FieldTypes.Image }); + const field = await this.pnp.sp.web.fields.addImageField(testFieldNameRand, { Group: testFieldGroup}); return expect(field.data.Title).to.be.equal(testFieldNameRand); }); it("addNumber", async function () {