Holden1515
Holden1515

Reputation: 709

Get SPFx List GUID with @pnp/sp

I'm creating a new list on my SharePoint site with the aid of @pnp/sp.

I create the list and create fields:

sp.web.lists.ensure("MySPListTest")
    .then((ler: ListEnsureResult) => {
        if (ler.created) {
            const batch = sp.web.createBatch();

            ler.list.fields.inBatch(batch).addText("TextField01");
            ler.list.fields.inBatch(batch).addDateTime("Date");
            ler.list.fields.inBatch(batch).addBoolean("Boolean",);
            ler.list.fields.inBatch(bacth).addLookup("Lookup", lookupListID, lookupFieldName);
        }
    })

I want to add a lookup field but I don't know how to get the GUID of a list on my site.

I get the list:

let list: List = sp.web.lists.getByTitle("Trucks")

But I don't see the ID anywhere on it.

enter image description here

Any suggestions on how to get the GUID of a List?

Upvotes: 2

Views: 2989

Answers (1)

Vadim Gremyachev
Vadim Gremyachev

Reputation: 59358

You forgot to execute the request (via get method) to return List properties:

let list = sp.web.lists.getByTitle(listTitle);
list.get().then(l => {
    console.log("List Id: " + l.Id);
}); 

References

Basic Operations

Upvotes: 3

Related Questions