0

I am using Sharepoint pnp js library to create a list: https://pnp.github.io/pnpjs/sp/lists/

How do I specify list URL (not title) when creating it?

Means, if I want my list URL to look like this (for example): https://contoso.sharepoint.com/sites/Site/lists/SomeCustomListUrl

I am interested how do you specify lists/SomeCustomListUrl part when creating a list.

For example, if I create it using CSOM, there Url parameter in ListCreationInformation, in SPMeta2 there is CustomUrl parameter in the list definition. I could not find anything similar in pnp js?

If not possible with pnp library, is it possible to do that with REST api?

Nikolay
  • 10,752
  • 2
  • 23
  • 51

2 Answers2

0

In PnP js , no need to specify the List Url, just need to pass the ListName and it will create the List Url automatically. It will create the list at the site where SPFX Solution is running, please check the following demo:

  sp.web.lists.add("SomeCustomListUrl", "This is a description of Generic List.", 100, true, { OnQuickLaunch: true }).then(i => {
      console.log(i);
    })

This is the all parameter in sp.web.lists.add function:

enter image description here

Jerry
  • 3,480
  • 1
  • 10
  • 12
  • Thank you Jerry, but the point is I do want the list URL and the list title to be different, thus the question. I do not want the list URL to be auto-generated. The reason is that user is allowed change the list title later, but the app needs a way to find the list (and not by ID, because ID is site-specific). Could you recommend a better way to achieve this? – Nikolay Jul 10 '20 at 14:34
  • The concept is similar to sharepoint field names: there is "internal name" used by application, there is "title" editable by user, and there is "id" that is unique guid. So I want to have the "internal name", for a list. Before, we used list URL for this purpose. – Nikolay Jul 10 '20 at 14:43
  • 1
    For PnP.js and Rest API, they both didn't provider Url paramater for override, this is the demo Rest API call, https://www.codesharepoint.com/rest-api/create-list-column-in-sharepoint-using-rest-api. also only provide the list title. As this limitation, I'm afraid, this should be some difference with CSOM.... – Jerry Jul 11 '20 at 15:07
0

Updating here in case it's helpful, this is still a gap between CSOM and PnPjs as of v2.0.9.

However, as a workaround, you can create the list with the URL as its title, and immediately update the title. For example:

const listUrl = "MyCustomList";
const listTitle = "My Custom List";

sp.web.lists.add(listUrl).then(res => res.list.update({ Title: listTitle }));

The end result of this is a list with a URL of [web]/Lists/MyCustomList, but with a list title of My Custom List.

Subsequent calls to retrieve the list using PnPjs should use the new title, e.g. web.lists.getByTitle("My Custom List");.

Chris Romp
  • 238
  • 1
  • 7