4

I want to check if list(by Display Name) exist in current web.

In pnp developer guide is no information on how to work with lists?

I need this for validate web property and i want to use pnp-core-library to do this:

private asyncTextBoxValidationMethod(value: string): Promise<string> {
if (value !== undefined && value.length > 3) {
  var url = this.context.pageContext.web.absoluteUrl + `/_api/web/lists?$filter=Hidden eq false`;

  return this.fetchLists(url).then((response) => {
      var lists: ISPList[] = response.value;
      var foundList: boolean = false;
      lists.forEach((list: ISPList) => {
          if (value === list.Title) {
              foundList = true;
          }
      });

      if (!foundList) {
        // resolve promise with error message to display..
        return Promise.resolve("Value entered did not match a list in this site!");
      }

      // otherwise resolve promise with an empty string..
      return Promise.resolve("");
    });
  }

}

How to do this?

3

You can use the lists.ensure method to check if the list exists or not.

Add the below import statement:

import pnp, { List, ListEnsureResult } from "sp-pnp-js";

In your method, you can use it as below:

// Use lists.ensure to always have the list available
pnp.sp.web.lists.ensure("Custom List").then((ler: ListEnsureResult) => {
    if (ler.created) {
        console.log("list exists");
        //do some awesome stuff with the list
    }
    else{
        console.log("list doesnt exists");
    }
}

Reference - PnP Core debugging

Using PnP with SPFx

  • 1
    If list does not exist, then ensure function creates it. Which is sometime not ideal. Correct me if I am wrong. – Asad Refai Nov 21 '18 at 15:02
  • Yes, it creates a new list. Which is not always ideal, but it has been modelled on the web.ensureUser method which checks if user exists , if not adds it to user info list and then gives you the user object. Having said that, you can use lists.ensure to also check if a doc lib exists or not like pnp.sp.web.lists.ensure("DocLib","",101).then(... etc. It can be use for lists of any templates as well :) Let me know if need more details :) – Gautam Sheth Nov 21 '18 at 15:44
  • Many thanks for this information. You are doing a very good contribution on SP.SE, keep it up. :) – Asad Refai Nov 22 '18 at 4:39
3

I have tested the PnP List existence check against SP Online. You may try the same with SPFx without SP.SOD

<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js">
</script>
<script type="text/javascript" 
src="https://cdnjs.cloudflare.com/ajax/libs/sp-pnp-js/2.0.7/pnp.js">
</script>
<script language="javascript" type="text/javascript">

$( document ).ready(function() {

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
$pnp.sp.web.lists.filter("Title eq Employee34'").get().then(function(result) 
{
   if (result.length > 0) {
      alert("List Exists");
   } else {
      alert("List Does Not Exist");
   }
});
});
});

</script>

On Testing :

enter image description here

enter image description here

  • This is a good approach. – Asad Refai Nov 21 '18 at 15:10

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.