[ROYALINE’s] SharePoint Dynamics

SharePoint Development & Techniques

How to use the Content Editor Web Part to build a dynamic link based on your current URL

Posted by Freelance-Puma on August 11, 2011


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:

  1. I have a custom list at the root site collection that is readable by everyone.
  2. 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.
  3. 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.
  4. 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:

  http://www.royaline.com/Articles/SitePages/How-To-Build-Dynamic-Link-From-Current-Page-Using-Javascript.aspx

The first thing you have to do is get the “protocol” of the URL. The protocol is “http”. There’s also “ftp”, “mms”, “telnet”, etc., but we’re only concerned with “http” since that’s what’s in our browser URL. To get the protocol, use this javascript:

  var wsURL = window.location.protocol + "//";   

This will show the ‘wsURL’ as “http://”. We’ll continue building the ‘wsURL’ variable until it becomes our full URL to the ‘LISTS.ASMX’ web service for the site we’re on. Next, we need to get the ‘host’, or the server this web site is on. To do that, add the following:

  var wsURL += window.location.host;   

Our ‘wsURL’ variable will now be “http://www.royaline.com”. Now, we’ll have to write code to ‘split’ each element between the “/”. To do this, create the following function:

<script type="text/javascript" language="javascript">// <![CDATA[
	function displayCustomURL() {
		var path = window.location.pathname.split("/");
		path.pop();
		var x;
		for (x in path) {
			if (path[x] == "Pages" || path[x] == "Lists") {
				break;
			} else {
				wsURL += path[x] + "/";
			}
		}
		alert(wsURL += wsWebService);
	}
// ]]></script>

As you can see, the ‘wsURL’ variable is created but excludes the web page. The last character in the ‘wsURL’ variable is “/”, which is exactly what we wanted to achieve. The next step is to create a new variable and append it to the end of the ‘wsURL’ variable. Since the LISTS.ASMX web service is always located at “/_vti_bin/lists.asmx”, no matter what SharePoint site you are on, let’s create a new variable:

  var wsWebService = "_vti_bin/lists.asmx";

Finally, we’ll append the ‘wsWebService’ variable to our ‘wsURL’ to create our dynamic link:

  wsURL += wsWebService;

Our completed code looks like this:

<script type="text/javascript" language="javascript">// <![CDATA[
	var wsWebService = "_vti_bin/lists.asmx";
	var wsURL = window.location.protocol + "//";
	wsURL += window.location.host;

	function displayCustomURL() {
		var path = window.location.pathname.split("/");
		path.pop();
		var x;
		for (x in path) {
			if (path[x] == "Pages" || path[x] == "Lists") {
				break;
			} else {
				wsURL += path[x] + "/";
			}
		}
		alert(wsURL += wsWebService);
	}
// ]]></script>
<a href="javascript:displayCustomURL();">Show Custom URL</a>

Remember, this can be used for many different reasons and has come to become a very valuable asset to my SharePoint toolbox.

END

One Response to “How to use the Content Editor Web Part to build a dynamic link based on your current URL”

  1. […] Use the current site’s URL to connect to the ‘Lists.asmx’ web service   (see: How to use the Content Editor Web Part to build a dynamic link based on your current URL) […]

Leave a comment