Believe it or not, this is a very useful piece of code. I use it often in my content editor web parts to get the URL of the site I’m currently on.
To be more specific, I use this code to get the URL of the site, then modify it to dynamically create a URL that points to the LISTS.ASMX web service. Why is this useful? Consider the following:
- I have a custom list at the root site collection that is readable by everyone.
- The list contains a link to a custom web part page that is located on each site (subsite) in my site collection. The web part page is just a blank page that has a few CEWP’s (content editor web parts) in different zones.
- One of the CEWP’s only function is to get the URL of the site, then does a javascript ‘split’ to get each element of the URL. This is necessary to build a custom URL that points to the ‘LISTS.ASMX’ web service for the site I’m on.
- Once I have the new URL (that has been modified to point to the ‘lists.asmx’ web service), I can then query my sites schema (including list schema) for the site.
I find this to be VERY useful in that I can get a listing of all lists and libraries for a site and view the XML schema behind them, all bundled up in a single report. Instead of going thru each site and each site’s lists one at a time, I can view the XML schema from a single ‘web part page’.
So, to get started, I’m going to show you how to get the URL from a site. Let’s begin by assuming you are on a SharePoint site at this URL: (more…)