Wednesday, March 10, 2010

Archive SharePoint Document Library versions.

Requirement : Archive versions created in a document library to another document library on daily basis.

Study :  There are vairuos option to try doing it, but to automate the process its better to go fir shareoint timer jobs.

Solution : Create timer jobs which starts on feature activation performing the requirement on daily basis.

  1.  Create Timer job in a feature.
         You can start creating a timer job and attaching it with feature from the below site steps.
           http://www.andrewconnell.com/blog/archive/2007/01/10/5704.aspx
           http://www.andrewconnell.com/blog/articles/CreatingCustomSharePointTimerJobs.aspx

        Only difference from the example is that you have to use SPDailySchedule
         http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spdailyschedule.aspx

   2.  Write code for perform versioning in the document library

       public override void Execute (Guid contentDbId) {
       try
           {   
             string[] urlofFile = null;
             string url = string.Empty;
             byte[] dBytes = null;
             StringBuilder verID = new StringBuilder(10);
             SPFileVersionCollection vercol = null;
             SPFile file = null;
         //Call webapplication
                   SPWebApplication webApplication = this.Parent as SPWebApplication;
                  SPContentDatabase contentDataBase = webApplication.ContentDatabases  [contentDBGuid];
          //call document librarys
   SPDocumentLibrary doclib1 = (SPDocumentLibrary)contentDataBase.Sites[0].RootWeb.Lists ["DocLib"];
   //Archival doc library
SPDocumentLibrary doclib2 = (SPDocumentLibrary)contentDataBase.Sites[0].RootWeb.Lists["Archives"];
//get current web
SPWeb currentweb = contentDataBase.Sites[0].RootWeb;
//get filecollection.
SPFileCollection filecols = currentweb.GetFolder("/DocLib").Files;
//Call Spfolder to add items to the archive.
SPFolder doclib2Folder = currentweb.Folders["/Archives"];
foreach (SPListItem items in doclib.Items){
//file object to get the files stored in doclib
file = items.File;
if (file.Exists){
string fileurl = file.Url.ToString();
//get collection of fileversions.
vercol = filecols[fileurl].Versions;
//Add the versions to Archive documents library if not a current version.
foreach (SPFileVersion ver in vercol){
if (!ver.IsCurrentVersion){
verID = verID.Append(ver.ID);
verID = verID.Append(",");
urlofFile = ver.File.Name.Split('.');
//Add version ID of the file version for uniqueness
url = urlofFile[0].ToString() + ver.ID + "." + urlofFile[1].ToString();
//get the versions of the file.
dBytes = ver.OpenBinary();
//Add it to Archive doclibrary
currentweb.AllowUnsafeUpdates = true;
doclib2Folder.Files.Add(url, dBytes);
doclib2.Update();
currentweb.AllowUnsafeUpdates = false;
}}
//Delete the versions from the current document library
if (!string.IsNullOrEmpty(verID.ToString())){
string[] vervalues = verID.ToString().TrimEnd(',').Split(',');
int i = 0;
foreach (string s in vervalues){
currentweb.AllowUnsafeUpdates = true;
i = Convert.ToInt32(s);
vercol.GetVersionFromID(i).Delete();
currentweb.AllowUnsafeUpdates = false;
}verID.Length = 0;
}}}}
catch (Exception ex)
{//Handle Exceptions....}
}

3. Deploy the solution as a feauture to your shareoint solution and activate the feature.

Happy Coding...........

4 comments:

Ajith George said...

http://www.andrewconnell.com/blog/archive/2008/06/11/SharePoint-Debugging-and-Logging-Tips-and-Tricks.aspx

SharePoint Consulting said...

This is a really good read for me. Must admit that you are one of the best bloggers I ever saw. Thanks for posting this useful article.

SharePoint Consulting said...

This is a really good read for me. Must admit that you are one of the best bloggers I ever saw. Thanks for posting this useful article.

Sharepoint 2010 Archiving said...

Thanks for this version detials