Domanda

Simple question.

Is there a way to move a file without changing its "Modified" date field?

I'm using SPfile.MoveTo to move the files around, and I wanted to move them in a way that it wouldn't count as Modified.

I'm moving via code some old files from my libraries(1 year old without any modifications) to a "dead" library and I didn't want to change their modified date.

È stato utile?

Soluzione

You could utilize Content Deployment and Migration API for that purpose. The following example demonstrates how to implement archival process for List Items using Content Deployment and Migration API:

class DocumentArchiver
{

    public void Process(SPListItem sourceListItem,SPFolder targetFolder)
    {
        this.targetFolder = targetFolder;
        ExportListItem(sourceListItem);   
    }


    void ExportListItem(SPListItem listItem)
    {
        var exportObject = new SPExportObject { Id = listItem.UniqueId, Type = SPDeploymentObjectType.ListItem };

        var exportSettings = new SPExportSettings();
        exportSettings.ExportObjects.Add(exportObject);
        exportSettings.FileLocation = Location;
        exportSettings.FileCompression = false;
        exportSettings.SiteUrl = listItem.Web.Url;

        var export = new SPExport(exportSettings);
        export.Completed += OnExported;
        export.Run();
    }


    public void Import()
    {
        var importSettings = new SPImportSettings
        {
            SiteUrl = targetFolder.ParentWeb.Url,
            FileLocation = Location,
            FileCompression = false,
            RetainObjectIdentity = false,
            UserInfoDateTime = SPImportUserInfoDateTimeOption.ImportAll //Retains the original author/editor information, time/date stamp, and user lookup value
        };



        var import = new SPImport(importSettings);
        import.Started += OnImportStarted;
        import.ObjectImported += OnImported;
        import.Run();
    }

    void OnExported(object sender, SPDeploymentEventArgs e)
    {
        Import();
    }

    void OnImported(object sender, SPObjectImportedEventArgs e)
    {

    }

    void OnImportStarted(object sender, SPDeploymentEventArgs args)
    {
        SPImportObjectCollection rootObjects = args.RootObjects;
        foreach (SPImportObject io in rootObjects)
        {
            io.TargetParentUrl = targetFolder.ServerRelativeUrl;
        }
    }

    private const string Location = @"d:\export";

    private SPFolder targetFolder;

}  

Usage

using (var site = new SPSite(siteUrl))
{
     using (var web = site.OpenWeb())
     {
         var sourceList = web.Lists.TryGetList(sourceListTitle);
         var sourceItem = sourceList.GetItemById(sourceItemId);
         var targetFolder = web.GetFolder(targetUrl);


         var archiver = new DocumentArchiver();
         archiver.Process(sourceItem, targetFolder);

      }
 }

References

Altri suggerimenti

Before moving get the Modified User Object and then Move and then set the Modified field.

http://sharepointvenividivici.typepad.com/sharepoint-customization/2011/06/maintain-file-version-history-when-movingcopying-files-between-sharepoint-sites.html

http://www.3guysonsharepoint.com/?p=1390

SPFile fileSource = itmSource.File;
/*Here we'll get the created by and created on values from the source document.*/
SPUser userCreatedBy = fileSource.Author;
/*Note we need to convert the "TimeCreated" property to local time as it's stored in the database as GMT.*/
DateTime dateCreatedOn = fileSource.TimeCreated.ToLocalTime();
//Get the versions
int countVersions = itmSource.File.Versions.Count;
/*This is a zero based array and so normally you'd use the < not <= but we need to get the current version too which is not in the SPFileVersionCollection so we're going to count one higher to accomplish that.*/
for (int i = 0; i <= countVersions; i++)
{
     Hashtable hashSourceProp;
     Stream streamFile;
     SPUser userModifiedBy;
     DateTime dateModifiedOn;
     string strVerComment = "";
     bool bolMajorVer = false;
     if (i < countVersions)
     {
/*This section captures all the versions of the document and gathers the properties we need to add to the SPFileCollection.  Note we're getting the modified information and the comments seperately as well as checking if the version is a major version (more on that later).  I'm also getting a stream object to the file which is more efficient than getting a byte array for large files but you could obviously do that as well.  Again note I'm converting the created time to local time.*/
          SPFileVersion fileSourceVer = itmSource.File.Versions[i];
          hashSourceProp = fileSourceVer.Properties;
          userModifiedBy = (i == 0) ? userCreatedBy: fileSourceVer.CreatedBy;
          dateModifiedOn = fileSourceVer.Created.ToLocalTime();
          strVerComment = fileSourceVer.CheckInComment;
          bolMajorVer = fileSourceVer.VersionLabel.EndsWith("0") ? true : false;
          streamFile = fileSourceVer.OpenBinaryStream();
     }
     else
     {
/*Here I'm getting the information for the current version.  Unlike in SPFileVersion when I get the modified date from SPFile it's already in local time.*/
          userModifiedBy = fileSource.ModifiedBy;
          dateModifiedOn = fileSource.TimeLastModified;
          hashSourceProp = fileSource.Properties;
          strVerComment = fileSource.CheckInComment;
          bolMajorVer = fileSource.MinorVersion == 0 ? true : false;
          streamFile = fileSource.OpenBinaryStream();
     }
     string urlDestFile = libDest.RootFolder.Url + "/" + fileSource.Name;
/*Here I'm using the overloaded Add method to add the file to the SPFileCollection.  Even though this overload takes the created and modified dates for some reason they aren't visible in the SharePoint UI version history which shows the date/time the file was added instead, however if this were a Microsoft Word document and I opened it in Word 2010 and looked at the version history it would all be reflective of the values passed to this Add method.  I'm voting for defect but there could just be something I'm missing.*/
     SPFile fileDest = libDest.RootFolder.Files.Add(
                 urlDestFile, 
                 streamFile, 
                 hashSourceProp, 
                 userCreatedBy, 
                 userModifiedBy, 
                 dateCreatedOn, 
                 dateModifiedOn, 
                 strVerComment, 
                 true);
     if (bolMajorVer)
/*Here we're checking if this is a major version and calling the publish method, passing in the check-in comments.  Oddly when the publish method is called the passed created and modified dates are displayed in the SharePoint UI properly without further adjustment.*/
          fileDest.Publish(strVerComment);
     else
     {
/*Setting the created and modified dates in the SPListItem which corrects the display in the SharePoint UI version history for the draft versions.*/
          SPListItem itmNewVersion = fileDest.Item;
          itmNewVersion["Created"] = dateCreatedOn;
          itmNewVersion["Modified"] = dateModifiedOn;
          itmNewVersion.UpdateOverwriteVersion();
     }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange
scroll top