Create list from template

Feb 7, 2013 at 9:49 PM
Did I mention I'm coding by the skin of my teeth?
So to get around my issue with not being able to add columns to a list programatically, I created a list along the right lines and saved it as a template.

BUT - when I open a list based on it to try and find the template ID, view source, search for ctx.listTemplate as I googled, it comes up as "100" which is the standard custom template right? So how do I create a new list based on my template "MyListTemplate" ?

thanks :)
Feb 9, 2013 at 3:46 PM
Anyone? I'd have thought this should be a fairly easy thing to manage!
Coordinator
Feb 10, 2013 at 5:35 AM
Are you now trying to create the list through the UI or programmatically? If it's the latter, can you show your code?

M.
Feb 10, 2013 at 11:48 AM
Hi Marc,

I was first trying to create a list progmatically (which worked) and then add columns to it (which didn't). Code is at the bottom of the post for reference.

That wasn't working, and as every list I need is based on the same four column names/types I figured I'd be better off creating the list once (thorugh the UI) and saving as a template, then (progmatically) creating a new list based on that template ID using the same bit of code operation: "AddList" as below. But I couldnt find out a seperate templateID for the template I created.
function createlist() {
 //Create list
 $().SPServices({
    operation: "AddList",
    listName: filename,
    description: "List created for quiz: "+filename,
    templateID: 100
 }); 
    //Add lots of fields to test (I previously tried just 1/2)
    var newFields = "<Fields>";
    for(i=1; i <= 100; i++) {
      newFields += "<Method ID='" + i + "'>" +
        "<Field Type='Text' DisplayName='Column_" + i + "' FromBaseType='TRUE' MaxLength='255' Description='Description of Column_" + i + "' />" +
        "</Method>";
    }
    newFields += "</Fields>";
    $().SPServices({
      operation: "UpdateList",
      listName: 'test1',
      newFields: newFields,
      completefunc: function (xData, Status) {
        alert(Status);
        alert(xData.responseText);
      }
    });


 alert('should be doned');
}
Coordinator
Feb 11, 2013 at 1:26 PM
Edited Feb 11, 2013 at 1:26 PM
If you call GetList, you can find out what the template GUID for that list is. For instance, then I call GetList on the foo list I created in the code I posted in your other thread, I see that ServerTemplate="100".
$().SPServices({
  operation: "GetList",
  listName: "foo"
});
<List DocTemplateUrl="" DefaultViewUrl="/Intranet/JQueryLib/Lists/foo/AllItems.aspx" MobileDefaultViewUrl="" ID="{C4590827-385E-4D55-B4E8-04F94236416E}" Title="foo" Description="List created for quiz: foo" ImageUrl="/_layouts/images/itgen.gif" Name="{C4590827-385E-4D55-B4E8-04F94236416E}" BaseType="0" FeatureId="00bfea71-de22-43b2-a848-c05709900100" ServerTemplate="100" Created="20130211 02:11:53" Modified="20130211 02:13:27" LastDeleted="20130211 02:11:53" Version="2" Direction="none" ThumbnailSize="" WebImageWidth="" WebImageHeight="" Flags="545263616" ItemCount="0" AnonymousPermMask="0" RootFolder="/Intranet/JQueryLib/Lists/foo" ReadSecurity="1" WriteSecurity="1" Author="3" EventSinkAssembly="" EventSinkClass="" EventSinkData="" EmailInsertsFolder="" EmailAlias="" WebFullUrl="/Intranet/JQueryLib" WebId="42f65d3f-343d-4627-a9a3-abf3d4d6491f" SendToLocation="" ScopeId="7c8593fb-90e4-4a2b-9e22-6ad48b082312" MajorVersionLimit="0" MajorWithMinorVersionsLimit="0" WorkFlowId="" HasUniqueScopes="False" AllowDeletion="True" AllowMultiResponses="False" EnableAttachments="True" EnableModeration="False" EnableVersioning="False" Hidden="False" MultipleDataList="False" Ordered="False" ShowUser="True" EnableMinorVersion="False" RequireCheckout="False">
I would suggest creating a list with all of the columns you want and then running GetList to obtain the XML. You could save that XML in a list as a document and read from it to create future lists.

M.
Jul 5, 2013 at 3:52 PM
Edited Jul 5, 2013 at 3:59 PM
Marc, could you expand on this a little more please?

I am trying to achieve a similar thing - manually creating a list with columns in the UI as a "template" of sorts, then programmatically creating a new list based on that "template with spservices.

I have used GetList to get the XML of the lists - but how much parsing does this require before I can "update" a new list? I have tried simply taking the <FIELDS> node of the template, but this doesnt work - with nasty soap errors.

Here is my code - is my approach wrong?

Thanks for your help

(SP2007 if that matters)
var _newListName= "newList";
$().SPServices({
         operation: "GetList",
         listName: "SubTemplate", //list created in the UI
         completefunc: function(xData, Status) {                             
             var _fields  = $(xData.responseXML).find("Fields").xml2string();  //simply convert to string                                                    
             createList(_newListName, _fields);
            }
});


function createList(_name, _newfields){
    $().SPServices({
          operation: "AddList",
          listName: _name,
          description: "list based on template",
          templateID: "100",  
          completefunc: function (xData, Status) {                           
                 $().SPServices({
                      operation: "UpdateList",
                      listName: _name,
                      newFields: _newfields,
                      completefunc: function (xData, Status) {}
                  });
          }
    });
}
Coordinator
Jul 9, 2013 at 3:14 AM
My guess is that if you look at the value of _fields, it isn't valid structure for UpdateList. Take a look at the docs to see what the structure needs to be and mimic that.
http://msdn.microsoft.com/en-us/library/lists.lists.updatelist.aspx

M.
Mar 15, 2015 at 2:29 PM
Hi, I am also stuck at a similar issue. Trying to programmatically create a new list, which ought to be an exact replica of an existing list and its data, that I have already saved as a list template on the SP2007 site.

$().SPServices({
webURL: "localhost/site/subsite/",
operation: "AddList",
listName: "testlist",
description: "test desc",
templateID: 100
});

The above code does create the list but there is only one field called the Title. (I guess that is because base tempateID 100 only contains this field). How about the other fields in my original list and the data? Do I need to use UpdateList to first add new fields and then create new list items cloning the existing list items in the original list?

Any help appreciated!
Coordinator
Mar 15, 2015 at 4:55 PM
templateID 100 is a Custom List. You'll need to use the templateID which corresponds to the type of list you want to create.

M.
Mar 15, 2015 at 7:40 PM
Marc, thanks for your valuable comments.

My list template is a custom list type. I have added custom fields and saved it as a template. I found the correct template ID by locally saving the stp file and then extracted the template id from manifest.xml. This turned out to be 1. However, the following code still gives the invalid template id error message.

$().SPServices({
webURL: "https://site/subsite",
operation: "AddList",
listName: "testlist1",
description: "test desc",
templateID: 1
});

I have also tried the following which fails with the error: "Cannot complete this action. Please try again."

$().SPServices({
webURL: "https://site/subsite",
operation: "AddListFromFeature",
listName: "testlist1",
description: "test desc",
featureID: featureIDofListTemplate,
templateID: 1
});
Coordinator
Mar 16, 2015 at 2:21 PM
The templateID is most definitely not just 1. It will be something like 100 (assuming it inherited from a Custom List) + a unique string to identify it.

M.
Mar 16, 2015 at 9:08 PM
In my experience with addList, I have had to make a second call to create the columns.

Paul
-- sent from mobile


Mar 16, 2015 at 11:57 PM
I agree with you. Seem to have exhausted all possible options. Need to create columns after using addList. This is because the templateID of the new template created by UI from the original customized list still remains 100. Ideally, it should have a new templateID but that is not the case.

@Marc: I have double checked. The templateID listed in the manifest.xml in the stp archive is 1.

Thanks for your comments,
Hark
Coordinator
Mar 20, 2015 at 1:25 PM
If you create a list manually using the saved template and look at its templateID, you'll see what the real value is. It won't be 1.

M.