Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I'm trying to access list items from an other site collection than the current "sitecollectionname" where I laungh my code in. I would like to validate the url of the current "sitecollectionname" with one of the fields/columns named 'Site Url' of the items in the 'Mylist'.

http://myserver/ root site
http://myserver/managedpathA/ root site with MyList
http://myserver/managedpathB/sitecollectionname

VS2010 indicates a problem with the listUrl passed in GetList... When putting the MyList in the root site at http://myserver/ and using SPSite("http://myserver") it works fine...

using (SPSite site = new SPSite("http://myserver/managedpathA"))
{
    using (SPWeb web = site.RootWeb)
    {
        SPWeb cweb = SPContext.Current.Web;
        string siteurl = cweb.Url.ToString();

        // Get data from a list.
        string listUrl = "/Lists/MyList";            
        SPList list = web.GetList(listUrl);
        SPListItemCollection items = list.Items;

        for(int i=0; i<items.Count;i++) 
           {
            SPListItem listItem = items[i];
            string listsiteurl = listItem["Site Url"].ToString();


            if (siteurl == listsiteurl)
            {
                OutputLabel.Text = listItem["Site Url"].ToString() + "Found in List"+ "<br/>";
            }
            else 
            {
                OutputLabel.Text = "No match found for current ste url in MyList";
            }
        }
    }
}
share|improve this question
add comment

4 Answers

GetList throws a couple of Exceptions, so put a try catch handler round it and inspect the Exception.Message object that's thrown to see where the problem is.

As a general rule, also, don't ever only retrieve SPList.Items. It's very slow and once many users start accessing it you'll inflate response times no end. Instead, use SPList.GetItems, passing in a SPQuery object with a CAML query set.

Details:

SPWeb.GetList

SPList.GetItems

share|improve this answer
1  
Thanks for the advise will follow it! You don't have any examples of getting a list in another site collection than the one where you define your SPWeb web? –  Wim Hill Jan 26 '11 at 13:53
    
Just looking at your original post in more detail looks like you're possibly passing in the wrong path. Add in the exceptions per the advice from James. I'd bet that "/Lists/MyList" is probably where your root problem lies. –  Neil Richards Jan 26 '11 at 16:36
add comment

If you're running this as a console application you will probably need to wrap your code in an SPSecurity.RunWithElevatedPrivileges statement. I sometimes get blocked when running this from the console.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    // implementation details omitted
});
share|improve this answer
    
This is a good point, you will need this if the current user doesn't have permissions on the other site collection. Remember that this would mean that your solution would not be able to run in the Sandbox. –  James Love Jan 26 '11 at 13:53
add comment
up vote 1 down vote accepted

Thanks all...

The following code did the trick:

using (SPSite site = new SPSite("http://myserver/managedpathA"))
{
    using (SPWeb web = site.RootWeb)
    {

        string currentsiteurl = SPContext.Current.Web.Url;

        SPQuery query = new SPQuery();
        query.Query = string.Concat(
                       "<Where><Eq>",
                          "<FieldRef Name='NewSiteTitle'/>",
                          "<Value Type='Text'>" + currentsiteurl + "</Value>",
                       "</Eq></Where>");

        query.ViewFields = string.Concat(
                            "<FieldRef Name='NewSiteVisible' />",
                            "<FieldRef Name='NewSiteRestricted' />",
                            "<FieldRef Name='NewSitePublic' />");

        query.ViewFieldsOnly = true; // Fetch only the data that we need.

        // Get data from a list.
        string listUrl = string.Format("{0}/Lists/NewSiteParameters", web.Url);
        SPList list = web.GetList(listUrl);
        SPListItemCollection items = list.GetItems(query);

        foreach (SPListItem item in items)
        {


            OutputLabel3.Text = item["NewSiteVisible"].ToString() + " " + item["NewSiteRestricted"].ToString() + " " + item["NewSitePublic"].ToString() + "<br/>";



        }

   }
}
share|improve this answer
add comment

If you are passing the list url to web.GetList(), it has to be full URL rather than a relative one.

string listUrl = string.format("{0}/Lists/MyList", web.Url);
SPList list = web.GetList(listUrl);

Additionally, be carefull with the following line of code:

SPWeb cweb = SPContext.Current.Web

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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