2

I came across this yesterday, and im curious if its intentional or its a bug.

So, I have an empty doc lib Docs. I wanted to check if a folder (lets say 2017) exists in the doc lib. So i called the API endpoint _api/web/getFolderByServerRelativeUrl('Docs/2017')

My assumption was, if its returning a row in the response the folder exists. I dont get anything, no suprise, the lib is empty. Why 500 Internal Server Error i dont know, but whatever, it works for me, i can handle it in code... simple API call

Then i tought if it exists, it will return a lot of junk info, why not select just for the Name attribute. And this is the strange part. API calll with $select now gives me back data Docs is still empty (folder 2017 never existed in the lib), how is it returning data then?

Also, it is not just the Name, you can enter any combination of existing or non existing field names, it gives back 200 OK

Any guess why is this happenning? Is it just a bug, or this is intentional behavior somehow, and if so why is this usefull?

1

I was exactly on the same boat you were, trying to verify whether a folder existed or not, so just like you, I was calling:

$"_api/web/GetFolderByServerRelativeUrl('{relativePath}')"

And, just as you described, I was having a 200 OK status if the folder existed or a 500 (with a JSON body with an error that gave no explanation of what really happened) if not.

According to my tests, it turns out the proper way to do it is to call:

$"_api/web/GetFolderByServerRelativeUrl('{relativePath}')/Exists"

Notice I'm now requesting the Exists property of the folder. This way, no matter if the folder exists or not, I receive a JSON object with a shape like this:

{ d: { Exists : [boolean] } }

In which the Exist property actually has the expected value, true or false, depending on the existance of the path supplied.

This might explain the behavior you saw when asking for properties of an non existing folder, because to assess if a folder exists, you actually need to access its properties.

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.