Tuesday, November 8, 2011

workflow: Copy a document from one SharePoint document library to another


Creating the custom SharePoint workflow

  1. In SharePoint, create 2 document libraries: One to copy documents from and another one to store the copied documents in. Note: These document libraries do not have to be located on the same site.
  2. In Visual Studio, create a SharePoint Sequential Workflow. If you are unsure as to which steps you have to take to create a SharePoint workflow, refer to the tutorials in the Visual Studio workflows for SharePoint mini course.
  3. Add a Code activity to the workflow.
    SharePoint Sequential Workflow with a code activity to copy a document to a library.
    Figure 1. SharePoint Sequential Workflow with a code activity to copy a document to a library.
  4. Write the following code in the ExecuteCode event handler of the Code activity:
    // Get the item the workflow is running on
    SPListItem item = workflowProperties.Item;


    // Set the name of the document library and site
    // where to copy the document to
    string siteUrl = "http://<ServerName>/<SiteName>/";
    string libName = "DocLibForCopies";


    // Open the site
    using (SPSite site = new SPSite(siteUrl))
    {
      using (SPWeb web = site.OpenWeb())
      {
        if (item.File != null)
        {
          // Copy the document to the library
          SPFolder library = web.Folders[libName];
          library.Files.Add(
            item.Name, item.File.OpenBinary());
          library.Update();
        }
      }
    }

    where ServerName is the name of your SharePoint server, SiteName the name of the site on which the second document library is located, and DocLibForCopes the name of the second document library to which documents will be copied.
  5. Attach the workflow to the document library that contains the documents you want to copy, and select the Allow this workflow to be manually started by an authenticated user with Edit Item Permissions check box when attaching the workflow to the document library.

Testing the workflow

Now whenever you manually start the workflow on a document in the first document library, that document will be copied to the second document library and the workflow will end.
To manually start the workflow:
  1. Hover over a document in the first document library until a drop-down list box appears, click on the down arrow, and select Workflows from the context menu that appears.


    Accessing the workflows that can run on a document in a SharePoint document library.

    Figure 2. Accessing the workflows that can run on a document in a SharePoint document library.
  2. On the Workflows page under Start a New Workflow, click the workflow you want to start.
    Manually starting a SharePoint workflow.

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

The Code 39 error occurs when Windows is unable to load the device driver for a specific hardware device. This typically indicates that the driver is corrupted, missing, or incompatible

The Code 39 error occurs when Windows is unable to load the device driver for a specific hardware device. This typically indicates that the...