Question

I have the following code in my application. It is throwing

FileNotFound exception at `web.GetList("Lists/TestList")

This list exists and I am able to get the list from web.Lists.TryGetList("TestList"). But I am getting exception when I am trying to get the list from the relative URL. Can anyone please help me?

            string siteUrl = "<siteURL>";

            using (SPSite site = new SPSite(siteUrl))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.GetList("/Lists/TestList");
                }                   
            }
Was it helpful?

Solution

SPList list = web.GetList("/Lists/TestList");

Should be

 SPList list = web.GetList(web.ServerRelativeUrl + "/Lists/TestList");

As it needs the server relative URL to your list

OTHER TIPS

You should use server relative Url of the list: Example: The server-relative URL to the root folder of a list, such as /sites/sitecollection/subsite/Lists/Announcements.

Are you sure that /Lists/TestList is the right (relative) URL?

Try the SPUrlUtility.CombineUrl method: https://msdn.microsoft.com/EN-US/library/office/microsoft.sharepoint.utilities.spurlutility.combineurl.aspx

Something like this:

List targetList = targetWeb.GetList(SPUrlUtility.CombineUrl(targetWeb.ServerRelativeUrl, "/_catalogs/yourList"));
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top