none
"Copy List Item with version history" workflow task across site collections? RRS feed

  • Question

  • Hi,

         How to copy items with current version using visual studio custom workflow likes State Machine or Sequential workflow (Not use in SharePoint Designer Workflow) from one document library folder to cross site collection document library folder in SharePoint 2010. This is possible or not.

    Note :- Versions History are mandatory for source to destination folder in cross site. 



    Thanks Mukesh



    Monday, October 15, 2018 11:35 AM

All replies

  • Hi Mukesh,

    Please follow the article below to create a visual studio workflow in SharePoint 2010.

    https://www.codeproject.com/Articles/416393/Create-a-Workflow-using-Visual-Studio-2010

    And copy folder with files(version history) from one site collection to another using the C# code below.

    string sourceSiteUrl = "http://sp2010/sites/team";
    string targetSiteUrl = "http://sp2010/sites/team1";
    string sourceLibrary = "DL/Folder1";
    string targetLibrary = "DL/Folder1";
    string destLib = "DL";
    
    SPSite sourceSite = new SPSite(sourceSiteUrl);
    SPWeb sourceWeb = sourceSite.RootWeb;
    SPSite targetSite = new SPSite(targetSiteUrl);
    SPWeb targetWeb = targetSite.RootWeb;
    
    SPFolder sourceFolder = sourceWeb.GetFolder(sourceLibrary);
    SPFolder targetFolder = targetWeb.GetFolder(targetLibrary);
    SPDocumentLibrary libDest = (SPDocumentLibrary)targetWeb.Lists[destLib];
    
    foreach (SPFile fileSource in sourceFolder.Files)
    {               
    	SPUser userCreatedBy = fileSource.Author;
    	DateTime dateCreatedOn = fileSource.TimeCreated.ToLocalTime();
    
    	//Get the versions
    	int countVersions = fileSource.Versions.Count;
    
    	for (int i = 0; i <= countVersions; i++)
    	{
    		Hashtable hashSourceProp;
    		Stream streamFile;
    		SPUser userModifiedBy;
    		DateTime dateModifiedOn;
    		string strVerComment = "";
    		bool bolMajorVer = false;
    
    		if (i < countVersions)
    		{
    			SPFileVersion fileSourceVer = fileSource.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
    		{
    			userModifiedBy = fileSource.ModifiedBy;
    
    			//I modify the modified time for the current version                       
    			dateModifiedOn = fileSource.TimeLastModified.ToLocalTime();
    
    			hashSourceProp = fileSource.Properties;
    			strVerComment = fileSource.CheckInComment;
    			bolMajorVer = fileSource.MinorVersion == 0 ? true : false;
    			streamFile = fileSource.OpenBinaryStream();
    		}
    
    		string urlDestFile = targetFolder.Url + "/" + fileSource.Name;
    
    		SPFile fileDest = targetFolder.Files.Add(urlDestFile,streamFile,true);
    
    		if (bolMajorVer)
    		{
    			libDest.EnableMinorVersions = true;
    			libDest.Update();
    
    			fileDest.Publish(strVerComment);
    
    			libDest.EnableMinorVersions = false;
    			libDest.DraftVersionVisibility = Microsoft.SharePoint.DraftVisibilityType.Reader;
    			libDest.Update();
    
    			fileDest.Item["Created"] = dateCreatedOn;
    			//fileDest.Item["Created By"] = userCreatedBy;
    			fileDest.Item["Modified"] = dateModifiedOn;
    			//fileDest.Item["Modified By"] = userModifiedBy;                        
    			fileDest.Item.UpdateOverwriteVersion();			
    		}
    		else
    		{
    			SPListItem itmNewVersion = fileDest.Item;
    			itmNewVersion["Created"] = dateCreatedOn;
    			//itmNewVersion["Created By"] = userCreatedBy;
    			itmNewVersion["Modified"] = dateModifiedOn;
    			//itmNewVersion["Modified By"] = userModifiedBy;                      
    			itmNewVersion.SystemUpdate();
    		}
    	}
    }

    If you want to run the code daily, we can create a console application and using windows job to achieve it. Or create a SharePoint timer job.

    https://www.c-sharpcorner.com/uploadfile/anavijai/custom-timer-job-in-sharepoint-2010/

    Best Regards,

    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Tuesday, October 16, 2018 8:10 AM
    Moderator
  • Hi Mukesh,

    Would you please provide us with an update on the status of your issue?

    Best Regards,
    Dennis


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Monday, October 22, 2018 2:17 AM
    Moderator
  • Hi Mukesh,

    Any update?

    If my reply helps you, please mark the reply as answer, it will make others who stuck with the similar issue easier to search for valid solutions in this forum.

    Best regards,
    Dennis 


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.


    Click here to learn more. Visit the dedicated forum to share, explore and talk to experts about Microsoft Teams.

    Tuesday, October 30, 2018 7:00 AM
    Moderator