【问题标题】:SPFx - having trouble read person field from SP listSPFx - 从 SP 列表中读取人员字段时遇到问题
【发布时间】:2020-03-04 04:40:03
【问题描述】:

我正在尝试从 SharePoint 中的人员字段获取数据。我的代码总是返回 8 行(它是正确的),但在包含 Person 的项目中,它返回 [object Obejct]。 enter image description here

export interface SPUser {
  Pracovnik: String;
}

.
.
private getListData(): void {
        this._getListData().then((response) => {
            this._renderList(response);
        });
      }

      private _renderList(items: SPUser[]): void {
        let html: string = '<table class="TFtable" border=1 width=100% style="border-collapse: collapse;">';
         html += `<th>Title</th>`;
         items.forEach((item: SPUser) => {
           if(item.Pracovnik != null) {
         html += `
            <tr> <td>${item.Pracovnik}</td> </tr>
         `};
        
        });
        html += `</table>`;
        const listContainer: Element = this.domElement.querySelector('#spGetListItems');
        listContainer.innerHTML = html;
        
      }


      private async _getListData(): Promise<SPUser[]> {
        return pnp.sp.web.lists.getByTitle("org_struktura").items.select("Pracovnik/ID").expand("Pracovnik").get().then((response) => {
          return response;
        });
      }
      
      
      
      public render(): void {
      this.domElement.innerHTML = `
      <div class="parentContainer" style="background-color: lightgrey">
      <div style="background-color: lightgrey" id="spGetListItems" />
      </div> 
      `;  
      this.getListData();    
    }

请问有什么问题吗?

【问题讨论】:

    标签: list sharepoint field spfx


    【解决方案1】:

    将 SharePoint 列表项映射到 Type 对象的示例演示。

    export interface IReactItem{ 
      Id:number,
      Title:string,
      Description:string,
      User:{ID:number,EMail:string},
      enableEdit:boolean
    }
    
    private async _getListData(): Promise<IReactItem[]> {
      return pnp.sp.web.lists.getByTitle("TestList").items.select("Id,Title,Description,User/ID,User/EMail").expand("User").get().then((response:IReactItem[]) => {
        return response;
      });
    }
    

    【讨论】:

    • 太棒了!它需要8个小时的睡眠,你的回答很好,谢谢!像魅力一样工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-07
    • 2015-02-19
    • 1970-01-01
    • 2014-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多