In a previous article I described how to clone a list in SharePoint using a power shell script. If, however, you want to copy the list to another SharePoint site or site collection you will require a different script. The following script will do just that.
# get source web $sourceWeb = Get-SPWeb http://SourceSiteURL # get source list $sourcelist = $sourceWeb.Lists["SourceList"] #get destinatin web $destinationWeb = Get-SPWeb http://DestSiteURL #save source list as template $sourcelist.SaveAsTemplate($sourcelist.ID.ToString(), $sourcelist.ID.ToString(), "", $false) # get template file $templateFile = $sourceWeb.GetFile($sourceWeb.ServerRelativeUrl+ "/_catalogs/lt/" + $sourcelist.ID.ToString() + ".stp"); # get destination folder (list template folder in destination web) $destinationFolder = $destinationWeb.GetFolder($destinationWeb.ServerRelativeUrl+ "/_catalogs/lt") # add template file to destination folder $destinationFolder.Files.Add($sourcelist.ID.ToString() + ".stp", $templateFile.OpenBinary()) # get custom list template $template = $destinationWeb.Site.GetCustomListTemplates($destinationWeb)[$sourcelist.ID.ToString()] # create list from template $destinationWeb.Lists.Add($sourcelist.Title, $sourcelist.Description, $template);
# get source web $sourceWeb = Get-SPWeb http://SourceSiteURL # get source list $sourcelist = $sourceWeb.Lists["SourceList"] #get destinatin web $destinationWeb = Get-SPWeb http://DestSiteURL #save source list as template $sourcelist.SaveAsTemplate($sourcelist.ID.ToString(), $sourcelist.ID.ToString(), "", $false) # get template file $templateFile = $sourceWeb.GetFile($sourceWeb.ServerRelativeUrl+ "/_catalogs/lt/" + $sourcelist.ID.ToString() + ".stp"); # get destination folder (list template folder in destination web) $destinationFolder = $destinationWeb.GetFolder($destinationWeb.ServerRelativeUrl+ "/_catalogs/lt") # add template file to destination folder $destinationFolder.Files.Add($sourcelist.ID.ToString() + ".stp", $templateFile.OpenBinary()) # get custom list template $template = $destinationWeb.Site.GetCustomListTemplates($destinationWeb)[$sourcelist.ID.ToString()] # create list from template $destinationWeb.Lists.Add($sourcelist.Title, $sourcelist.Description, $template);
Simply change SourceSiteURL, SourceList and DestSiteURL to the URL of your source site, the source list and the URL of your destination site respectively.
I hope you found this guide useful. If you have any problems or any other questions, please send an e-mail to [email protected] or post a comment.