0

I'm creating a list with PnP with an allready existing content type and then I'm trying to add a custom view with the columns from that content type.

The problem is that the view gets created but it only have some of the columns and it is random witch columns gets added when i run the function.

Can anybody tell me what is wrong?

Here's the code that creates the list with existing content type and adds the view. enter image description here

Thanks in advance.

  • Are those columns present in the content type? Checked internal names ? What exactly is the issue ? What should happen, can you elaborate? – Gautam Sheth Sep 3 '18 at 18:46
  • I found a solution but thanks for the reply. – fkgdmk Sep 6 '18 at 6:27
  • awesome, you can post that solution as answer, it will help the community ! You can then accept your own answer as solution as well ! Cheers :) – Gautam Sheth Sep 6 '18 at 6:31
0

This is something that is unique to SharePoint list views and not in a good way unfortunately. Even I learnt it when I tried your code.

To add columns to the list views, they need to be done one after the other and not in parallel. Perhaps that is why you got tripped up.

So, you can use either of the below mentioned code to add your fields to the created list view:

1) Using then syntax:

spList.list.views.add("My New View 4", false, {
      RowLimit: 20,
      DefaultView: true,
      ViewQuery: "<OrderBy><FieldRef Name='Modified' Ascending='False' /></OrderBy>",
    }).then((v) => {

    v.view.fields.removeAll().then(() => {

        v.view.fields.add("AnniversariesEventTitle").then(() =>{
        v.view.fields.add("AnniversariesDate").then(() =>{
        v.view.fields.add("AnniversariesPerson").then(() =>{                
                    console.log("Done");
                });
            });
        });       
    });
});

2) Using batch endpoint, it wont work in 2013 AFAIK, but in SP Online, SP 2016 and upcoming SP 2019, it will work:

spList.list.views.add("My New View 4", false, {
      RowLimit: 20,
      DefaultView: true,
      ViewQuery: "<OrderBy><FieldRef Name='Modified' Ascending='False' /></OrderBy>",
    }).then((v) => {

        var spBatch = sp.web.createBatch();

        var fieldArray = ['AnniversariesEventTitle','AnniversariesDate','AnniversariesPerson'];

        v.view.fields.inBatch(spBatch).removeAll();
        fieldArray.forEach(fieldInternalName => {
            v.view.fields.inBatch(spBatch).add(fieldInternalName);
        });

        spBatch.execute()
        .then(console.log)
        .catch(console.log)    
});

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.