I created an empty sharepoint project. Then I added a new application page and in the page_load method of the page I wrote this code:

protected void Page_Load(object sender, EventArgs e)
        SPSite sitecollection = SPContext.Current.Site;

        SPWeb web = /*sitecollection.OpenWeb()*/ SPContext.Current.Web;

                web.AllowUnsafeUpdates = true;
                SPListCollection lists = web.Lists;
                lists.Add("My new List", "A new list created using code", web.ListTemplates["Calendar"]);

                SPList newOne = web.Lists["My new List"];

                newOne.OnQuickLaunch = true;
                newOne.Fields.Add("Demofield", SPFieldType.Text, false);

And if I am right a new list will be created on the quick launch bar, but its not happening so. Did I miss something????

  • 1
    Do you see the new list? Use dispose your SPSite and SPWeb properly. They are deadly! – Shoban Jun 6 '11 at 8:50
  • 1
    Hello Shoban. Thank you for the reply. And how to use the SPSite and SPWeb??? I am really new to shaepoint programming, please explain. Thank You – Chandra Shekhar Jun 6 '11 at 8:57
  • Check this post. msdn.microsoft.com/en-us/library/aa973248%28v=office.12%29.aspx Also what happens when you execute the above code? Do you see the new list? – Shoban Jun 6 '11 at 8:58
  • Hello, No I don't see the list in the quick launch and not even in "View All Site Content" – Chandra Shekhar Jun 6 '11 at 9:02
  • I modified the code which I am editing above. Please have a look and provide the feedback. Thanks – Chandra Shekhar Jun 6 '11 at 9:09

You are disposing an instance of SPContext.Current.Site (ie: enveloped it in a Using Statement). This is bad, as the SPSite will be disposed before the page has finished with it.

If you are getting an SPSite from SPContext, do not wrap it in a using statement.

  • Hello James, I modified the code accordingly, but no results. Please suggest – Chandra Shekhar Jun 6 '11 at 10:00
  • Is this a publishing site? – James Love Jun 6 '11 at 10:04
  • No, its a blank site. – Chandra Shekhar Jun 6 '11 at 10:09
  • 1
    Ok, I just tried this code and it worked for me, showed on the QuickLaunch and all (apologies for lack of formatting here) SPSite site = new SPSite("http://demolab-sps2010"); SPWeb web = site.OpenWeb(); Guid listG = web.Lists.Add("My List", "", web.ListTemplates["Calendar"]); SPList list = web.Lists[listG]; list.OnQuickLaunch = true; list.Update(); – James Love Jun 6 '11 at 10:11
  • Hello James, thanks for the input but it didn't work for me. Where exactly are you writing that piece of code. Well I am creating an empty sharepoint project then added an application page and in the page_load method I am writing the above code. Is that right??? Please suggest – Chandra Shekhar Jun 6 '11 at 10:21

If your code is excuted from an application page or web part:

SPContext.Current.Web.Lists.Add("My List Title", "Description of My List", SPListTemplateType.Announcements);

If your code is executed from a web-scoped feature, it will be in a feature receiver and you'll get the SPWeb from the properties parameter:

  using (SPWeb web = properties.Feature.Parent as SPWeb)
    web.Lists.Add("My List Title", "Description of My List", SPListTemplateType.Announcements);
  • Dear Laurie, thank you for the information. I have used the above mentioned code in an console application and it works, but however if I do it using an application page it doesn't work. Can you please provide me the steps on how to create a list using application page?? Thank you. – Chandra Shekhar Jun 7 '11 at 6:42
  • Some things to check: Verify that the user has permission to create a list on the site. Make sure that your dll is deployed to the GAC and that you have recycled the app pool for this web app. To be sure that your code is being executed, set a breakpoint and attach to the w3wp.exe process. Then execute the event that should hit your code. – Laurie Jun 7 '11 at 6:52
  • Dear Laurie, I would like to add the information to the <SafeControls> tab in web.config file. So please suggest where I can find the information. I am using the application page only so how to obtain the info to add in <SafeControls> tab. Please suggest. thank u – Chandra Shekhar Jun 7 '11 at 9:34
  • SafeControl entry is not necessary for application pages. These pages reside on the file system and can execute code. The SafeControl entry is needed for web parts, since they will be added to a customized page and will reside in the database. – Laurie Jun 7 '11 at 15:45
  1. Open Google in your browser
  2. Enter "sharepoint create list programmatically". Click Search.
  3. Take your pick of the blog posts showing how to do this.
  • I could create list using console application in visual studio, but unable to do so using application page....if you can help me out please provide me the steps on how to do it.... – Chandra Shekhar Jun 7 '11 at 6:39

Check this



I recommend getting a hold of this book: Inside Microsoft SharePoint 2010. It will provide you with a good foundation.

I used Inside Microsoft Windows SharePoint Services 3.0 to learn SharePoint development, so I trust the 2010 version will be superb as well.

Without a good understanding of the framework, you are going to continue to be frustrated by the whole development experience.

  • Thank you Laurie. Just wanted to get my hands dirty while I learn coding. Cheers – Chandra Shekhar Jun 8 '11 at 4:55

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.