Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
    
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 '12 at 9:52

2 Answers 2

up vote 3 down vote accepted

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 :-)

share|improve this answer
    
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 '12 at 12:12
    
The syntax I have used is correct (/sites/...) –  thomius Feb 16 '12 at 12:13
    
Sometimes guids are not possible. Ill check ur solution in a few minutes. –  Frederik P. Feb 16 '12 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. –  Frederik P. Feb 17 '12 at 10:59

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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