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 intend to create list using code. So I used an empty SharePoint project and in that I used the ApplicationPage.aspx. In that I wrote the following code:

using(SPSite siteColl=new SPSite(@"http://diplsp:1090/"))
            {
                SPWeb webSite = siteColl.OpenWeb();
                webSite.Lists.Add("Leave Form1", "", webSite.ListTemplates["Calendar"]);

                SPList leaveList = webSite.Lists["Leave Form"];

                leaveList.OnQuickLaunch = true;
                leaveList.Fields.Add("Start Date", SPFieldType.DateTime, true);

                leaveList.Update();
            }

So according to the above code I should see the list created on my quick launch, but its not happening. However if I use the console application or list feature the above code works perfectly.

Please suggest.

share|improve this question
    
any error messages? –  int32 Sep 5 '11 at 11:58
    
Nope no error messages.... –  Chandra Shekhar Sep 5 '11 at 12:13

1 Answer 1

Not sure that Leave Form1 (when adding list) and Leave Form (when getting list) is issue here but this code works for me:

    using(SPSite site = new SPSite("http://diplsp:1090/"))
    {
        using(SPWeb web = site.OpenWeb())
        {
            web.Lists.Add("Leave Form1", "", web.ListTemplates["Calendar"]);
            SPList list = web.Lists["Leave Form1"];
            list.OnQuickLaunch = true;
            list.Fields.Add("Start Date", SPFieldType.DateTime, true);
            list.Update();
        }
    }

And if you want to be sure no errors are generated you need to check if your list exist before adding it.

Btw. You always need to dispose SPWeb object (when you are finished with it) either by putting it in using (like in my code) or by calling web.Dispose()

share|improve this answer
    
Hmm...I tried that as well..but same issues.....I guess the list is created but for some reasons its not displayed.....Any ideas why? –  Chandra Shekhar Sep 7 '11 at 3:55
    
Did you tried to add your list manually? Just to check if everything is working as expected. –  Vedran Rasol Sep 7 '11 at 6:55

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.