[英]SPFx using @pnp/sp to create list
我試圖使用@ pnp / sp創建一個自定義列表,在我的例程中,我需要檢查列表是否存在,如果不存在,我將創建該列表並添加其列。
下面的代碼有時可以工作,可能是因為sp.web。*方法是異步的,並且會引起問題。
那么,正確的方法是:1)檢查特定列表,2)添加列表(如果不存在),3)將字段添加到列表中?
sp.web.lists.ensure("SliceBox").then( List => {
List.fields.getByTitle("Body").get().catch( f => {
f.fields.addMultilineText("Body", 4, true, false, false, true);
});
List.fields.getByTitle("Link").get().catch( f => {
f.fields.addUrl("Link", UrlFieldFormatType.Hyperlink);
});
List.fields.getByTitle("Visible").get().catch( f => {
f.fields.addBoolean("Visible");
});
})
.catch( err => {
console.log("> Failure: ", err);
});
如果我嘗試使用非常明確的方式(請參見下文)也沒關系,它也會失敗:
sp.web.lists.ensure("SliceBox").then( List => {
sp.web.lists.getByTitle("SliceBox").fields.getByTitle("Body").get().catch( f => {
f.fields.addMultilineText("Body", 4, true, false, false, true);
});
// ... shortened for brevity ...
})
.catch( err => {
console.log("> Failure: ", err);
});
我的示例測試代碼可以正常工作。
sp.web.lists.ensure("SliceBox").then( sliceBox => {
sliceBox.list.fields.getByTitle("Visible").get().catch( f => {
sliceBox.list.fields.addBoolean("Visible");
alert('fieldAdded');
});
})
更新:
嘗試這個:
sp.web.lists.ensure("SliceBox").then( sliceBox => {
sliceBox.list.fields.getByTitle("Visible").get().catch( f => {
sliceBox.list.fields.addBoolean("Visible").then(f =>{
sliceBox.list.fields.getByTitle("Link").get().catch( f => {
sliceBox.list.fields.addUrl("Link", UrlFieldFormatType.Hyperlink);
alert('done');
});
})
});
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.