Dynamically Pass List item parameter using pnp js
-
08-02-2021 - |
Question
I have created SPFx web part using React JS framework. I am reading the data from the CSV file and adding item to SharePoint list using inBatch
in PnP JS.
I was able to add the item to SharePoint list by passing the parameter name with static value. But I have a requirement of adding the column with it's value dynamically.
Below is the sample to add item with column value like below.
var ItemArray = [];
ItemArray.push(pnp.sp.web.lists.getByTitle('listdisplayname').items.inBatch(batch).add({
Title : "Title"
}));
Promise.all(ItemArray).then(function() {
console.log("Item Added successfully!!");
});
batch.execute();
Please let me know, if there is any way to pass parameter dynamically.
Example:
var ItemArray = [];
var body = "Title : dynamic value"
ItemArray.push(pnp.sp.web.lists.getByTitle('listdisplayname').items.inBatch(batch).add({
body
}));
Promise.all(ItemArray).then(function() {
console.log("Item Added successfully!!");
});
batch.execute();
Solution
Try Using something like below:
var ItemArray = [];
var internalNameOfTitleCol = "Title"
var titleColValue = "Here is a dynamic value for list item Title";
var body = {};
body[internalNameOfTitleCol] = titleColValue;
ItemArray.push(pnp.sp.web.lists.getByTitle('listdisplayname').items.inBatch(batch).add(body));
Promise.all(ItemArray).then(function() {
console.log("Item Added successfully!!");
});
batch.execute();
Note: Make sure you are using correct internal names of your SharePoint Columns.
Update from comments:
Try like below:
var body = {};
for(var l = 0; l < SPColumnName.length; l++){
body[SPInternalName[l]] = CSVColumnsVal[[l]];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange