0

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();
1

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]];
}
| improve this answer | |
  • Thanks for the update but I am column name (i.e. "Title") is also dynamic. for(var l = 0; l < SPColumnName.length; l++){ body += SPInternalName[l] + " : " + CSVColumnsVal[[l]] + ","; } – Rushil Shah Jan 7 at 13:28
  • Welcome. Please check the updated answer. This will solve your issue. – Ganesh Sanap Jan 7 at 13:34
  • I have tried the above approach. It is created array but while adding the item it is still throwing the error like below. Uncaught (in promise) ProcessHttpClientResponseException: Error making HttpClient request in queryable: [400] Bad Request – Rushil Shah Jan 7 at 13:52
  • Well, that issue is something related to your PnP JS call. For this check the documentation here. Is there any reason you are using Promises? – Ganesh Sanap Jan 7 at 13:56
  • 1
    I am now able to add the item. Thanks for the quick answer. – Rushil Shah Jan 7 at 14:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.