views:

826

answers:

3
+1  Q: 

Duplicate a list

Is there a way to easily duplicate a list code wise?

A: 

Are you looking for this?

ArrayList a = new ArrayList();
ArrayList b = new ArrayList();

b.AddRange(a);

Just substitute the blank list "a" for your actual list that you want to copy

scunliffe
I think he's talking about SharePoint lists, not ArrayLists.
Flo
+5  A: 

Judging from the SharePoint tag, I'm assuming you meant to ask "How do I copy a SharePoint list (SPList) programmatically?"
Without testing it (or even trying to compile it), I would do something like:

SPWeb web; /* add code to initialize to current web */
SPList sourceList = web.Lists["nameOfSourceList"];
sourceList.SaveAsTemplate("templateFNM.stp", "tempList", "tempListDescription", 
                          true /* include list content */);
SPListTemplate template = web.ListTemplates["tempList"];
web.Lists.Add("newListName", "newListDescription", template);
web.Update();
SPList newList = web.Lists["newListName"];

Also, here's a link to a blog post that achieves the same accross web applications.

And finally a word of advice: you'll get better search results if you use "programmatically" instead of "code wise".

Hope this helps.

vitule
A: 

Hi,

I am trying the same code but facing some problem.

When I save the list as tempate it comes under the folder _catalog\lt at the root site, but I am not able to find the same programatically.

Can you help me on this.

SPSite siteColl = new SPSite(SPContext.Current.Web.Site.ID); if (siteColl.RootWeb.GetFile("_catalogs/lt/" + "DecisionTaskTemplate.stp").Exists) { siteColl.RootWeb.Lists [GlobalResources.Resources.ListTemplateGalleryName].RootFolder.Files[ "DecisionTaskTemplate.stp"].Recycle(); } SPContext.Current.Web.Lists["Decision Task " + cashedFile.Name.Replace(".aspx","")].SaveAsTemplate("DecisionTaskTemplate.stp","DecisionTaskTemplate", "Template", true); SPContext.Current.Web.Lists["Decision Task " + cashedFile.Name.Replace(".aspx", "")].Update(true); SPContext.Current.Web.Update(); } } SPSite globalSiteColl = new SPSite(SPContext.Current.Web.Site.ID);

foreach (SPListTemplate listTemplate in globalSiteColl.GetCustomListTemplates(globalSiteColl.RootWeb)) { if (listTemplate.Name.Equals("DecisionTaskTemplate.stp")) { SPContext.Current.Web.Lists.Add("Decision Task " + addedFile.Name.Replace(".aspx", ""), "Decision Task " + addedFile.Name.Replace(".aspx", ""), listTemplate); } }