0

So I can create a list just with ensure:

sp.web.lists.ensure(list)
    .then((ler: ListEnsureResult) => {

        if (ler.created) {
            console.log(list, " was created; creating column");

            ler.list.fields.addText("myTestColumn");
        }
        return ler.created;
    });

But is not creating a column.

I have also tried code samples from here and here, neither of which have worked.

Holden1515
  • 659
  • 2
  • 7
  • 20

1 Answers1

1

As mentioned in comments, your code is working fine.

Its just that the column is not visible in the List View. It is already created in the List and is visible when we check the list settings page or try to create a new list item.

So, now you need to add the column to the default list view. For that, you just need to make additional REST API call to make it visible in the default list view as below:

sp.web.lists.ensure(list)
    .then((ler: ListEnsureResult) => {

        if (ler.created) {
            console.log(list, " was created; creating column");

            ler.list.fields.addText("myTestColumn").then(function(){

                const view = ler.list.defaultView;

                view.fields.add("myTestColumn");

          });           

        }
        return ler.created;
    });
Gautam Sheth
  • 2,442
  • 1
  • 17
  • 16