{"id":8647,"date":"2016-01-06T12:25:59","date_gmt":"2016-01-06T12:25:59","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/sharepoint-csom-fixing-the-value-does-not-fall-within-the-expected-range-error-calling-getfolderbyserverrelativeurl\/"},"modified":"2016-07-28T10:57:30","modified_gmt":"2016-07-28T10:57:30","slug":"sharepoint-csom-fixing-the-value-does-not-fall-within-the-expected-range-error-calling-getfolderbyserverrelativeurl","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/sharepoint-csom-fixing-the-value-does-not-fall-within-the-expected-range-error-calling-getfolderbyserverrelativeurl\/","title":{"rendered":"SharePoint CSOM: Fixing the &ldquo;Value does not fall within the expected range&rdquo; Error Calling GetFolderByServerRelativeUrl"},"content":{"rendered":"<p>There are times when you just need to get a reference to a folder in the master page gallery of a site collection using CSOM, and that&#8217;s where I found myself the other day.&#160; I had a SharePoint context, so I attempted to get my folder using:<\/p>\n<p> <code>   <\/code><\/p>\n<p><code>var folder = clientContext.<strong>Web<\/strong>.GetFolderByServerRelativeUrl(      <br \/>&#160;&#160;&#160;&#160; \"\/_catalogs\/masterpage\/SomeFolder\")<\/code><\/p>\n<p><code> <\/code>  <\/p>\n<p>Unfortunately, instead of getting back a folder after executing my context, I got back an error<\/p>\n<p><strong>Value does not fall within the expected range.<\/strong><\/p>\n<p>Awesome.&#160; I happened to remembered that my SharePoint context was pointing to a subsite and not the root web of the site collection, and after prodding around a bit I confirmed that when you call <a href=\"https:\/\/msdn.microsoft.com\/EN-US\/library\/office\/microsoft.sharepoint.client.web.aspx\">Web<\/a>.<a href=\"https:\/\/msdn.microsoft.com\/EN-US\/library\/office\/microsoft.sharepoint.client.web.getfilebyserverrelativeurl.aspx\">GetFolderByServerRelativeUrl<\/a>, the URL you pass to the method must be a child of the URL in the context.&#160; In other words, you can&#8217;t get a folder reference that exists in a parent site, you have to get a folder that is a child of the <a href=\"https:\/\/msdn.microsoft.com\/EN-US\/library\/office\/microsoft.sharepoint.client.web.aspx\">Web<\/a> instance.&#160; Since I needed to get something in the root web of the site collection the fix was fairly easy:<\/p>\n<p> <code>   <\/code><\/p>\n<p><code>var folder = clientContext.<strong>Site.RootWeb<\/strong>.GetFolderByServerRelativeUrl(      <br \/>&#160;&#160;&#160;&#160; \"\/_catalogs\/masterpage\/SomeFolder\")<\/code><\/p>\n<p><code> <\/code>  <\/p>\n<p>You just need to get an appropriate <a href=\"https:\/\/msdn.microsoft.com\/EN-US\/library\/office\/microsoft.sharepoint.client.web.aspx\">Web<\/a> instance from which to make the call and then it should work fine.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are times when you just need to get a reference to a folder in the master page gallery of a site collection using CSOM, and that&#8217;s where I found myself the other day.&#160; I had a SharePoint context, so I attempted to get my folder using: var folder = clientContext.Web.GetFolderByServerRelativeUrl( &#160;&#160;&#160;&#160; &#8220;\/_catalogs\/masterpage\/SomeFolder&#8221;) Unfortunately, instead&#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-8647","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\/8647","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=8647"}],"version-history":[{"count":3,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/8647\/revisions"}],"predecessor-version":[{"id":42526,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/8647\/revisions\/42526"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=8647"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=8647"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=8647"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=8647"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}