Friday, August 7, 2009

Programmatically get the sharePoint List Url in C#

To get the Share Point list "url" based on list "Guid", here is the code shown below

SPSite Site = SPContext.Current.Site;
SPWeb web = Site.OpenWeb();
Guid listid = new Guid("{xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}");
web.AllowUnsafeUpdates = true;
SPList List = web.Lists[listid];
string url = SPContext.Current.Site.Url + "/" + List.RootFolder.Url;
Response.Write(url);
web.AllUnSafeUpdates = false;
web.Dispose();

Hopes this helps you a lot.

6 comments:

Asad Jahangir said...

Thanks...

Anonymous said...

Depending on your needs, you may want to change that to ...Site.ServerRelativeUrl +"/" +...

That avoids having the http://MyServer.mycompany.com at the beginning, which can mess up alternate access mappings and, if you are storing the URL makes it more portable.

Jeff said...

if you are on a subsite, it won't work. So it's better to get the SPWeb url instead of the Spsite :)

Unknown said...

sharepoint electronic signature
Thanks for the code. I always get needed help from your blog. You have posted so many useful source codes on your blog. Keep sharing, I will visit again.

Abdul Hafees said...

thanks , it helped me.

Anonymous said...

Thanks very much