Click here to Skip to main content
Click here to Skip to main content

Creating a new list in SharePoint Services at runtime

, 16 May 2006
Rate this:
Please Sign up or sign in to vote.
How to create a new list in a SharePoint Services area at runtime

Introduction

Not having found any documentation on how to create a new listing in SharePoint Portal Server during runtime, I decided to publish my findings so that others might also benefit from it.

Background

During a recent redesign of the corporate intranet, we found it necessary to create new lists in SharePoint Portal Server areas that had a consistent structure. Just one problem: the code would fail when trying to submit the new list when calling the Update.

Some assumptions

  1. You are familiar with creating webparts in VS.NET
  2. You have the webpart framework installed and running on your development PC

Using the code

Firstly get a reference to the current area that you want to work with. I assume that it is the current area that the webpart is running in and therefore referencing Context.

Dim thisWeb As SPWeb = SPControl.GetContextWeb(Context)

Create a reference to the new list that you want to add to the area. The reference has to be a Guid as SPList accepts an identifier of this type.

I am assigning the GenericList template to the list as I want to start with a clean list template. You can assign any of the SPListTemplateType templates to the list if you want to use a generic list format.

Dim docLibName As String = "New List Name"
Dim guid As System.Guid = thisWeb.Lists.Add(docLibName, _
 "A custom list created during runtime.", SPListTemplateType.GenericList)

Add the field that you want to have in the field. First apply the global settings and force an Update before you add the fields to the list.

Dim newList As SPList = thisWeb.Lists(guid)
With newList
    .AnonymousPermMask = SPRights.EmptyMask
    .EnableVersioning = True
    .OnQuickLaunch = True
    .ReadSecurity = 1
    .WriteSecurity = 2
    .Update()
    .Fields.Add("Text field one", SPFieldType.Text, True)
    .Fields.Add("Text field two", SPFieldType.Text, False)
    .Fields.Add("Boolean field", SPFieldType.Boolean, False)
End With

As far as I can tell, the only field that any template will "force" on the list is the Title field.

Points of Interest

Do not call Update again after adding the new fields! If you do call another Update, then you will receive the following exception on the next Update:

Save Conflict

Your changes conflict with those made concurrently by another user. 
If you want your changes to be applied, click Back in your Web browser, 
refresh the page, and resubmit your changes. 

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

RayBez
Program Manager Ashrays.co.za
South Africa South Africa
Stuck with a desk job by day, but an entrepreneur, software developer and internet marketer by night. I own a South African retail site - Ashrays.co.za - and have been involved in a number of web ecommerce projects. ASP.Net being my main technology of choice while building on MOSS 2010.
Follow on   LinkedIn

Comments and Discussions

 
GeneralRun SharePoint application on Windows XP PinmemberShailesh B Nikam23-Oct-07 18:02 
GeneralFurther Assistance PinmemberMegathron5-Jul-07 4:04 
GeneralVery Useful. PinmemberAshaman19-May-06 1:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 17 May 2006
Article Copyright 2006 by RayBez
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid