Sharepoint - SPFx - PnP-JS-Core - Check if list exist in current web
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
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 :