I have many subsite levels, and in the last one I have a pages library. I want to access it by code, the following code says the LIST does not exist.

When I debugged, the URL of SPContext.Current.Site is the sitecollection url not


so the SpDocumentlibrary line says that list does not exist.

on the ribbon ations

var options = {              
            url: '{SiteUrl}/_layouts/xxx/MoveFiles.aspx?items=' + myItems + '&source=' + SP.ListOperation.Selection.getSelectedList()  + '&url=' + SP.ClientContext.get_current().get_web().Url ,

protected override void OnLoad(EventArgs e)
        if (Request.QueryString["items"] != null && Request.QueryString["source"] != null)
            string source = Request.QueryString["source"];
            string url = Request.QueryString["url"];

            string[] items = Request.QueryString["items"].ToString().Split('|');
            LabelItems.Text = "You have selected the following items to move:<br><br>";
            source = source.Substring(1, source.Length - 2).ToLower();
            Guid sourceID = new Guid(source);


            string SiteUrl = SPContext.Current.Site.Url;//Get the url of current site
            using (SPSite site = new SPSite(SiteUrl))
                //open the site with the url passed
                using (SPWeb oWeb = site.OpenWeb())
                    SPDocumentLibrary sourceDocLib = (SPDocumentLibrary)oWeb.Lists["Site Pages"];
                    //SPDocumentLibrary sourceDocLib = (SPDocumentLibrary)SPContext.Current.Web.Lists["Site Pages"];

                    ListItems = new System.Collections.Generic.List<SPListItem>();
                    for (int i = 1; i < items.Length; i++)
                        SPListItem currentListItem = sourceDocLib.GetItemById(int.Parse(items[i]));
                        LabelItems.Text += currentListItem.Name + "<br>";


  1. My sites are like this: http://servername



So, in the wiki subsite I have a Site Pages Library. And on that context, the new ribbon button appears.

I tried using the {SPUrl} as explained below, but it returns me only this part: http://servername/TestWorkSpace

I need to be able to create a SPWeb object based on: http://servername/TestWorkSpace/Wiki

I updated the code above.!!!

  • Where is this running from? Depending on that SPContext.Current.X could be so many different things. Mar 7, 2012 at 16:04
  • this is an application page that I launch with a new button from the ribbon, the application page is obviously deployed to the layouts folder, that might be the reason? No matter what I do, it always returns me the sitecollection site. Mar 7, 2012 at 16:08
  • I could be remembering incorrectly but I want to say context should still work there. The caveat is that the application page could technically be ran from any site. For example it could be server/_layouts/page.aspx or server/sub1/sub2/sub3/_layouts/page.aspx - if the path you are using to open the page includes the subsites then maybe you really can't get the context? Mar 7, 2012 at 16:11
  • please see the update in how I am opening the dialog. Mar 7, 2012 at 16:14
  • Facing same issue - what was the fix for this? Feb 2, 2013 at 8:17

2 Answers 2


For the ribbon button the /_layouts/... will end up with a site collection relative url instead of a web relative url. In your command action when you define your ribbon button you can use the {SiteUrl} tag (which is replaced with the current web URL at run time) to generate a full URL to the application page that includes the current site. See this question and answer for more details.

For the server side of things you want to use SPContext.Current.Web instead of SPContext.Current.Site.

In the API an SPWeb refers to a site and all configuration options / content associated to it and an SPSite is the general configuration / properties of the Site Collection as a whole. (SPSite.RootWeb would give you the top level site - e.g.)

  • no, that doesnt not work either, it still says the list does not exist at url .... Mar 7, 2012 at 15:59
  • Updated my answer based on feedback. Mar 7, 2012 at 16:21
  • Hi, Almost there, please see my update, I could get a reference to the parent site of the subsite, but not yet to the subsite, I need to be able to create a SPDocumentLibrary object thats on the subsite level. Mar 7, 2012 at 16:45
  • So you must be hitting the button from a page on servername/TestWorkSpace? Also you are still using SPContext.Current.Site instead of SPContext.Current.Web according to your code. Mar 7, 2012 at 16:51
  • indeed, my mistake, it works now. give me few minutes to test and I will come back Mar 7, 2012 at 16:52

I just passed the {SiteUrl} as a Query String parameter and it solved my problem.

As i'm using CustomAction -> UrlAction and i'm passing this query string parameter.

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.