[英]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>
<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.