typescript 为什么我的代码无法从sharepoint站点获取列表?

bsxbgnwa  于 2022-12-24  发布在  TypeScript
关注(0)|答案(1)|浏览(104)

我一直在尝试使用此代码从sharepoint站点获取列表,但列表未加载到响应中。有人能帮助解决此问题吗?我也尝试了其他解决方案,只是列表获取留在此代码中,否则代码工作正常。我已正确地提到了表的名称,以及serve.json中站点的URL

import { IPropertyPaneConfiguration } from '@microsoft/sp-property-pane';

import { BaseAdaptiveCardExtension } from '@microsoft/sp-adaptive-card-extension-base';

import { CardView } from './cardView/CardView';

import { QuickView } from './quickView/QuickView';

import { HelloWorldPropertyPane } from './HelloWorldPropertyPane';

import { SPHttpClient, SPHttpClientResponse } from '@microsoft/sp-http';


export interface IHelloWorldAdaptiveCardExtensionProps {

  title: string;

  description: string;

  iconproperty: string;

  targetlist: string;

}


export interface IHelloWorldAdaptiveCardExtensionState {

  description: string;

  items:IListItem[];

}

export interface IListItem{

  EmployeeName:string;

  City:string;

  ContactNumber:string;

  BaseLocation:string;

}


const CARD_VIEW_REGISTRY_ID: string = 'HelloWorld_CARD_VIEW';

export const QUICK_VIEW_REGISTRY_ID: string = 'HelloWorld_QUICK_VIEW';


export default class HelloWorldAdaptiveCardExtension extends BaseAdaptiveCardExtension<

  IHelloWorldAdaptiveCardExtensionProps,

  IHelloWorldAdaptiveCardExtensionState

> {

  private _deferredPropertyPane: HelloWorldPropertyPane | undefined;


  public onInit(): Promise<void> {

    this.state = {

      description:this.properties.description,

      items:[]

    };


    this.cardNavigator.register(CARD_VIEW_REGISTRY_ID, () => new CardView());

    this.quickViewNavigator.register(QUICK_VIEW_REGISTRY_ID, () => new QuickView());


    setTimeout(this.loadSpoData,500);

    return Promise.resolve();

  }


  public get title():string{

    return this.properties.title;

  }


  private loadSpoData=async(): Promise<void>=>{

    if(this.properties.targetlist){

      this.context.spHttpClient.get(

        `${this.context.pageContext.web.absoluteUrl}/_api/lists/GetByTitle('MyCompanyEmployeeList')/items`,

        SPHttpClient.configurations.v1)

        .then((response:SPHttpClientResponse)=>{

          response.json().then((responseJSON:any)=>{

            const items:IListItem[]=(<any[]>(responseJSON.value))

            .map<IListItem>((v:{EmployeeName:string;City:string;ContactNumber:string;BaseLocation:string;}): IListItem=>{

              return{

                EmployeeName:v.EmployeeName,

                City: v.City,

                ContactNumber: v.ContactNumber,

                BaseLocation: v.BaseLocation

              };

            });


            this.setState({

              items:items

            });

          });

        });

    }

  }


  protected loadPropertyPaneResources(): Promise<void> {

    return import(

      /* webpackChunkName: 'HelloWorld-property-pane'*/

      './HelloWorldPropertyPane'

    )

      .then(

        (component) => {

          this._deferredPropertyPane = new component.HelloWorldPropertyPane();

        }

      );

  }


  protected renderCard(): string | undefined {

    return CARD_VIEW_REGISTRY_ID;

  }


  protected getPropertyPaneConfiguration(): IPropertyPaneConfiguration {

    return this._deferredPropertyPane?.getPropertyPaneConfiguration();

  }

}
mrzz3bfm

mrzz3bfm1#

一些建议:
1.您能否验证"targetList"属性是否填充在manifest.json中?
1.您可以尝试删除if(this. properties. targetlist){(和相应的右括号)并查看列表数据是否会在之后加载。我不确定如果检查的目的是什么,当您已经在GetByTitle('MyCompanyEmployeeList ')中硬编码了列表标题时

相关问题