Create a list in a SharePoint site programmatically
Posted by andrewmyhre on March 21, 2009
Creating a list programmatically might be handy when you need to do this in a web part or, if you’re in the situation I was in today, you need to create a list where the template is not available on the Create page.
In my case I had a Collaboration site but I needed to create a Discussion Board – impossible using the SharePoint UI. How to get around this?
The first solution that occurred to me, and what I assumed I HAD to do, was to create a custom site definition. I had no idea how to go about doing this so this was a daunting prospect. But it occurred to me that, ultimately, I’ll only need to do this one time, maybe twice, and my users will never do this. So why go to all that trouble when the purpose of a site definition is to create a repeatable story for your end users? All I needed was a one shot hack.
So I remembered the SharePoint object model, and wrote an app. This app takes three arguments – Site URL, List Name, List Template ID. I used it to create a new discussion board in my site, it’s repeatable enough for my purposes, and it took me 5 minutes to write.
The code is provided below, but I want to hit home the point of this post – SharePoint is not as difficult as you think. There is always a simple way to do what you want to do. The difficulty is remembering to think outside the UI. I used to think that where SharePoint build was concerned I’d have to get used to telling customers ‘No’ but, as it turns out, that’s not that case at all.
CreateList.exe Source
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.SharePoint;
6
7 namespace CreateList
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 if (args.Length == 0)
14 {
15 PrintListTemplates();
16 return;
17 }
18
19 bool validArgs = true;
20 if (!Uri.IsWellFormedUriString(args[0], UriKind.Absolute))
21 {
22 Console.WriteLine(“Arg 0 must be an absolute Sharepoint site URL e.g: http://mysite.com”);
23 validArgs = false;
24 }
25
26 int templateId=0;
27 if (!int.TryParse(args[2], out templateId))
28 {
29 Console.WriteLine(“Arg 2 must be a valid list template id from the following list:”);
30 validArgs = false;
31 PrintListTemplates();
32 }
33 if (!validArgs)
34 {
35 Console.WriteLine(“Invoke with no arguments to get a list of available SharePoint List templates”);
36 return;
37 }
38
39
40 Uri siteUrl = new Uri(args[0]);
41 string listName = args[1];
42
43 AddListToSite(siteUrl, listName, templateId);
44 }
45
46 private static void AddListToSite(Uri siteUrl, string listName, int SPListTemplateTypeId)
47 {
48 using (SPSite site = new SPSite(string.Format(“{0}”, siteUrl)))
49 {
50 SPWeb web = site.OpenWeb();
51 Guid listGuid = web.Lists.Add(listName, “”, (SPListTemplateType)SPListTemplateTypeId);
52 SPList list = web.Lists.GetList(listGuid, true);
53 }
54 }
55
56 private static void PrintListTemplates()
57 {
58 string[] typeNames = System.Enum.GetNames(typeof(SPListTemplateType));
59 Array typeValues = System.Enum.GetValues(typeof(SPListTemplateType));
60
61 int j = 0;
62
63 foreach (int i in typeValues)
64 {
65 Console.WriteLine(typeNames[j++].ToString() + ” “ + i.ToString());
66 }
67 }
68 }
69 }
sara said
Nice,
Try this too…
Create SharePoint list in C# and more !