Forum Discussion

Ricky Raghavan's avatar
Ricky Raghavan
Copper Contributor
Sep 11, 2018

Using Node PNP JS How to check whether a folder exists in a Document Library

There are lot of examples on the Internet about checking whether a folder exists using CSOM. I did a lot of searching but couldn't find a good article that talks about doing this using PNP JS. 

 

I would highly appreciate if anyone could give an Insight on how to approach this.

 

 

Thanks!

1 Reply

  • Ricky Raghavan  hope this will help you., below code works for me.

     

     

    public static getDocSetOrFolder(endPoint,listTitle,folerName):Promise<any>{
            let web = new Web(endPoint);
            const xml=this.returnCamlQueryToGetDocSetOrFolder(folerName);
            const q: CamlQuery = {
                ViewXml: xml,
            };

            return web.lists.getByTitle(listTitle).getItemsByCAMLQuery(q).then((docDetails)=>{
                console.log("Folder details",docDetails);
                return docDetails;
            });

        }

        public static returnCamlQueryToGetDocSetOrFolder(folerName){
            const viewXml =
            "<View>" +
            "<Query>" +
                "<Where>" +
                    "<And>" +
                        "<Eq>" +
                            "<FieldRef Name=\"FSObjType\"/>" +
                            "<Value Type=\"Integer\">1</Value>" +
                        "</Eq>" +
                        "<Eq>" +
                            "<FieldRef Name=\"FileLeafRef\" />" +
                            "<Value Type=\"Text\">" + folerName + "</Value>" +
                        "</Eq>" +
                    "</And>" +
                "</Where>" +
            "</Query>" +
            "<RowLimit>1</RowLimit>" +
            "</View>";
            return viewXml;
        }

     

     

Resources