Home > SharePoint > Copy a document library item in Moss 2007 maintaining the version

Copy a document library item in Moss 2007 maintaining the version

2011/07/28

A customer has requested to have a Document library , and a second Document library identical to the first, that must be filled every time that in the first library is added or updated a document.

The reason of this strange request is that the second library must be visible only to specific users.

In a first time i was thinking immediately to a workflow, and i have implemented a workflow on the first list.

The first try was the simple Create List Item as Action in the workflow:


But the copied document in the History list was not readable if saved as docx , and saved as .doc was always empty ! a Moss bug ? I will try on MOSS 2010.

So i have tried with copy item


It worked, but some metadata of the Document library was not correctly copied to the history list (i have tried on my vm and there was not this problem); but apart this the true problem is that the copied document was always a 0.1 version: this because is the version of the history list, and generating a record at every update in the main list the version starts always from beginning.

So i ended up by writing a custom event handler with Visual Studio 2008 , this is the main code:

public override void ItemUpdated(SPItemEventProperties properties)
{
   if (properties.ListTitle.ToLower() == "corporate identity")
   {
      // copy listitem in history
      string strSrcDocLib = properties.WebUrl + "/CorporateIdentity"; 
      string strDestDocLib = properties.WebUrl + "/CorporateIdentityHistory"; 
      using (SPSite objSite = new SPSite(properties.WebUrl)) 
      { 
          using (SPWeb objWeb = objSite.OpenWeb()) 
          {
             SPListItem objSrcListItem = objWeb.GetListItem(properties.WebUrl + "/" + properties.AfterUrl);
             SPDocumentLibrary objSourceDocLib = objWeb.GetList(strSrcDocLib) as SPDocumentLibrary; 
             SPDocumentLibrary objDestDocLib = objWeb.GetList(strDestDocLib) as SPDocumentLibrary; 
             SPFile objDocFileItem = objSrcListItem.File; 
             SPFile objDestFile = null; 
             // copy the document
             using (Stream objStream = objDocFileItem.OpenBinaryStream()) 
             {
                string strFileName = Path.GetFileNameWithoutExtension(objSrcListItem.Name) + " " + objDocFileItem.UIVersionLabel + Path.GetExtension(objSrcListItem.Name);
                objDestFile = objDestDocLib.RootFolder.Files.Add(strFileName , objStream); 
                // is needed to generate another name else is found the existent and nothing is done
                objStream.Close(); 
             } 
            // and the metadata
             SPListItem objDestDocItem = objDestFile.Item; 
             objDestDocItem["Author"] = objSrcListItem["Author"];
             objDestDocItem["BA"] = objSrcListItem["BA"];
             objDestDocItem["Date of editing"] = objSrcListItem["Date of editing"];
             objDestDocItem["Description"] = objSrcListItem["Description"];
             objDestDocItem["Functional Target"] = objSrcListItem["Functional Target"];
             objDestDocItem["Geo"] = objSrcListItem["Geo"];
             objDestDocItem["Tag"] = objSrcListItem["Tag"];
             objDestDocItem["Title"] = objSrcListItem["Title"] + " " + objDocFileItem.UIVersionLabel;
             objDestDocItem["Typology"] = objSrcListItem["Typology"];
             objDestDocItem["Year of editing"] = objSrcListItem["Year of editing"];
             objDestDocItem.Update();
          } 
      }
   }
}

Categories: SharePoint