SharePoint Workflow - Move Documents

Hi all

I am trying to move a document when a workflow is run, which physically moves a document from one document library to another. I am aware that this can be achieved by getting the workflow to copy list item and then delete from existing location. However, the problem with this is the version history is not maintained.

I have looked into alternative options like the Drop Off library, setting Content Organizer rules, and getting a workflow to feed a document into the Drop Off Library, however I get an error whenever the workflow is run.

Any ideas, how I could implement this, without code?! :-)

Thanks in advance

April 24th, 2015 11:06am

You can use REST API call from SP Designer workflow..here is an example.

Free Windows Admin Tool Kit Click here and download it now
April 24th, 2015 2:01pm

Thanks Suravijit. Looking at that tutorial, it doesn't break down how exactly the workflow was created in SPD.

For example, Build (...) Dictionary (Output:variable.........

None of these are options I can find in the ribbon on SPD. Apologies, like these workflows are a little out of my depth!

Thanks in advance

April 27th, 2015 4:41am

Hi Red_Nation,

Did you use Send Document to Repository action in the workflow to move the document to Drop Off library?

If yes, the action will not move the documents without the version history to the Drop Off library as my test.

As Suravijit suggested, you can use Call HTTP Web Server and Build Dictionary actions in 2013 workflow platform to move the documents with version history to another library.

To be able to use 2013 workflow platform in SharePoint Designer, you need to install workflow manager.

https://technet.microsoft.com/en-us/library/jj658588.aspx?f=255&MSPPError=-2147217396

After that, you can use the two actions to achieve your goal.

Thanks,

Victoria

Free Windows Admin Tool Kit Click here and download it now
April 28th, 2015 12:27am

Hi Victoria

I will install and configure Workflow manager and feedback.

Thank you

April 28th, 2015 8:15am

Hi Red_Nation,

Hopefully after installing workflow manager you are able see all the activities needed for SharePoint Designer workflow. 

One quick question, if you have on-premise SharePoint then why not using eventreceiver for moving documents within different libraries?

I found the code snippet here. This snippet is in a function that takes two parameters SPListItem itmSource which is the source item to move, and SPDocumentLibrary libDest which is the target document library.

void MoveFile(SPListItem itmSource, SPDocumentLibrary libDest)
        {
            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();
                }
            }
        }
Hope this will help you.

Free Windows Admin Tool Kit Click here and download it now
April 28th, 2015 2:35pm

Thanks Avijit. This is most helpful.

I am not a developer so event receivers and the like are a little beyond me at the moment. I have an on-premise SP environment. How would I go about implementing this code into my environment and then in turn, testing it?

Thanks in advance

April 30th, 2015 11:41am

Allow me for a day and I'll provide you the code as well as wsp to deploy.
Free Windows Admin Tool Kit Click here and download it now
April 30th, 2015 12:17pm

Wow, very helpful! Thanks!! :-)
April 30th, 2015 1:06pm

Please go to the following link and download the wsp and source code...

https://onedrive.live.com/embed?cid=E5667EFD4B1314F3&resid=e5667efd4b1314f3%212770&authkey=AGXeUIMgXn1y7ig

I have developed one custom sharepoint designer activity which you can use to copy documents with version history from one doc lib to another within the same site.

Deployment Steps:

1. Deploy the wsp

2. Add reference in web.config

  • Back up your web.config file. If you misconfigure the web.config file, your SharePoint site will not function until you correct the file. The default location of the web.config file isC:\inetpub\wwwroot\wss\VirtualDirectories\portnumber\web.config, where portnumber is the port number of your SharePoint web application.
  • Open the web.config file.
  • Locate the tag named <System.Workflow.ComponentModel.WorkflowCompiler>.
  • Inside the tag named <authorizedTypes>, add a tag named <authorizedType> with the following attributes:
<authorizedType Assembly="acs.sharepoint.workflowactions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=25e2ea93e77f7dff" Namespace="acs.sharepoint.workflowactions" TypeName="*" Authorized="True" />
  • Save the web.config file.

  • Than Restart IIS. Open run and just write IISreset and Ok.

Now Open Sharepoint Designer 2010 & Go to workflow

1. See the new activity under Core Actions

2. Provide destination library name in the action.

Make sure both the libraries are within the same site and version enabled.

Hope this will help you...cheers!!!

Free Windows Admin Tool Kit Click here and download it now
May 1st, 2015 11:00am

Please go to the following link and download the wsp and source code...

https://onedrive.live.com/embed?cid=E5667EFD4B1314F3&resid=e5667efd4b1314f3%212770&authkey=AGXeUIMgXn1y7ig

I have developed one custom sharepoint designer activity which you can use to copy documents with version history from one doc lib to another within the same site.

Deployment Steps:

1. Deploy the wsp

2. Add reference in web.config

  • Back up your web.config file. If you misconfigure the web.config file, your SharePoint site will not function until you correct the file. The default location of the web.config file isC:\inetpub\wwwroot\wss\VirtualDirectories\portnumber\web.config, where portnumber is the port number of your SharePoint web application.
  • Open the web.config file.
  • Locate the tag named <System.Workflow.ComponentModel.WorkflowCompiler>.
  • Inside the tag named <authorizedTypes>, add a tag named <authorizedType> with the following attributes:
<authorizedType Assembly="acs.sharepoint.workflowactions, Version=1.0.0.0, Culture=neutral, PublicKeyToken=25e2ea93e77f7dff" Namespace="acs.sharepoint.workflowactions" TypeName="*" Authorized="True" />
  • Save the web.config file.

  • Than Restart IIS. Open run and just write IISreset and Ok.

Now Open Sharepoint Designer 2010 & Go to workflow

1. See the new activity under Core Actions

2. Provide destination library name in the action.

Make sure both the libraries are within the same site and version enabled.

Hope this will help you...cheers!!!

  • Marked as answer by Red_Nation Monday, May 11, 2015 3:54 PM
May 1st, 2015 11:00am

Avijit

That is super! Thanks ever so much :-)

This looks like its the sort of thing I would want to try on a dev environment first before delving straight into production. The organisation I am working in only has prod at the moment (crazy I know), so will have a go when the dev environment is setup.

Thanks again! 

Free Windows Admin Tool Kit Click here and download it now
May 1st, 2015 12:23pm

Do they support Sandbox solutions? We can convert this to SandBox solution and then you do not need a dev  environment.
May 1st, 2015 1:15pm

Hi Avijit

At the risk of sounding silly, how can I check if we support Sandbox solutions?

Many thanks
Free Windows Admin Tool Kit Click here and download it now
May 5th, 2015 4:16am

Hi Red_Nation,

Please start Microsoft SharePoint Foundation Sandboxed Code Service in Manage service on server in Central Administration.

After that, the sandbox solution is supported in SharePoint.

Thanks,

Victoria

May 6th, 2015 2:00am

Hi Victoria

Thanks for that. I have switched on this service in CA now. Is there any measures I can take to ensure the area I am working in now is a sandbox solution? Or should I create a new site collection and tweak the settings?

Thanks in advance

Free Windows Admin Tool Kit Click here and download it now
May 6th, 2015 8:06am

Hi Red_Nation,

What did you mean by "ensure the area I am working in now is a sandbox solution"?

Sandboxed solutions, which are hosted in the SharePoint user code solution worker process (SPUCWorkerProcess.exe), run code that can only affect the site collection of the solution.

It depends on which site collection you deploy the sandbox solution that can work with it.

More references:

https://msdn.microsoft.com/en-us/library/ee361616.aspx

https://msdn.microsoft.com/en-us/library/office/gg615450(v=office.14).aspx

Thanks,

Victoria

May 7th, 2015 1:40am

Thanks for the reassurance Victoria. I just assumed (perhaps wrongly) that a given site collection needs to be setup to support sandbox solutions rather than deploying applications directly onto the site collection.

Thanks again.

 
Free Windows Admin Tool Kit Click here and download it now
May 7th, 2015 8:04am

I tried uploading Avijit's wsp file as a sandboxed solution, however upon Activating, I receive an error. I am wondering if there is a process to convert this solution into a sandboxed one?

Thanks

May 7th, 2015 9:26am

The solution provided is not a Sandbox solution. I am working on converting the existing solution into a sandbox one and update you in couple of days. Sorry for keep you waiting but busy with regular jobs :(.

Meanwhile if you want, you can test the existing wsp during the weekend with proper mitigation plan.

  • Edited by Avijit Sur Thursday, May 07, 2015 10:54 AM update
Free Windows Admin Tool Kit Click here and download it now
May 7th, 2015 10:51am

The solution provided is not a Sandbox solution. I am working on converting the existing solution into a sandbox one and update you in couple of days. Sorry for keep you waiting but busy with regular jobs :(.

Meanwhile if you want, you can test the existing wsp during the weekend with proper mitigation plan.

  • Edited by Avijit Sur Thursday, May 07, 2015 10:54 AM update
May 7th, 2015 10:51am

No problem Avijit, you have been more than helpful so far so thank you!! :-)

No rush at all!

Thanks again.

Free Windows Admin Tool Kit Click here and download it now
May 7th, 2015 11:00am

Hello Red_Nation,

Please find the following link for Sandbox wsp...

One-Drive Link

Solution Name is home.sharepoint.workflowactions.wsp

The steps will be remain same in designer only that the activity name has been changed to 

Copy Document with Versions in Core Actions category.

Hope this will solve your purpose...Cheers!!!

Thanks,

Avijit

May 8th, 2015 8:05am

Hi Avijit

Thanks so much for this! I have deployed as a solution and can see it in the ribbon in SP Designer.

Now comes the task of actually setting the condition in the workflow to move the document and was struggling with this. I am running the WF on the Shared Docs, and created a separate doc library called Destination library. Major versioning has been switched on both library's.

I have selected the Destination library, but it is asking for the Field from source and Find the List Item entries (please see screenshot). I was hoping it would be a case of the user starts the WF and it moves the document?

Thanks in advance!


Free Windows Admin Tool Kit Click here and download it now
May 8th, 2015 10:31am

I am a bit confused. Please try the following for testing...

  • Go to the source library
  • Create a new workflow in designer
  • Add the new activity and provide the destination library name
  • Publish the workflow
  • Go back to the source library and choose any document
  • Click Workflows from the top ribbon
  • Start the workflow manually
  • Check the result
  • If everything looks Ok then try to add your conditions in the workflow before the new activity

I may sound stupid or preachee but I have tested in many times and never asked for any fields from source.

Please let me know if I am wrong.

 
May 8th, 2015 11:34am

I am a bit confused. Please try the following for testing...

  • Go to the source library
  • Create a new workflow in designer
  • Add the new activity and provide the destination library name
  • Publish the workflow
  • Go back to the source library and choose any document
  • Click Workflows from the top ribbon
  • Start the workflow manually
  • Check the result
  • If everything looks Ok then try to add your conditions in the workflow before the new activity

I may sound stupid or preachee but I have tested in many times and never asked for any fields from source.

Please let me know if I am wrong.

 
  • Marked as answer by Red_Nation Monday, May 11, 2015 3:53 PM
Free Windows Admin Tool Kit Click here and download it now
May 8th, 2015 3:32pm

Hi Avijit

It is bizarre. I created a doc library called 'Destination Library' and it didn't seem to like it. When I created another one called 'Document 2', it copied across fine.

This is great! However, comments made in the document before the workflow is run are lost, although there is a version in there (please see screenshot).

The time has been taken back an hour, however, I am not sure whether this is the workflow being funny or one of the servers times is wrong.

Thanks!



  • Edited by Red_Nation Monday, May 11, 2015 11:24 AM
May 11th, 2015 8:38am

Please check and confirm whether your server time is wrong or not.

Please enable minor and major versions to copy the checking comments.



  • Edited by Avijit Sur Monday, May 11, 2015 11:09 AM update
Free Windows Admin Tool Kit Click here and download it now
May 11th, 2015 10:48am

Hi

Just checked servers, and they all seem in sync...

The other query I had was, I was hoping it was 'moving' the document, rather than 'copy'. Not sure if there is a huge overhead in the coding or not.

As always, thanks for all the help.  

May 11th, 2015 11:23am

Please find the updated Sandbox solution and hope this will cover all the aspects of your requirement

Home.SharePoint.WorkflowActions.wsp

  • Edited by Avijit Sur Monday, May 11, 2015 3:18 PM update
  • Marked as answer by Red_Nation Monday, May 11, 2015 3:47 PM
Free Windows Admin Tool Kit Click here and download it now
May 11th, 2015 3:16pm

I just ran it and it worked like a dream. Avijit, you legend!!! :-)

Thanks a million!

May 11th, 2015 3:47pm

Hi,

I tried to use this method to move document from one list to other with version history through custom workflow. as soon as the workflow hits this move statement, it holds there and workflow remains in running stage, not sure if I am missing something, exactly did as above..

Appreciate your help on this one.

Thanks and Regards,

Manjunath

Free Windows Admin Tool Kit Click here and download it now
July 27th, 2015 9:56pm

Hi,

I'll check and let you know. Meanwhile, please let me know the steps and implementation details you are trying to achieve. 

Thanks,

Avijit Sur

July 28th, 2015 4:51pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics