I'm making a visual webpart in a sp2010 environment. I am trying to get the document library URL. I have this code so far:

My variables are not getting the right values. Like the libraryURL, librarySiteRelativeURL, libraryNewFormURL, libraryNewFolderURL, librarySettingsURL.

For example if the library was located here:

http://mydomain/en/Information/admin/IS/RD/Pages/mylibrary where /en... is a site collection, then I would like:

libraryURL = http://mydomain/en/Information/admin/IS/RD/Pages/mylibrary
librarySiteRelativeURL = /en/Information/admin/IS/RD/Pages/mylibrary
(librarySettingsURL is the settings page of the library)


        context = SPContext.Current;
        web = context.Web;
        site = context.Site;

        webURL = web.Url;
        siteURL = site.Url;
        pageURL = webURL + System.Web.HttpContext.Current.Request.RawUrl;
        pageURLWithoutQuery = (new Uri(pageURL)).GetLeftPart(UriPartial.Path);

            // get the list
            library = (SPDocumentLibrary)web.GetList(webpartObject.SHAREPOINT_LIST);
            libraryURL = siteURL + "/" + library.RootFolder.Url;
            libraryName = Path.GetFileNameWithoutExtension(libraryURL);
            librarySiteRelativeURL = siteURL + library.ParentWebUrl;
            libraryNewFormURL = webURL + "/_layouts/Upload.aspx?List={" + library.ID + "}&RootFolder=/" + libraryName;
            libraryNewFolderURL = libraryURL + "/Forms/Upload.aspx?RootFolder=/" + libraryName + "&Type=1&IsDlg=1";
            librarySettingsURL = siteURL + "/_layouts/listedit.aspx?List={" + library.ID + "}";

            fileOrderProperty = webpartObject.ORDER_PROPERTY_NAME;
            library = null;

Does anyone know the right way of getting these links?


When working with URLs in SharePoint, there are two very helpful utility classes: SPUtility and SPUrlUtility

There are three different kinds of url used in different properties of various SharePoint objects: absolute, server-relative and site-relative. One need to be careful with them, especially when site collection is not located directly at web application root url.

In order to get list/library url from SPList object, the following method could be used (examples):

var web = SPContext.Current.Web;
var webAbsoluteUrl = web.Url;
var library = web.GetList("<Server-relative url needed here, not site relative as documentation says>");
var libraryAbsoluteUrl = SPUtility.ConcatUrls(webAbsoluteUrl, library.RootFolder.Url);
var libraryServerRelativeUrl = library.RootFolder.ServerRelativeUrl;
var libraryUploadFormUrl = SPUtility.ConcatUrls(webAbsoluteUrl, string.Format("/_layouts/Upload.aspx?List={0}", library.ID));
var libraryNewFolderServerRelativeUrl = string.Format("{0}?RootFolder={1}&Type=1", library.DefaultNewFormUrl, HttpUtility.UrlEncode(library.RootFolder.ServerRelativeUrl));
var libraryNewFolderAbsoluteUrl = SPUtility.GetFullUrl(SPContext.Current.Site, libraryNewFolderServerRelativeUrl);
var librarySettingsUrl = SPUtility.ConcatUrls(webAbsoluteUrl, string.Format("/_layouts/listedit.aspx?List={0}", library.ID));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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