1

We have developed multilingual sites in SharePoint 2013 using resource files. Since the language settings of site in SP 2013 are now moved to browser language settings, we are facing serious issues.

There are a couple of custom lists/ libraries, that are picking up the list titles from resource files to change according to site locale. The list titles change as per the locale and text mentioned in resource files but the problem comes when we are referring those lists in custom web parts. I am using the following line of code in the web part to get the list:

SPList list = web.Lists[ResourceHelper.GetLocalized("Custom_List_Title", "Resources")];

when the site language is English (default language)- the list is retrieved and the web part works properly. When I change the site language from browser, the list title changes but the web part throws an exception - "List not found". However the list exists with changed title.

It is strange as it used to work fine in SP 2010

Can anyone help out here and shed some light as why SP 2013 fails to find list?

2

Do not use practice to retrieve list by its Title, because the Title can be changed by user through UI. For me the best solution is to use list URL (for example /Lists/Documents)

public static SPList GetListByUrl(this SPWeb web, string url)
{
     try
     {
          string relativeUrl = web.ServerRelativeUrl.TrimEnd('/');
          return web.GetList(string.Format("{0}/{1}", relativeUrl, url.TrimStart('/')));
     }
     catch (Exception)
     {
          return null;
     }
}
1
  • 1
    Good point. And it uses SPListCollection.GetListByName internally afaik. But couldn't you use web.GetList(SPUtility.ConcatUrls(web.ServerRelativeUrl, url)); May 29 '13 at 7:25
2

The correct way to get a List by its Title would be to do:

web.Lists.TryGetList(ResourceHelper.GetLocalized("Custom_List_Title", "Resources"));

By using the indexer Lists["title"] you risk getting an exception back if the list does not exists.

2
  • The OP asked the question in the context of server-side API, as far as I see. There is no SPListCollection.GetByTitle method in this API. The ListCollection.GetByTitle method is the "alias" in the client-side API for the SPListCollection.Item[string] indexer in the server-side API. They both look up the list by title and not by name. The code of this indexer is a bit confusing, as it actually invokes the internal method GetListByName. Actually, there is no such property (or field) defined at all as Name for the SPList / List, or have I missed something?
    – pholpar
    Mar 8 '18 at 11:29
  • I guess (this was a while ago) that I meant to write TryGetList. Regarding the name / title issue, the TryGetList method works on title. The Lists[] will also check on title, and on the EntityTypeName (I think) but will throw a nasty error if the list is not found Mar 8 '18 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.