{"id":767,"date":"2012-02-09T19:57:42","date_gmt":"2012-02-09T19:57:42","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=767"},"modified":"2012-02-10T22:00:06","modified_gmt":"2012-02-10T22:00:06","slug":"get-listtemplate-create-list-powershell","status":"publish","type":"post","link":"http:\/\/www.stuartroberts.net\/index.php\/2012\/02\/09\/get-listtemplate-create-list-powershell\/","title":{"rendered":"Get ListTemplate and Create New List in PowerShell"},"content":{"rendered":"<p>A <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/dd835506(v=vs.85).aspx\" title=\"PowerShell\" target=\"_blank\">PowerShell<\/a> related post today:<\/p>\n<p>To create a list or library in SharePoint, you can do the following:<\/p>\n<pre lang=\"powershell\">\r\n$web = Get-SPWeb \"http:\/\/serverUrl\"\r\n$web.Lists.Add(\"List Title\", \"List description\", \"Template Name\")\r\n<\/pre>\n<p>Now, say you were creating lists based on the schema from another list:<\/p>\n<pre lang=\"powershell\">\r\n$web = Get-SPWeb \"http:\/\/anotherServerUrl\"\r\n$list = $eb.Lists.get_Item(\"List name\")\r\n$listSchema = $list.SchemaXml\r\n<\/pre>\n<p>The SchemaXml from a list only contains the ServerTemplate numeric value, which can not be passed to the Add method in a PowerShell script.<br \/>\n<!--more--><\/p>\n<pre lang=\"xml\">\r\n<List ID=\"...\" ServerTemplate=\"100\" Title=\"...\">\r\n    <Fields\/>\r\n<\/List>\r\n<\/pre>\n<p>Unlike adding a list using C#, where you can get the SPListTemplateType from the ServerTemplate value:<\/p>\n<pre lang=\"csharp\">\r\nSPListTemplateType templateType = (SPListTemplateType)100; \/\/ 100 is Generic List\r\n<\/pre>\n<p>You can&#8217;t pass the numeric value of the list template into the Add method.  If you know the template value it&#8217;s easy to get the matching object by using the following script:<\/p>\n<pre lang=\"powershell\">\r\n$web = Get-SPWeb \"http:\/\/serverUrl\"\r\n$template = $web.ListTemplates | Where-Object {$_.Type -eq 100}\r\n# Now add the list using the retrieved template object\r\n$web.Lists.Add(\"List Title\", \"List description\", $template)\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A PowerShell related post today: To create a list or library in SharePoint, you can do the following: $web = Get-SPWeb &#8220;http:\/\/serverUrl&#8221; $web.Lists.Add(&#8220;List Title&#8221;, &#8220;List description&#8221;, &#8220;Template Name&#8221;) Now, say you were creating lists based on the schema from another &hellip; <a href=\"http:\/\/www.stuartroberts.net\/index.php\/2012\/02\/09\/get-listtemplate-create-list-powershell\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":[]},"categories":[3],"tags":[41,81],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-cn","_links":{"self":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/767"}],"collection":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=767"}],"version-history":[{"count":11,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/767\/revisions"}],"predecessor-version":[{"id":824,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/767\/revisions\/824"}],"wp:attachment":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=767"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}