{"id":8385,"date":"2015-10-20T15:18:50","date_gmt":"2015-10-20T15:18:50","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/getting-the-absolute-url-of-a-file-in-csom\/"},"modified":"2016-07-28T10:57:06","modified_gmt":"2016-07-28T10:57:06","slug":"getting-the-absolute-url-of-a-file-in-csom","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/getting-the-absolute-url-of-a-file-in-csom\/","title":{"rendered":"Getting the Absolute URL of a File in CSOM"},"content":{"rendered":"<p>Sometimes you need the absolute URL of a file in SharePoint, and if you happen to just have the absolute URL then you are in luck.&#160; I suppose, however, that if you already have the absolute URL then you wouldn&#8217;t be here reading about how to to get it.&#160; It&#8217;s more likely that you have a <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.client.file.aspx\">File<\/a> object reference and have noticed that SharePoint provides you with the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.client.file.serverrelativeurl.aspx\">ServerRelativeUrl<\/a> property, conveniently leaving you with about half of what you need to get the job done.&#160; <\/p>\n<p>There are two ways to get the absolute URL. The first is using a hidden list item field called FileRef.&#160; This approach works with any file as long as it is in a document library. Why?&#160; Because to get the FileRef value you have to go through the file&#8217;s backing <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.client.listItem.aspx\">ListItem<\/a>, and system files that reside outside of document libraries do not have backing list items.&#160; So the following code will fail if attempted on a system file:<\/p>\n<p><strong>NOTE: this code works most of the time<\/strong><\/p>\n<pre>var file = context.Web.GetFileByServerRelativeUrl(     \"\/sites\/Site\/Library\/MyFile.txt\");\ncontext.Load(file, f =&gt; f.ListItemAllFields[\"FileRef\"]);\ncontext.ExecuteQuery();\nvar absoluteUrl = file.ListItemAllFields[\"FileRef\"];<\/pre>\n<p>But what if you want a way that works for system files as well as for files that are in a document library?&#160; As much as I like to give SharePoint grief for not giving you everything you need in a nicely packaged format, all of the information is actually available if you know how to string it together (pun definitely intended).&#160; When you have a server relative URL to a file, it&#8217;s fairly easy to build the absolute URL to a file using the following code:<\/p>\n<p><strong>NOTE: this code works ALL of the time and may avoid a round-trip to SharePoint depending on your situation.<\/strong><\/p>\n<pre>var serverRelativeUrl = \"\/sites\/Site\/Library\/MyFile.txt\";\nvar absoluteUrl = new Uri(context.Url)  .GetLeftPart(UriPartial.Authority) + serverRelativeUrl;<\/pre>\n<p>You just grab the missing authority information directly from the context&#8217;s URL property and slap the server relative URL on the end of it creating an absolute URL.&#160; Another nice aspect about this approach is if you already have the server relative URL, then this saves you an entire round-trip to SharePoint to acquire the absolute URL.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes you need the absolute URL of a file in SharePoint, and if you happen to just have the absolute URL then you are in luck.&#160; I suppose, however, that if you already have the absolute URL then you wouldn&#8217;t be here reading about how to to get it.&#160; It&#8217;s more likely that you have&#8230;&hellip;<\/p>\n","protected":false},"author":46738,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-8385","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/8385","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/46738"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=8385"}],"version-history":[{"count":4,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/8385\/revisions"}],"predecessor-version":[{"id":42515,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/8385\/revisions\/42515"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=8385"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=8385"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=8385"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=8385"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}