This project has moved and is read-only. For the latest updates, please go here.

Extending $().SPServices.SPListNameFromUrl

Mar 16, 2012 at 3:19 PM
Edited Mar 16, 2012 at 3:21 PM


I've been experimenting with extending this utility function to take a URL as an argument. With some minor tweaks we can adapt the existing function to retrive the GUID of any list or library.

I've been trialling the following code, which is adapted from the source of SPServices and the results are very encouraging.


 * Returns GUID of parent list/library, type and webURL of any SP location.
 * @param String srcPage
 * @returns Object{String guid, Boolean isList}
function getSourceList(srcPage) {
 var srcList = "Not found";
 var isList = new Boolean();
 // Parse out the list's root URL from the current location
 var thisPageBaseName = srcPage.substring(0, srcPage.indexOf(".aspx"));
 var listPath = unescape(thisPageBaseName.substring(16, thisPageBaseName.lastIndexOf(SLASH) + 1)).toUpperCase();
 var srcWeb = listPath.substring(0, listPath.lastIndexOf(SLASH));  
 // This is a document library, append "FORMS/" to the list path (required for matching)
 if ( listPath.indexOf("/Lists/") == -1 ) {
  srcWeb = srcWeb.substring(0, srcWeb.lastIndexOf(SLASH) + 1);
  listPath += "FORMS/"
 // This is a list, strip out "Lists/" from the web url
 else {
  isList = true;
  srcWeb = srcWeb.substring(0, srcWeb.lastIndexOf("/Lists/") - 5);

 // Call GetListCollection and loop through the results to find a match with the list's URL to get the list's GUID (ID)
  operation: "GetListCollection",
  webURL: srcWeb,
  async: false,
  completefunc: function(xData, Status) {
   $(xData.responseXML).find("List").each(function() {
    var defaultViewUrl = $(this).attr("DefaultViewUrl");
    var listCollList = defaultViewUrl.substring(0, defaultViewUrl.lastIndexOf(SLASH) + 1).toUpperCase();
    if(listPath == listCollList) {
     srcList = $(this).attr("ID");
     return false;
 return {guid: srcList, isList: isList};


I'm using the above in a cross-site collection help/feedback form that captures the origin of the query. This allows us to programatically identify the publishing contact for any given page and automatically assign these tasks directly to the content owners best equipped handle them.

I was hoping to get you/the community's thoughts and see if anyone else is already doing something similar.

Mar 19, 2012 at 4:09 PM

I'm already saving the current list in a variable called thisList in SPService so that I don't try to figure it out more than once per page load. Maybe a good alternative would be to allow you to set this in the defaults? That would mean that you could set it yourself if you chose to. I could also add the capability to pass a URL into $().SPServices.SPListNameFromUrl, as you are above.

Make sense?


Mar 20, 2012 at 8:13 AM

Yup, makes sense. I did see the thisList variable and that's what got me thinking in the first place. Do you think the lines that make it work for Libraries are complete/ worthwhile?

Mar 22, 2012 at 6:57 PM
Edited Mar 22, 2012 at 6:57 PM

Just to make sure I wasn't crazy, I tested $().SPServices.SPListNameFromUrl() from a Document library form, and it returns the correct GUID. So I think my function already works correctly. I'll add a new task for the next release to allow you to pass in the listName (or GUID) in as an option.


Mar 22, 2012 at 6:58 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Mar 23, 2012 at 12:21 AM

You're completely right, it works in the context of a document library form, because both the doc lib forms and the default view reside under the FORMS/ path. Give it a go in the context of a web part page however, and it doesn't return a value. As web part pages are not stored in that folder, you need to strip out the web part page name and append append FORMS/ to get a match when you loop through the DefaultViewURL attributes.