Get List by its URL

Mar 24, 2013 at 2:33 PM
Edited Mar 24, 2013 at 2:35 PM
Working in a bilingual environment brings his amount of problem. One of those is to be able to query a list based on its URL instead of its name. For example, if I query a list like this :
$().SPServices({
        operation: "GetListItems",
    async: true,
    cache: false,
    listName: "Shared Documents",
    CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
    completefunc: function (xData, Status) {
        $(xData.responseXML).SPFilterNode("z:row").each(function() {
            alert($(this).attr("ows_Title"));
        });
    }
});
I will only get my results when MUI is in English. In French, that will not work because the list name is "Documents partagés". So I need a way to query GetListItems with a List Guid. That guid is autogenerated when the site is created, so I can't rely on a fixed guid in my JS code. The only thing that I know that will not change is its URL.

By querying all the SPListCollection of a precise SPWeb, I can make it work, but I was wondering if I could make it work with a better approach.

Here is what I built :
function GetListIdByUrl(listRootFolderUrl)
{
    var listId = EMPTY_GUID;

    if(!listRootFolderUrl.startsWith("/"))
    {
        listRootFolderUrl = "/" + listRootFolderUrl;
    }
        
    if(!listRootFolderUrl.endsWith("/"))
    {
        listRootFolderUrl = listRootFolderUrl + "/";
    }
            
    $().SPServices({
        operation: "GetListCollection",
        async: false,
        cached: true,
        completefunc: function (xData, Status) {
            $(xData.responseXML).SPFilterNode("List").each(function() {
                var webUrl = $(this).attr("WebFullUrl");
                var defaultViewUrl = $(this).attr("DefaultViewUrl");
                var listUrl = defaultViewUrl.replace(webUrl, "");
                    
                if(listUrl != "")
                {
                    if(listUrl.startsWith(listRootFolderUrl))
                    {
                        listId = $(this).attr("ID");
                    }
                }
            });
        }
    });
            
    return listId;
}
Do you see any concern on using GetListCollection ? How can I improve that piece of code ?

Thanks !
Coordinator
Mar 25, 2013 at 2:51 PM
Sebastian:

Matt Bramer (@ionline247) mentioned something on Twitter that I think was a great suggestion. Add something to the page that indicates the language. It could be a DVWP or really anything that you could select on in your script to know which language you're dealing with.

M.