0

我在我的 webpart 中使用此代码插入到带有列表名称的共享点列表中,但是将此代码移动到生产环境中会产生一个问题,因为它形成了用于插入列表的错误 url,生产中的 url 是 https:// abcportal.sharepoint.com/sites/SolutionBook/SitePages/_api/web/lists/getByTitle('Smart%20City%20IAQ%20Demo%20Requests')?$select=ListItemEntityTypeFullName

但是在本地环境中工作正常,它形成了这个 url https://abcportal.sharepoint.com/sites/solutionbooktest/_api/web/lists/getByTitle('Smart City IAQ Demo Requests')/items

生产环境中URL SitePages 自动来了,怎么去掉?

--------代码--------- public insertEmailToList() {

pnp.sp.web.lists.getByTitle("Smart City IAQ Demo Requests").items.add({
  Title: this.state.Email
}).then(r => {
  this.setState({ ButtonActive: false });
});

}

或者有没有办法使用列表的 URL 插入共享点列表?

4

1 回答 1

0

您需要为 PnPJ 建立 SPFx 上下文。这可以通过从@pnp/core@pnp/sp导入的setup()方法在 Web 部件的onInit()方法中完成。

使用@pnp/core 设置

import { setup as pnpSetup } from "@pnp/core";

// ...

protected onInit(): Promise<void> {

  return super.onInit().then(_ => {

    // other init code may be present

    pnpSetup({
      spfxContext: this.context
    });
  });
}

// ...

使用@pnp/sp 设置

import { sp } from "@pnp/sp/presets/all";

// ...

protected onInit(): Promise<void> {

  return super.onInit().then(_ => {

    // other init code may be present

    sp.setup({
      spfxContext: this.context
    });
  });
}

// ...

有关更多详细信息,请参阅此链接

于 2022-02-18T12:19:45.720 回答