4

SPWeb.GetList() on an application page throws an exception ("Cannot Complete this Action") during postback. For some reason the first request to the application page seems to work.

on an application page I have the following code which works fine:

SPWeb contextWeb = SPContext.Current.Web;
SPList listByTitle = contextWeb.Lists["MyList"]; //works
SPList listByRelativeUrl = contextWeb.GetList("/sites/MySiteCollection/Lists/MyList"); //works

Then I call the exact same code from an event handler in the same application page

void Btn_Click(object sender, EventArgs e)

where

SPList listByTitle = contextWeb.Lists["MyList"]; //works
SPList listByRelativeUrl = contextWeb.GetList("/sites/MySiteCollection/Lists/MyList"); //throws

Background: I am creating lists via code during feature activation and would like to access them without displaynames / titles nor Guids after the deployment. Currently I use SPWeb.GetList() but I'd be interested in another approach as SPWeb.GetList() seems to perform rather poorly. Linq4SP is not an option due to the lack of support for anonymous users.

1
  • can you post some code ? Some log ? Did you find something in the ULS logs ? We can't guess what's wrong with only an small error message.
    – Steve B
    Feb 16, 2012 at 9:52

2 Answers 2

8

It's better if you use the GUID instead of the staticnames.

But I always use SPWeb.Lists[], only on rare occasions i use the GetList() method. (when getting a list from a full url for example).

this code works fine for me:

SPList oList = SPContext.Current.Web.GetList("/sites/sitename/Lists/ListName");

But please keep in mind, the documentation will tell you that the GetList() method accepts a parameter containing a SITE relative url to the list. This is wrong, you have to enter a SERVER relative url.

Wrong:

SPList oList = SPContext.Current.Web.GetList("/Lists/ListName");
SPList oList = SPContext.Current.Web.GetList("/ListName");

Correct:

SPList oList = SPContext.Current.Web.GetList("/sites/sitename/Lists/ListName");

Also make sure that the List is located in the same Web as the SPWeb object.

Correct:

SPWeb oWeb = new SPSite("http://localhost/sites/testsite").RootWeb;
//The above code should be written better, (using statement etc)

SPList oList = oWeb.GetList("/sites/testsite/Lists/ListName");

Wrong:

SPWeb oWeb = new SPSite("http://localhost/sites/testsite").RootWeb;
//The above code should be written better, (using statement etc)

SPList oList = oWeb.GetList("/sites/testsite2/Lists/ListName");

Enjoy :-)

4
  • How would u use Guids in a scenario where lists are created using code during feature activation and accessed via code dynamically after deployment.
    – thomius
    Feb 16, 2012 at 12:12
  • The syntax I have used is correct (/sites/...)
    – thomius
    Feb 16, 2012 at 12:13
  • Sometimes guids are not possible. Ill check ur solution in a few minutes. Feb 16, 2012 at 12:30
  • I've tested this in 2 occasions: Page Load, Button_Click. Both work. Just make sure that the List you are looking for is in the same web as the SPWeb object. I've modified my answer. Feb 17, 2012 at 10:59
2

Along with the points suggested by Frederik, you can try something

using (SPSite curSite = new SPSite(SPContext.Current.Web.Url))
                {
                    curSite.AllowUnsafeUpdates = true;
                    using (SPWeb curWeb = curSite.OpenWeb())
                    {
                        curWeb.AllowUnsafeUpdates = true;
                        SPList list2 = curWeb.GetList(SPUrlUtility.CombineUrl(curWeb .ServerRelativeUrl.TrimEnd('/'), "/Lists/ListName"));
                        curWeb.AllowUnsafeUpdates = false;
                    }
                    curSite.AllowUnsafeUpdates = false;
                }

The allow unsafe updates are the key here, i was also getting the same exception and the above code works for me.

1
  • 1
    Instead of SPUrlUtility.CombineUrl(curWeb.ServerRelativeUrl.TrimEnd('/'), "/Lists/ListName") you can do SPUtility.ConcatUrls(curWeb.ServerRelativeUrl, "/Lists/ListName") as it handles the '/' issue when combining URLs where it is not certain if there is a need to add or strip slashes between the concatenated URLs.
    – Jussi Palo
    Apr 18, 2017 at 6:08

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.