简体   繁体   English

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

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

I am able to fetch the list which we see on the landing screen of the site-content.我能够获取我们在站点内容的登陆屏幕上看到的列表。 However, when I am trying to fetch data by finding a particular item by its title I get an error CANNOT FIND LIST 'EmployeeList' IN THE URL.但是,当我尝试通过按标题查找特定项目来获取数据时,我在 URL 中出现错误 CANNOT FIND LIST 'EmployeeList'。

在此处输入图像描述

I have built a React Web-part and here are the files and code我已经构建了一个 React Web 部件,这里是文件和代码

ListOfSprintStories.tsx 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 IListOfSprintStoriesProps.ts

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

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

ListOfSprintStoriesWebPart.ts 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
                })
              ]
            }
          ]
        }
      ]
    };
  }
}

I have followed the documentation.我已按照文档进行操作。

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

I was able to fetch the complete site content list(Document library), but when I am trying to fetch a particular list using getByTitle('EmployeeList'), it fails.我能够获取完整的站点内容列表(文档库),但是当我尝试使用 getByTitle('EmployeeList') 获取特定列表时,它失败了。

Here is the error message:这是错误消息:



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


Please Advice.请指教。

Issue fixed: The WebPart was working fine locally by fetching data from _getMockListData.已修复的问题:通过从 _getMockListData 获取数据,WebPart 在本地运行良好。

However, it wasn't when I was trying to test the WebPart on the https://MyOffice365.sharepoint.com/sites/**InCorrectSPSite**/_layouts/15/workbench.aspx但是,不是当我尝试在https://MyOffice365.sharepoint.com/sites/**InCorrectSPSite**/_layouts/15/workbench.aspx上测试 WebPart 时

I later noticed that I was pointing to wrong SP-site.后来我注意到我指向了错误的 SP 站点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何删除所有出现的相同 Web 部件列表的标题 - How to remove headers on all occurrences of the same Web-part list SharePoint | 网页部分| 如何根据状态限制下拉菜单选项 - SharePoint | Web-Part | How to Limit Dropdown Options Based on the Status 在React中使用Fetch从服务器获取数据 - Using Fetch in React to fetch data from server 无法通过网络应用将数据保存在Firebase中 - Not able to save data in firebase from web app 尝试从反应应用程序的本地主机 web api 端点获取数据 - NoCors 错误 - Trying to Fetch data from a localhost web api end point from react app - NoCors error 在React中使用fetch()从API使用数据 - Using data from API using fetch() in React 如何仅从反应本机中的数组中获取特定项目的数据 - How to Fetch data of specific Item only from array in react native 如何使用反应钩子从第一个 api 调用结果中获取第二个 api 调用需要特定数据的序列 api 调用? - How to fetch sequence api calls where second api call need particular data from first api call result using react hooks? 在反应中使用该项目的 ID 滚动到特定项目? - Scroll to particular Item using ID of that item in react? 使用 React UseEffect() 从 GraphQL 获取数据 - Using React UseEffect() to fetch data from GraphQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM