0

I want to get value of a column located in a list within another site but inside the same site collection. How do I do that?

Site 1: http://sitecollection/site1

Site 2: http://sitecollection/site2/subsite2/subsubsite2

Here is the way I do to get a list from the current site (site1):

using (SPSite site = new SPSite(SPContext.Current.Site.ID))
{
    using (SPWeb web = site.OpenWeb(SPContext.Current.Web.ID))
    {
        SPList currentWebList = web.GetListByInternalName(listInternalName);
    // doing stuff with list items here
    }
}

How to get the list from the site2 being on site1 ?

The method getListByInternalName is a custom method of course but it doesn't matter.

I try to replace the Current.Site.ID by Current.Site.EverythingElseToGetTheRightUrlOrID but it take way too much time to get the value so I think I'm missing something.

Does the fact that the list is located in another site could slow down processing time? (I'm just reading a column)

1

You can connect directly to the web like this

using (SPSite site = new SPSite(SPContext.Current.Web.Url))
using (SPWeb web = site.OpenWeb())
{
    SPList currentWebList = web.GetListByInternalName(listInternalName);
    // doing stuff with list items here
}
0

You should be able to access the subsite by using the REST api. A REST call similar to this (see below) should return a specific item from the subsite list.

http://sitecollection/site2/subsite2/subsubsite2/_api/web/lists/GetByTitle('Test')/items(itemid)

For more information on working with the SharePoint REST api you can read here: https://msdn.microsoft.com/en-us/library/office/dn292552.aspx


If you want to get a list item using CSOM then its possible you could use something like this:

// Starting with ClientContext, the constructor requires a URL to the 
// server running SharePoint. 
ClientContext context = new ClientContext("http://sitecollection/site2/subsite2/subsubsite2"); 

// Assume the web has a list named "Announcements". 
List announcementsList = context.Web.Lists.GetByTitle("Announcements"); 

// This creates a CamlQuery that has a RowLimit of 100, and also specifies Scope="RecursiveAll" 
// so that it grabs all list items, regardless of the folder they are in. 
CamlQuery query = CamlQuery.CreateAllItemsQuery(100); 
ListItemCollection items = announcementsList.GetItems(query); 

// Retrieve all items in the ListItemCollection from List.GetItems(Query). 
context.Load(items); 
context.ExecuteQuery(); 
foreach (ListItem listItem in items) 
{ 
    // We have all the list item data. For example, Title. 
    label1.Text = label1.Text + ", " + listItem["Title"]; 
}
  • Thank you Tekk_Know. Is there a way to do it only by using code behind ? – ameliapond Oct 19 '16 at 13:11
  • 1
    @ameliapond I'm not as familiar using the CSOM, but I'll edit my answer with a possible working CSOM answer – Tekk_Know Oct 19 '16 at 13:45
0
using (SPSite site = new SPSite(SPContext.Current.Site.Url)) // get the site collection url.
{   
    // The site, subsite, subsubsite must exist under the site collection.         
    using (SPWeb web = site.OpenWeb("/site2/subsite2/subsubsite2"))
    {
        // The full url of the list site including the site collection url.
        string listUrl = SPContext.Current.Site.Url + 
        "/site2/subsite2/subsubsite2/Lists/listInternalName";
        SPList list = web.GetList(listUrl);
    }
}

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.