{"id":8725,"date":"2016-01-28T21:32:04","date_gmt":"2016-01-28T21:32:04","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/fixing-the-file-not-found-error-opening-a-sharepoint-site-with-spaces-in-the-url-using-site-openweb\/"},"modified":"2016-07-28T10:57:44","modified_gmt":"2016-07-28T10:57:44","slug":"fixing-the-file-not-found-error-opening-a-sharepoint-site-with-spaces-in-the-url-using-site-openweb","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/fixing-the-file-not-found-error-opening-a-sharepoint-site-with-spaces-in-the-url-using-site-openweb\/","title":{"rendered":"Fixing the File Not Found Error Opening a SharePoint Site with Spaces in the URL using Site.OpenWeb"},"content":{"rendered":"<p>I was investigating a bug in the SharePoint app I&#8217;m working on where a function was failing on sites with URLs containing spaces.&#160; Although I&#8217;m not a fan of creating sites with spaces in the URL, SharePoint lets you do it so it&#8217;s going to happen.&#160; At first, I thought the issue would be easy to track down and that the problem had something to do with how we were parsing a URL somewhere.&#160; It took a bit of time, but I eventually found out that SharePoint is a bit quirky about what it allows in URLs.&#160; Ultimately, the issue boiled down to this:<\/p>\n<p><strong>The <\/strong><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/microsoft.sharepoint.client.site.aspx\"><strong>Site<\/strong><\/a><strong>.<\/strong><a href=\"https:\/\/msdn.microsoft.com\/EN-US\/library\/office\/microsoft.sharepoint.client.site.openweb.aspx\"><strong>OpenWeb<\/strong><\/a><strong> method does not support URL encoded spaces.&#160; <\/strong><\/p>\n<p>So if you have a server-relative URL like <strong>\/sites\/Some Site With Spaces\/ <\/strong>but you pass the URL encoded version \/sites\/Some%20Site%20With%20Spaces\/ to the <a href=\"https:\/\/msdn.microsoft.com\/EN-US\/library\/office\/microsoft.sharepoint.client.site.openweb.aspx\"><strong>OpenWeb<\/strong><\/a> method, then you are going to end up with a File Not Found error.<\/p>\n<p>One of the reasons you may be getting this error is if you are using the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/System.Uri(v=vs.110).aspx\">Uri<\/a>.<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.uri.absolutepath(v=vs.110).aspx\">AbsolutePath<\/a> property to retrieve the server-relative URL of your web.&#160; The <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.uri.absolutepath(v=vs.110).aspx\">AbsolutePath<\/a> property automatically URL encodes spaces.&#160; To avoid this issue, you can just use the Uri.<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.uri.localpath(v=vs.110).aspx\">LocalPath<\/a> property, which returns spaces in all of their unencoded glory.<\/p>\n<p>If you are not using a <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/System.Uri(v=vs.110).aspx\">Uri<\/a> then you can just replace all of the occurrences of %20 with a space in the string and you&#8217;ll be good to go.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was investigating a bug in the SharePoint app I&#8217;m working on where a function was failing on sites with URLs containing spaces.&#160; Although I&#8217;m not a fan of creating sites with spaces in the URL, SharePoint lets you do it so it&#8217;s going to happen.&#160; At first, I thought the issue would be easy&#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-8725","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\/8725","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=8725"}],"version-history":[{"count":3,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/8725\/revisions"}],"predecessor-version":[{"id":42531,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/8725\/revisions\/42531"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=8725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=8725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=8725"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=8725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}