无法使用 React APP 从网站内容的特定列表项中获取数据 WEB-part

Not able to fetch data from particular list item of site content using React APP WEB-part

我能够获取我们在 site-content 的登录屏幕上看到的列表。但是,当我尝试通过按标题查找特定项目来获取数据时,出现错误 CANNOT FIND LIST 'EmployeeList' IN THE URL.

我构建了一个 React Web-part,这里是文件和代码

ListOfSprintStories.tsx

private _getListData(): Promise<ISPLists> {  
    return this.props.context.spHttpClient.get(this.props.context.pageContext.web.absoluteUrl + `/_api/web/lists/GetByTitle('EmployeeList')/Items`, SPHttpClient.configurations.v1)  
        .then((response: SPHttpClientResponse) => {   
          return response.json();  
        });  
    }

  private _renderListAsync(): void {
    // Local environment
    if (Environment.type === EnvironmentType.Local) {
      this._getMockListData().then((response) => {
        this.setState({ finalList: response.value });
      });
    }
    else if (Environment.type == EnvironmentType.SharePoint ||
      Environment.type == EnvironmentType.ClassicSharePoint) {
      this._getListData()
        .then((response) => {
          console.log('======>', response.value)
          this.setState({ finalList: response.value });
        });
    }
  }

  componentDidMount() {
    this._renderListAsync()
  }

IListOfSprintStoriesProps.ts

import { WebPartContext } from '@microsoft/sp-webpart-base';

export interface IListOfSprintStoriesProps {
  description: string;
  context: WebPartContext;
}

ListOfSprintStoriesWebPart.ts

import * as React from 'react';
import * as ReactDom from 'react-dom';
import { Version } from '@microsoft/sp-core-library';
import {
  IPropertyPaneConfiguration,
  PropertyPaneTextField
} from '@microsoft/sp-property-pane';
import { BaseClientSideWebPart } from '@microsoft/sp-webpart-base';

import * as strings from 'ListOfSprintStoriesWebPartStrings';
import ListOfSprintStories from './components/ListOfSprintStories';
import { IListOfSprintStoriesProps } from './components/IListOfSprintStoriesProps';
import { WebPartContext } from '@microsoft/sp-webpart-base';

export interface IListOfSprintStoriesWebPartProps {
  description: string;
  context: WebPartContext;
}

export default class ListOfSprintStoriesWebPart extends BaseClientSideWebPart<IListOfSprintStoriesWebPartProps> {

  public render(): void {
    const element: React.ReactElement<IListOfSprintStoriesProps> = React.createElement(
      ListOfSprintStories,
      {
        description: this.properties.description,
        context: this.context
      }
    );

    ReactDom.render(element, this.domElement);
  }

  protected onDispose(): void {
    ReactDom.unmountComponentAtNode(this.domElement);
  }

  protected get dataVersion(): Version {
    return Version.parse('1.0');
  }

  protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {
    return {
      pages: [
        {
          header: {
            description: strings.PropertyPaneDescription
          },
          groups: [
            {
              groupName: strings.BasicGroupName,
              groupFields: [
                PropertyPaneTextField('description', {
                  label: strings.DescriptionFieldLabel
                })
              ]
            }
          ]
        }
      ]
    };
  }
}

我已按照文档进行操作。

https://docs.microsoft.com/en-us/sharepoint/dev/spfx/web-parts/get-started/connect-to-sharepoint

我能够获取完整的站点内容列表(文档库),但是当我尝试使用 getByTitle('EmployeeList') 获取特定列表时,它失败了。

错误信息如下:



{"error":{"code":"-1, System.ArgumentException","message":"List 'EmployeeList' does not exist at site with URL 'https://myTenant.sharepoint.com'."}}


请指教

问题已解决:WebPart 通过从 _getMockListData 获取数据在本地正常工作。

但是,当我尝试在 https://MyOffice365.sharepoint.com/sites/**InCorrectSPSite**/_layouts/15/workbench.aspx

上测试 WebPart 时,情况并非如此

后来我发现我指向了错误的 SP 站点。