4

I am trying to imitate the process of creating a list from a template in the UI by using a Powershell script. When you create a list from a template with the UI, all of the original list's views are created in the new list. But when I use:

$templateName = "CM-$listName"
$templateFilename = $templateName + ".stp"
$list.SaveAsTemplate($templateFilename, $templateName, "$listName template", $true)

...get the template copied to the list gallery of the $outWeb site collection ...

$listTemplates = $outRootWeb.Site.GetCustomListTemplates($outRootWeb)    
$newGuid = $outWeb.Lists.Add($list.Title, $list.Description, $listTemplates[$templateName])

I get the new list created but with only the All Documents view.

  • what kind of template you are using? OOTB or custom? which view were not being created via powershell – Waqas Sarwar MVP Jul 25 '14 at 21:29
  • The template was created earlier in the same Powershell script using SPList.SaveAsTemplate. – Michael Frederick Jul 26 '14 at 2:56
  • 1
    I did find that the code that I was using (and I showed in the original question) was using the system-defined list templates and not my custom-created template. So I fixed the code and guess what; I still get the list and no views! – Michael Frederick Jul 27 '14 at 2:15
  • Code now looks like: $listTemplates = $inWeb.Site.GetCustomListTemplates($inWeb) $newGuid = $outWeb.Lists.Add($list.Title, $list.Description, $listTemplates[$templateName], $outWeb.DocTemplates[0]) – Michael Frederick Jul 27 '14 at 2:16
1

Use SPList.SaveAsTemplate for your source. Should be the same as the GUI.

If that doesn't work, access the SPList.Views collection and copy the views over to the new list after creating it from the template.

  • The template was created using SPList.SaveAsTemplate – Michael Frederick Jul 26 '14 at 2:55
  • Have you tried comparing the contents of the file using SaveAsTemplate compared to the GUI file? I think they are both zip files. Also, could you update your code sample to include the save statement? It would also help to describe your custom document library. For example, what are the details of the views that don't reproduce? – Tom Resing Jul 27 '14 at 13:55
  • I used my script to create the template and leave it in place. When I create a library using the UI from this template, I get all views. When I create another library with a different name I see several differences: 1) The UI-restored library has all views created; the script one does not, 2) the UI-created library has content types in place; the script version does not 3) when you use View All Site Content the 2 libraries have different icons; the UI-created library has a normal-looking icon, but the script-created library has a lock instead of a white document in the lower right corner. – Michael Frederick Jul 27 '14 at 19:40
  • Just so you understand, now I am creating 2 lists from the same list template; 1 using the UI and 1 using my script. The resulting lists are not the same. – Michael Frederick Jul 27 '14 at 22:58
  • 1
    Yes, my last experiments were on the same exact site. – Michael Frederick Jul 28 '14 at 18:28
0

Try this for creating list and view in sharepoint using powershell :

http://nkelkar.wordpress.com/2011/06/21/powershell-create-lists-add-columns-set-new-default-view/

  • 1
    Please add more details to your answer (e.g. give a summary of the info presented in the link you provided). Link only answers become less useful over time as links often break as time goes on. – Phil Greer Jul 26 '14 at 14:32
0

I have discovered the solution to this problem. If I add $outWeb.AllowUnsafeUpdates = $true before the $outWeb.Lists.Add() call, the list gets created correctly and completely. I also set this property back to $false immediately after the list creation.

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.