0

I am using the following code to read a picture library:

SPList list = SPContext.Current.Web.GetList("http://win-12/sites/mysite/ImageLibrary")

Problem is, in above code "win-12" is my machine name and if I move this web part to some other server, I don't think it will work!?

So I want to know how to use dynamic or relative path instead of hard coded one?

Or before moving this web part to new server, I will have to change its URL then recompile?

1 Answer 1

1

No need of using absolute URL. You can pass server-relative URL of the list in SPWeb.GetList().

SPList list = SPContext.Current.Web.GetList("/sites/mysite/ImageLibrary")
3
  • Ok I just realized one thing. On my development machine, site's name is "mysite" as you can see in URL above. What if on production, site's name is something else? Then my webpart won't work. How to make it work then? Commented Apr 9, 2013 at 14:25
  • Have the list in current web and use SPContext.Current.Web.Lists["<your-list-name>"]; Although this is less preferred way in case your web is having a lot of lists. Commented Apr 10, 2013 at 6:09
  • This brings me to another problem. My current list is created in "parent" site. I have two variations "English" and "Arabic". So when I give list name in above code, it gives error that "list not found" because "English" is the default variation that opens while list is created in "parent" hence it cannot access it. If I define my list in "English" then "Arabic" site won't be able to access it. Commented Apr 10, 2013 at 7:59

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.