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 have a parent site which creates child pages programmatically. They use a custom template with a List which I have created in VS. Now I want to access that list in C# from the parent site. I have tried it like the code part below but when I loop through all available lists it isn't there. My question is, how can I acccess the list. After debugging it fails when it looks for the ArtistInformation list.

            //Create website
            artistWeb.Webs.Add(artist, artist, artist, artistWeb.Language, webTemplate,     false, false);
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
            listItem.SystemUpdate(false);
        }

            fillArtistInformation();
    }

    public void fillArtistInformation()
    {
        using (SPSite siteCollection = new SPSite(fullURL))
        {
            SPWeb web = siteCollection.OpenWeb();
            System.Diagnostics.Debug.Write(web.Title);

            try
            {
                SPList list = web.Lists["ArtistInformation"];
                SPListItem item = list.Items.Add();
                item["Title"] = artist;
                item["WorkAddress"] = address;
                item["WorkZip"] = zip;
                item["WorkCity"] = city;
                item["WorkCountry"] = country;
                item["Price"] = price;
                item.Update();
            }
            catch (Exception e)
            {
                System.Diagnostics.Debug.WriteLine(e);
            }
        }
    }
}
share|improve this question
    
Can you provide XML definition of your element file? If you create your element.xml using visual studio - your list name is generated by VS... –  Andrew Adamich Feb 22 '13 at 9:13

1 Answer 1

up vote 1 down vote accepted

Firstly, your SPWeb (which you've created using OpenWeb()) should be inside a using() construct.

The reason I suspect that you're not able to get the correct list is either because:

  • The name of the list is not "ArtistInformation", remember you must use the DISPLAY NAME of the list in this situation

or

  • fullURL does not have the actual URL of the created web within it. Since you haven't given us the context for that variable there is no way to tell if this is absolutely true, but I suspect that it is, in fact, the parent web's URL.

Tim

share|improve this answer
    
Your first solution fixed my problem. Thank you! –  Fabian Blom Feb 22 '13 at 14:10

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.