Sign In
dwinter's [MSFT] WebLog
SharePoint Program Manager
Translate This Page
Translate this page
Powered by
Microsoft® Translator
Options
Blog Home
About
Email Blog Author
Share this
RSS for posts
Atom
RSS for comments
Search
Advanced search options...
Search In:
Everything
Blogs
Forums
People
Groups
Places
Pages
Date range:
All Time
Last Year
Last 6 Months
Last 3 Months
Last Month
Last Week
Last Two Days
Tags
Active Directory
DST2007
Exchange Server
General OM
Introducing...
Pages
Patching
PowerShell
Product Support
SharePoint 2010
Site Definitions
SPAT
SPC2008
SPModule
Tool Release
ULSViewer
Web Services
WikiMigrator
Archive
Archives
December 2009
(1)
August 2009
(1)
May 2009
(1)
December 2008
(1)
November 2008
(1)
September 2008
(2)
August 2008
(1)
July 2008
(4)
June 2008
(10)
March 2008
(2)
December 2007
(1)
June 2007
(2)
March 2007
(1)
December 2006
(2)
July 2006
(1)
June 2006
(1)
January 2006
(1)
May 2005
(1)
March 2005
(5)
February 2005
(10)
July 2004
(1)
How to reprovision a document library programatically (simple example)
MSDN Blogs
>
dwinter's [MSFT] WebLog
>
How to reprovision a document library programatically (simple example)
How to reprovision a document library programatically (simple example)
Dan Winter - MSFT
14 Mar 2005 11:15 PM
Comments
1
I have gotten a lot of questions recently around the topic of reprovisioning. I wanted to show a simple example to get folks started. Reprovisioning will be a topic of interest during the up and comming site definitions posts that I'm working on putting together. I am still holding on those for now, but decided that this code sample would be worth showing.
This sample does retain versions if they are enabled.
It does not handle non-default views or customized fields. As such, while you would retain versions, you would not retain custom metadata. You would need the fields to be defined in the new list objects for that to be possible. You also would want to verify metadata/etc. I will cover some of this in another post--remember this is just a simple example. You would need to handle a lot of these items if you were operating against a document library that is far customized from default. Also note that none of the defaults are carried over other then versioning.
I am using moveto instead of copyto because copyto does not retain versions. It only takes the most recent object.
// These strings would match your server, site and list name...
SPSite mySite =
new
SPSite("
http://dwinter
");
SPWeb myWeb = mySite.OpenWeb("/sites/test");
SPList myList = myWeb.Lists["test1234"];
string
docLibName = myList.Title;
string
docLibTemplateName = myList.BaseTemplate.ToString().Insert(myList.BaseTemplate.ToString().IndexOf("Library")," ");
SPListCollection lc = myWeb.Lists;
SPListTemplate lt = myWeb.ListTemplates[docLibTemplateName];
// Create temporary list
System.Guid newDocLibGuid = lc.Add(docLibName+"TEMP",docLibName+"TEMP", lt);
SPList newList = ((SPDocumentLibrary)(myWeb.Lists[newDocLibGuid]));
try
{newList.EnableVersioning = myList.EnableVersioning;}
// Lazy Try/Catch, write some error handling here
catch
{}
// Move all items (including version info) from original to temporary list
foreach
(SPListItem mySPListItem
in
myList.Items)
{
mySPListItem.File.MoveTo(newList.RootFolder.Url+"/"+mySPListItem.File.Name);
}
// Remove original list
lc.Delete(myList.ID);
// Recreate original list as a new list
System.Guid newDocLibGuid2 = lc.Add(docLibName,docLibName, lt);
SPList newList2 = ((SPDocumentLibrary)(myWeb.Lists[newDocLibGuid2]));
SPList myList2 = myWeb.Lists.GetList(newDocLibGuid,
false
);
try
{newList2.EnableVersioning = myList2.EnableVersioning;}
// Lazy Try/Catch, write some error handling here
catch
{}
// Move all items (including version info) from temporary to new list
foreach
(SPListItem mySPListItem2
in
myList2.Items)
{
mySPListItem2.File.MoveTo(newList2.RootFolder.Url+"/"+mySPListItem2.File.Name);
}
// Remove temporary list
lc.Delete(newDocLibGuid);
So if you were to use this, make sure you add some logging/control. This is the kind of code to play with, not the kind to drop into anything close to production.
1 Comments
General OM
Blog - Comment List MSDN TechNet
Comments
Loading...
Leave a Comment
Name
Comment
Please add 2 and 2 and type the answer here:
Post