Sharepoint 2013 REST API: Testing for the Existence of a Document In a Document Library   Leave a comment

This is just a short program note concerning the REST API for Sharepoint 2013.  If you want to test for the existence of a Document within a Document Library in Sharepoint you might be tempted to use this uri fragment:

web/getfilebyserverrelativeurl(‘{document library name and folder path }\{document name}’)”

and indeed if the document exists this call will return an HTTP Status 200 code and the standard ATOM feed with one entry node with the meta data for the document.

<!–?xml version=”1.0″ encoding=”utf-8″?>
xml:base=”
http://ddhdemoauthor1.aicdevspweb1.wrbts.ads.wrberkley.com/_api/” xmlns=”http://www.w3.org/2005/Atom” xmlns:d=”http://schemas.microsoft.com/ado/2007/08/dataservices” xmlns:m=”http://schemas.microsoft.com/ado/2007/08/dataservices/metadata” xmlns:georss=”http://www.georss.org/georss” xmlns:gml=”http://www.opengis.net/gml”>
<id>5a670571-9fac-4a07-8c06-42a6787d5fd1</id>
<title/>
2013-12-04T21:09:54Z
<entry>
     GetFileByServerRelativeUrl(‘/DLR%20Document%20Library/_f37-06_tc_big.svg’)”/>
[many other links]

    <content>

       <m:properties>
<d:Name>_f37-06_tc_big.svg</d:Name>
<d:ServerRelativeUrl>/DLR Document Library/_f37-06_tc_big.svg</d:ServerRelativeUrl>

                [many other properties]
       </m:properties>
</content>
</entry>

</feed>

Now here is todays puzzle.  If the file does not exist, what does Sharepoint return for this call:

  1. Http Status Code 200 and an Atom Feed with no entry nodes;
  2. HTTP Status Code 404 (Not Found) and no Atom Feed; or
  3. HTTP Status Code 500 (Internal Server Error)
If you selected answer 1 you have been programming Sharepoint 2013 awhile but are still quite naïve.

If you selected answer 2 you are a RESTafarian and have no business programming Microsoft products.

If you selected answer 3 you have been programming this interface for way to long and should find another line of work.

The correct answer is 3!  Don’t ask me why.

To be consistent we would like to query Sharepoint 2013 and get an Atom Feed if the file exists or not and expect that the feed will have zero entry nodes when the Document does not exist.  How to do this?  Query the Document Library and folder path using a completely different uri fragment  for all files and use an OData filter to limit the returned Atom Feed to zero entry nodes with the document does not exist. Here is an example:

web/GetFolderByServerRelativeUrl(‘{document library name and folder path }’)/Files?$filter=Name eq ‘{document name}’

Now you will get an HTTP Status code of 200 whether the document exists or not.  The Atom Feed will be returned in all cases but  will differ depending if the Document exists (one entry node ) or if the Document does not exist (no entry nodes).

Strange but true.  Don’t let this error happen to you.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: