Creating the custom SharePoint workflow
- 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.
- 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.
- Add a Code activity to the workflow.
Figure 1. SharePoint Sequential Workflow with a code activity to copy a document to a library. - 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. - 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:
- 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.
Figure 2. Accessing the workflows that can run on a document in a SharePoint document library. - On the Workflows page under Start a New Workflow, click the workflow you want to start.
No comments:
Post a Comment
Thank you for Commenting Will reply soon ......