pnp / pnpjs

Fluent JavaScript API for SharePoint and Microsoft Graph REST APIs
https://pnp.github.io/pnpjs/
Other
753 stars 305 forks source link

How to update SharePoint Online column type using pnp/sp? #2944

Closed bhoomesh-spe closed 6 months ago

bhoomesh-spe commented 6 months ago

What version of PnPjs library you are using

3.x

Minor Version Number

3.15.0

Target environment

SharePoint Framework

Additional environment details

SharePoint Online, SPFx, React

Question/Request

Hello Everyone,

I have created a SharePoint list and column programmatically using the below code.

import { spfi } from "@pnp/sp";
import "@pnp/sp/webs";
import "@pnp/sp/lists";
import "@pnp/sp/items";
import "@pnp/sp/fields";

const sp = spfi(...);
let listCreate = await sp.web.lists.ensure("MyList", "MyList Description", 100, false, { Hidden: false });
let addColumn = await sp.web.lists.getByTitle("MyList").fields.addText("Comment")
let addColumnInView = await sp.web.lists.getByTitle("MyList").defaultView.fields.add("Comment");

I have taken a reference from Add a New Field and Ensure that a List exists links.

Now, I need to change the Comment column type from single-line text to multi-line text. So, I have checked Update a Field link but there is no option for changing the type of the existing column.

Can anyone help me with the same?

Thanks

bcameron1231 commented 6 months ago

Hi. Just pass in the multi-line text field props you want, plus the Field Types

  const field = sp.web.lists.getByTitle("Test").fields.getByTitle('SingleLine');
  await field.update({
     NumberOfLines: 6,
     FieldTypeKind: 3
  }, "SP.FieldMultiLineText");
bhoomesh-spe commented 6 months ago

@bcameron1231 - Thanks for the reply. It threw me an error NumberOfLines property is not present.

bcameron1231 commented 6 months ago

@bhoomesh-spe Ah, apologies, ya you can't set the number of lines until it's a multiline text field.

  const field = sp.web.lists.getByTitle("Test").fields.getByTitle('SingleLine');

   //convert to multiline text field
  await field.update({
     FieldTypeKind: 3
  }, "SP.FieldMultiLineText");

  // update multiline text field (optional)
   await field.update({
     NumberOfLines: 6,
  }, "SP.FieldMultiLineText");
bcameron1231 commented 6 months ago

Closing this issue as answered. If you have additional questions or we did not answer your question, please open a new issue, ref this issue, and provide any additional details available. Thank you!

github-actions[bot] commented 6 months ago

This issue is locked for inactivity or age. If you have a related issue please open a new issue and reference this one. Closed issues are not tracked.