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
/site/site1/site3
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);
//SPUtility.GetPageUrlPath(SPContext.Current);
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]));
ListItems.Add(currentListItem);
LabelItems.Text += currentListItem.Name + "<br>";
}
}
}
}
}
UPDATE
- My sites are like this: http://servername
http://servername/TestWorkSpace
http://servername/TestWorkSpace/Wiki
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.!!!