簡體   English   中英

無法使用 React APP WEB 部分從網站內容的特定列表項中獲取數據

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

我能夠獲取我們在站點內容的登陸屏幕上看到的列表。 但是,當我嘗試通過按標題查找特定項目來獲取數據時,我在 URL 中出現錯誤 CANNOT FIND LIST 'EmployeeList'。

在此處輸入圖像描述

我已經構建了一個 React Web 部件,這里是文件和代碼

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'."}}


請指教。

已修復的問題:通過從 _getMockListData 獲取數據,WebPart 在本地運行良好。

但是,不是當我嘗試在https://MyOffice365.sharepoint.com/sites/**InCorrectSPSite**/_layouts/15/workbench.aspx上測試 WebPart 時

后來我注意到我指向了錯誤的 SP 站點。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM