While working on a project with some existing code I noticed the developer did write large portions of code to get from an URL to a SPList. He probably didn’t know some of the hidden gems in SharePoint.
Get the full URL
Sometimes you need the full URL and only have the relative one.
For example, when opening a new SPSite
or when writing code in a SPNavigationProvider
.
For this you could use the SPUtility
class:
For example:
There is one catch:
Check the type of URL
The former example is nice, but you would still need to write code to check if the input already contains the full URL. Nope!
For this, two gems are found in the SPUrlUtility
class.
These methods do exactly what their names imply: check if the URL is relative or full. So, for example:
Great! Now we can combine the two:
Now webUrl
will always be the full URL.
URL concatenation
Ever did web.ServerRelativeUrl + "/something"
and found out it did work nicely except it start doing something really weird on your root web?
On the rootweb
the relative URL is /
, and this results in the URL //something
which on its own turn gets translated to http://something
,
and that URL does not exist (most of the time).
When working with file system locations, you should always use Path.Combine()
instead of concatenating paths yourself.
But there is no Uri.Combine().
You could write an extension method. But the SharePoint team made it easier.
This method does the same thing like Path.Combine()
. For example:
That is the final (hidden) gem for today.