I’m working on a project which involves scripting, via batch files, an intranet portal. We wanted to script the creation of the various division and department sites along with several lists that would be needed in each site. Eventually these site will be turned into custom site definitions using Features and Feature Stapling to provision the lists and various files but we needed something to enable us to quickly mock up a site taxonomy and present that to stakeholders. I had lots of commands to accomplish many of the required tasks but I didn’t have anything to create lists and do some basic configurations of those lists. The first command I needed is detailed here and is called gl-addlist. I’ll discuss some of the other commands I created in follow-up posts.

Fortunately, creating a list via code is super simple – there’s single Add method you call via the SPListCollection object which you can get from the SPWeb’s List property. So turning this into a new STSADM command is as simple as getting the parameters needed and passing them into the Add method:

  1public class AddList : SPOperation
  2{
  3     /// <summary>
  4    /// Initializes a new instance of the <see cref="AddList"/> class.
  5    /// </summary>
  6    public AddList()
  7    {
  8        SPParamCollection parameters = new SPParamCollection();
  9        parameters.Add(new SPParam("url", "url", true, null, new SPUrlValidator(), "Please specify the web url to add the list to."));
 10        parameters.Add(new SPParam("urlname", "name", true, null, new SPNonEmptyValidator()));
 11        parameters.Add(new SPParam("title", "title", true, null, new SPNonEmptyValidator()));
 12        parameters.Add(new SPParam("featureid", "fid", true, null, new SPGuidValidator()));
 13        parameters.Add(new SPParam("templatetype", "type", true, null, new SPIntRangeValidator(0, int.MaxValue)));
 14        parameters.Add(new SPParam("description", "desc", false, null, new SPNonEmptyValidator()));
 15        parameters.Add(new SPParam("doctemplatetype", "doc", false, null, new SPIntRangeValidator(0, int.MaxValue)));
 16 
 17        StringBuilder sb = new StringBuilder();
 18        sb.Append("\r\n\r\nAdds a list to a web.\r\n\r\nParameters:");
 19        sb.Append("\r\n\t-url <web url to add the list to>");
 20        sb.Append("\r\n\t-urlname <the name that will appear in the URL>");
 21        sb.Append("\r\n\t-title <list title>");
 22        sb.Append("\r\n\t-featureid <the feature ID to which the list definition belongs>");
 23        sb.Append("\r\n\t-templatetype <an integer corresponding to the list definition type>");
 24        sb.Append("\r\n\t[-description <list description>]");
 25        sb.Append("\r\n\t[-doctemplatetype <the ID for the document template type>]");
 26        Init(parameters, sb.ToString());
 27    }
 28 
 29   
 30    #region ISPStsadmCommand Members
 31 
 32    /// <summary>
 33    /// Gets the help message.
 34    /// </summary>
 35    /// <param name="command">The command.</param>
 36    /// <returns></returns>
 37    public override string GetHelpMessage(string command)
 38    {
 39        return HelpMessage;
 40    }
 41 
 42    /// <summary>
 43    /// Runs the specified command.
 44    /// </summary>
 45    /// <param name="command">The command.</param>
 46    /// <param name="keyValues">The key values.</param>
 47    /// <param name="output">The output.</param>
 48    /// <returns></returns>
 49    public override int Execute(string command, StringDictionary keyValues, out string output)
 50    {
 51        output = string.Empty;
 52 
 53        try
 54        {
 55            string url = Params["url"].Value;
 56 
 57            using (SPSite site = new SPSite(url))
 58            using (SPWeb web = site.OpenWeb())
 59            {
 60                AddListHelper(web.Lists, Params["urlname"].Value, Params["title"].Value, Params["description"].Value,
 61                              new Guid(Params["featureid"].Value),
 62                              int.Parse(Params["templatetype"].Value),
 63                              Params["doctemplatetype"].Value);
 64            }
 65        }
 66        catch (Exception ex)
 67        {
 68            Console.WriteLine("An error occured executing the command: {0}\r\n{1}", ex.Message, ex.StackTrace);
 69            return (int)ErrorCodes.GeneralError;
 70        }
 71        return OUTPUT_SUCCESS;
 72    }
 73 
 74    #endregion
 75 
 76 
 77    /// <summary>
 78    /// Adds a list to the specified list collection.
 79    /// </summary>
 80    /// <param name="lists">The lists collection to add the list to.</param>
 81    /// <param name="urlName">Name of the list to use in the URL.</param>
 82    /// <param name="title">The title of the list.</param>
 83    /// <param name="description">The description.</param>
 84    /// <param name="featureId">The feature id that the list template is associated with.</param>
 85    /// <param name="templateType">Type of the template.</param>
 86    /// <param name="docTemplateType">Type of the doc template.</param>
 87    /// <returns></returns>
 88    internal static SPList AddListHelper(
 89        SPListCollection lists, 
 90        string urlName, 
 91        string title, 
 92        string description, 
 93        Guid featureId, 
 94        int templateType, 
 95        string docTemplateType)
 96    {
 97        if (docTemplateType == "")
 98            docTemplateType = null;
 99 
1001        Guid guid = lists.Add(title, description, urlName, featureId.ToString("D"), templateType, docTemplateType);
1011        return lists[guid];
1021    }
1031 
1041}

The help for the command is shown below:

C:\>stsadm -help gl-addlist

stsadm -o gl-addlist


Adds a list to a web.

Parameters:
        -url <web url to add the list to>
        -urlname <the name that will appear in the URL>
        -title <list title>
        -featureid <the feature ID to which the list definition belongs>
        -templatetype <an integer corresponding to the list definition type>
        [-description <list description>]
        [-doctemplatetype <the ID for the document template type>]

The following table summarizes the command and its various parameters:

Command NameAvailabilityBuild Date
gl-addlistWSS v3, MOSS 2007Released: 8/6/2008
Parameter NameShort FormRequiredDescriptionExample Usage
urlYesThe URL of the web to add the list to.-url http://portal/
urlnamenameYesThe text that will appear in the URL for the list. So if the web URL is http://portal and you want the list to be reached via http://portal/Lists/ListName/AllItems.aspx you would pass in “Lists/ListName” as the value.-urlname Lists/ListName, -name Lists/ListName
titleYesThe title of the list.-title "A New List"
featureidfidYesThe ID of the Feature that the list is defined in. You can find this information in the 12/Template/Features/… folders. For example, the Announcements list is defined in 12/Template/Features/AnnouncementsList.-featureid "00BFEA71-D1CE-42de-9C63-A44004CE0104", -fid "00BFEA71-D1CE-42de-9C63-A44004CE0104"
templatetypetypeYesThe template type ID of the list. Like the Feature ID this information can be obtained from the 12/Template/Features/… folder. For example, the Announcements list template type ID is 104 and can be found in 12/Template/Features/AnnouncementsList/ListTemplates/Announcements.xml-templatetype 104, -type 104
descriptiondescNoA brief description of the list.-description "This is a new list.", -desc "This is a new list."
doctemplatetypedocNoA string containing the integer ID for the document template type. This value corresponds to the Type attribute value specified for a document template in the Onet.xml file of the specified site template. The following list shows the default possible values: 100 – None, 101 – Word document, 102 – SharePoint Designer Web page, 103 – Excel spreadsheet, 104 – PowerPoint presentation, 105 – Basic page, 106 – Web Part page, 130 – Data connection, 1000 – Blank form. This parameter only applies to document libraries.-doctemplatetype 100, -doc 100

The following is an example of how to create an announcements list intended specifically for new promotions:

stsadm -o gl-addlist -url http://portal -urlname Lists/Promotions -title Promotions -featureid "00BFEA71-D1CE-42de-9C63-A44004CE0104" -templatetype 104 -description "This list contains all announcements related to employee promotions."