SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
up vote 1 down vote accepted

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")
share|improve this answer
Thanks it worked. – Frank Martin Apr 8 '13 at 12:55
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? – Frank Martin Apr 9 '13 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. – Amit Kumawat Apr 10 '13 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. – Frank Martin Apr 10 '13 at 7:59

Your Answer


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.