{"id":8127,"date":"2015-07-16T13:33:17","date_gmt":"2015-07-16T13:33:17","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/an-odd-behavior-with-the-serverrelativeurl-property-in-csom\/"},"modified":"2016-07-28T10:56:46","modified_gmt":"2016-07-28T10:56:46","slug":"an-odd-behavior-with-the-serverrelativeurl-property-in-csom","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/an-odd-behavior-with-the-serverrelativeurl-property-in-csom\/","title":{"rendered":"An Odd Behavior with the ServerRelativeUrl Property in CSOM"},"content":{"rendered":"<p>I was attempting to build a query using the <a href=\"https:\/\/msdn.microsoft.com\/library\/office\/microsoft.sharepoint.client.clientcontext(v=office.15).aspx\">ClientContext<\/a>.<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/ee544076.aspx\">LoadQuery<\/a> method because it has some performance benefits that make it a useful approach for querying SharePoint. I was attempting to query a specific list using the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/microsoft.sharepoint.client.folder.serverrelativeurl.aspx\">ServerRelativeUrl<\/a> property on the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/microsoft.sharepoint.client.list.rootfolder.aspx\">RootFolder<\/a> on the <a href=\"https:\/\/msdn.microsoft.com\/EN-US\/library\/office\/microsoft.sharepoint.client.list.aspx\">List<\/a>, but I only had the site-relative URL of the list. So I was combining that partial URL with the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/microsoft.sharepoint.client.site.serverrelativeurl.aspx\">ServerRelativeUrl<\/a> property from the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/microsoft.sharepoint.client.clientcontext.site.aspx\">Site<\/a> property on the <a href=\"https:\/\/msdn.microsoft.com\/library\/office\/microsoft.sharepoint.client.clientcontext(v=office.15).aspx\">ClientContext<\/a> to construct the full server relative URL. My queries returned no items.<\/p>\n<p>The server-relative URL of the list&#8217;s <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/microsoft.sharepoint.client.list.rootfolder.aspx\">RootFolder<\/a> was coming back as:     <br \/><b>\/sites\/dev\/lists\/MyList\/<\/b><\/p>\n<p>The site-relative URL that I had for the list was:    <br \/><b>\/lists\/MyList\/<\/b><\/p>\n<p>And the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/microsoft.sharepoint.client.site.serverrelativeurl.aspx\">ServerRelativeUrl<\/a> coming back from the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/microsoft.sharepoint.client.clientcontext.site.aspx\">Site<\/a> on the <a href=\"https:\/\/msdn.microsoft.com\/library\/office\/microsoft.sharepoint.client.clientcontext(v=office.15).aspx\">ClientContext<\/a> was:     <br \/><b>\/Sites\/Dev\/<\/b><\/p>\n<p>This may better point out the issue:    <br \/><b>\/sites\/dev\/lists\/MyList\/<\/b>     <br \/><b>\/Sites\/Dev\/lists\/MyList\/<\/b><\/p>\n<p>So the casing of the URL coming back from the Site object was incorrect, and unfortunately there is no way to normalize casing when using the <a href=\"https:\/\/msdn.microsoft.com\/library\/office\/microsoft.sharepoint.client.clientcontext(v=office.15).aspx\">ClientContext<\/a>.<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/ee544076.aspx\">LoadQuery<\/a> method because it doesn&#8217;t support the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/system.string.toupper(v=vs.110).aspx\">ToUpper()<\/a> or <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/e78f86at(v=vs.110).aspx\">ToLower()<\/a> methods in queries. But my question is, why was the URL casing different on the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/microsoft.sharepoint.client.site.serverrelativeurl.aspx\">ServerRelativeUrl<\/a> properties on the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/microsoft.sharepoint.client.clientcontext.site.aspx\">Site<\/a> and the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/microsoft.sharepoint.client.list.rootfolder.aspx\">RootFolder<\/a> instances? You would think that SharePoint would be nice enough to provide the values using the same casing. <\/p>\n<p>Well, it turns out the issue is simple &#8211; the URL that is populated into the Site object matches the casing used to construct the <a href=\"https:\/\/msdn.microsoft.com\/library\/office\/microsoft.sharepoint.client.clientcontext(v=office.15).aspx\">ClientContext<\/a> instance. My code looked like this:<\/p>\n<p><b>var context = new ClientContext(&#8220;https:\/\/someserver\/Sites\/Dev\/&#8221;);<\/b><\/p>\n<p>Changing the URL in the <a href=\"https:\/\/msdn.microsoft.com\/library\/office\/microsoft.sharepoint.client.clientcontext(v=office.15).aspx\">ClientContext<\/a> constructor fixed my issue. Oddly enough, if you attempt to access the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/microsoft.sharepoint.client.site.serverrelativeurl.aspx\">ServerRelativeUrl<\/a> property on the <a href=\"https:\/\/msdn.microsoft.com\/library\/office\/microsoft.sharepoint.client.clientcontext(v=office.15).aspx\">ClientContext<\/a>&#8216;s <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/microsoft.sharepoint.client.clientcontext.site.aspx\">Site<\/a> property without loading it, then you will get an error about how the property has not yet been loaded. So you have to make a round trip to the server just so it can ultimately use the URL that you already provided in client code. Thanks SharePoint.<\/p>\n<p>Unfortunately, this is not the only location where this shows up. This is also an issue when you call the <a href=\"https:\/\/msdn.microsoft.com\/EN-US\/library\/ee537040\">Web<\/a>.<a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.client.web.getfolderbyserverrelativeurl.aspx\">GetFolderByServerRelativeUrl<\/a> method and then request the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.client.folder.serverrelativeurl.aspx\">ServerRelativeUrl<\/a> of the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.client.folder.aspx\">folder<\/a>. You&#8217;ll actually get back exactly what you passed into the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.client.web.getfolderbyserverrelativeurl.aspx\">GetFolderByServerRelativeUrl<\/a> method. And in this scenario it&#8217;s not just casing, if you pass in a URL to a file in the folder, the folder will come back successfully but the <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/microsoft.sharepoint.client.folder.serverrelativeurl.aspx\">ServerRelativeUrl<\/a> property will be initialized with the URL to the file and not just that of the folder.<\/p>\n<p>I&#8217;m sure there are other places where this crops up as well, so be forewarned.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was attempting to build a query using the ClientContext.LoadQuery method because it has some performance benefits that make it a useful approach for querying SharePoint. I was attempting to query a specific list using the ServerRelativeUrl property on the RootFolder on the List, but I only had the site-relative URL of the list. So&#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-8127","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\/8127","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=8127"}],"version-history":[{"count":3,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/8127\/revisions"}],"predecessor-version":[{"id":26061,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/8127\/revisions\/26061"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=8127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=8127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=8127"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=8127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}