How To: Test image exists

%3CLINGO-SUB%20id%3D%22lingo-sub-1427417%22%20slang%3D%22en-US%22%3EHow%20To%3A%20Test%20image%20exists%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1427417%22%20slang%3D%22en-US%22%3E%3CP%3EI%20need%20to%20test%20to%20see%20if%20an%20image%20exists%20in%20a%20library.%20I%20am%20basing%20my%20code%20on%20the%20PNP%20JS%20documentation%20found%20here%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fpnp.github.io%2Fpnpjs%2Fsp%2Ffiles%2F%22%20target%3D%22_blank%22%20rel%3D%22nofollow%20noopener%20noreferrer%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fpnp.github.io%2Fpnpjs%2Fsp%2Ffiles%2F%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20code%20works%3C%2FP%3E%3CPRE%3E%3CSPAN%20class%3D%22kr%22%3Econst%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22nx%22%3Efile%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22o%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22nx%22%3Esp%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22nx%22%3Eweb%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22nx%22%3EgetFileByUrl%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22nx%22%3Eurl%3C%2FSPAN%3E%3CSPAN%20class%3D%22p%22%3E)%3B%3C%2FSPAN%3E%3C%2FPRE%3E%3CP%3Ebut%20it%20always%20returns%20an%20object.%20My%20challenge%20had%20been%2C%20how%20to%20tell%20if%20the%20object%20is%20indicating%20the%20image%20exists%20or%20doesn't.%20The%20only%20thing%20that%20has%20worked%20for%20me%20is%20to%20call%20the%20getItem()%20then%20fieldValuesAsText().%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-javascript%22%3E%3CCODE%3Esp.web.getFileByUrl(%22%22).getItem().then(function(val)%7B%0A%20%20%20%20val.fieldValuesAsText()%0A%7D)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3CDIV%3EWhen%20I%20console.log()%20the%20output%2C%20I%20get%20yet%20another%20object%20that%20allows%20me%20to%20see%20that%20a%20non-exist%20image%20is%20returning%20a%20404%20error%20but%20the%20properties%20are%20%5B%5B%5D%5D%20and%20I%20can't%20address%20them%20individually%20in%20code.%3C%2FDIV%3E%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1427417%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EPnP%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1429255%22%20slang%3D%22en-US%22%3ERe%3A%20How%20To%3A%20Test%20image%20exists%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1429255%22%20slang%3D%22en-US%22%3EHi%2C%20If%20you%20are%20aware%20of%20the%20extension%20of%20the%20file%2C%20try%20to%20use%20the%20getText%20or%20other%20methods%20on%20the%20file%20object.%20You%20can%20also%20check%20the%20file%20object%20to%20see%20whether%20the%20file%20exists%20or%20not.%3C%2FLINGO-BODY%3E
Highlighted
Contributor

I need to test to see if an image exists in a library. I am basing my code on the PNP JS documentation found here: https://pnp.github.io/pnpjs/sp/files/

 

This code works

const file = sp.web.getFileByUrl(url);

but it always returns an object. My challenge had been, how to tell if the object is indicating the image exists or doesn't. The only thing that has worked for me is to call the getItem() then fieldValuesAsText().

 

sp.web.getFileByUrl("").getItem().then(function(val){
    val.fieldValuesAsText()
});
 
When I console.log() the output, I get yet another object that allows me to see that a non-exist image is returning a 404 error but the properties are [[]] and I can't address them individually in code.
 
1 Reply
Highlighted
Hi, If you are aware of the extension of the file, try to use the getText or other methods on the file object. You can also check the file object to see whether the file exists or not.