2

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");
                }                   
            }
4

3 Answers 3

2
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

1
  • Thanks Robert, Nadeem and Dave. That was really helpful. I should have checked MSDN properly before posting it. Really sorry for the inconvenience. But anyway, thank you very much to all of you, for helping me out quickly.
    – Ven
    Nov 23, 2015 at 4:07
1

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.

0

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"));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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