简体   繁体   English

使用 React PNPJs 推送到 SharePoint 列表后检索 ID

[英]Retrieve ID after pushing to SharePoint list using React PNPJs

I'm building a SharePoint SPFx react app.我正在构建一个 SharePoint SPFx 反应应用程序。 In a nutshell, the user fills out a form that I created.简而言之,用户填写了我创建的表单。 When the user hit's submit, using PNPJs: https://pnp.github.io/pnpjs/sp/items/ I'm adding the item to a list called Request .当用户点击提交时,使用 PNPJs: https://pnp.github.io/pnpjs/sp/items/我将该项目添加到名为Request的列表中。

From there I want to send an email that contains the URL link to that item they created.从那里我想发送一个 email ,其中包含指向他们创建的那个项目的 URL 链接。 Right now, my code adds the item to the list and I'm able to send an email with no problem.现在,我的代码将项目添加到列表中,我可以毫无问题地发送 email。 However, I want to get the ID of the item that was just added to the list, so that I can add it to the email.但是,我想获取刚刚添加到列表中的项目的 ID,以便可以将其添加到 email。

Here is a striped down snippet of my function that adds items to the Request list.这是我的 function 的条带化片段,它将项目添加到Request列表中。

async submitNewRequest():Promise<any> {

    let preprocessedData;

    try {       

        // add an item to the list
        pnp.sp.web.lists.getByTitle("Requests").items.add({
            Title: this.state.Title,
            Requestor_x0020_Email: this.state.getEmail,
            Created: this.state.startDate,

        }).then((iar) => {
            console.log(iar);
            //Is this where I would get the ID 
        }); 
            
        const emailProps: EmailProperties = {
            To: [this.state.getEmail],
            Subject: "Your court requisition has been submitted.",
            Body: this.initalMessage
        };
        
    } catch(error) {

    }

    return preprocessedData;            


}

I believe what I have to do is in the .then((iar) => { when item is successfully added to the list, to get a response back with that item ID. But I'm not sure how. In my const emailProps: EmailProperties is where I'm sending the email, which again works.我相信我必须做的是在.then((iar) => {当项目成功添加到列表中时,以使用该项目 ID 获得回复。但我不确定如何。在我的const emailProps: EmailProperties是我发送 email 的地方,它再次有效。

Typically I can do something like this await sp.web.lists.getByTitle("Request").items.getById(1).get();通常我可以做这样的事情await sp.web.lists.getByTitle("Request").items.getById(1).get(); and in the console I will get back something like this:在控制台中我会得到这样的东西:

0:
Title: "My title here"
Description: "Description Here"
ID: 24

Here is on submit function:这是提交 function:

async _onNewRequest(event) {
    event.preventDefault();
    await this.submitNewRequest();
    this.displayPop();
}   

And lastly my email function:最后是我的 email function:

get initalMessage() {
    return `<p>This is my email template that I stripped down.</p>
    
    &nbsp;
    <p>
    <a href="https://mywebsite.sharepoint.com/sites/Requests/SitePages/Home.aspx#/MyRequests/'+ NEED_ID_HERE +'" target="_blank">
        Click Here
    </a>
</p>`; 

You could retrieve the Id of the item like this:您可以像这样检索项目的 ID:

  sp.web.lists.getByTitle("ct0").items.add({
    Title:"test"
  }).then(iar=>{
    console.log(iar.data.ID);
  })

The code would be like this:代码将是这样的:

  const iar=await sp.web.lists.getByTitle("ct0").items.add({
    Title:"test"
  });
  const id=iar.data.ID;
  const emailProps: EmailProperties = {
        To: [this.state.getEmail],
        Subject: "Your court requisition has been submitted.",
        Body: this.initalMessage,
        ID:id
  };

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

相关问题 React SPFx - 使用 PnPjs 将文件添加到 SharePoint 列表字段 - React SPFx - Adding files to SharePoint list field using PnPjs 使用 React &amp; PnPJS 更新 SharePoint spfx 中的数组 - Updating an array in SharePoint spfx using React & PnPJS SharePoint React pnpjs - SharePoint React pnpjs Sharepoint 框架 SPFx - 带有 React 的 Pnp/PnPjs - 分类选择器不工作 - Sharepoint Framework SPFx - Pnp/PnPjs with React - Taxonomy Picker not working 类型错误:使用 pnpjs 在 SharePoint 中上传文件时无法获取 - Typeerror : failed to fetch while Uploading file in SharePoint using pnpjs React PNPJs sendemail CC 和 BCC 不起作用 - React PNPJs sendemail CC and BCC not working 使用 React 组件中另一个 object 的 id 通过 id 检索 object - Retrieve object by id using the id of another object in React Component 如何使用 PnPJS 检查当前用户是否属于 sharepoint 组? - How can I check if current user is part of a sharepoint group with PnPJS? 将 Microsoft 身份验证库 MSAL 与 PnPjs 一起使用 - Using Microsoft Authentication Library MSAL with PnPjs 如何使用ID /密钥(使用React)从Firebase检索和呈现数据? - How to Retrieve and Render Data from Firebase using ID/Key (with React)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM