December 15, 2010

// // Leave a Comment

Copy list items from one site to another using visual studio workflow in share point:

In this post I will tell how to copy list items from one list to other in 5 simple steps ...

Step1:
Create 2 lists in your share point site. I have created 2 lists namely list1 and list2.

List1 – Has the following fields
1.       Title
2.       Age
3.       Date
4.       Employee Name

List2 – Has the following fields
1.       Title
2.       Age
3.       Date
4.       Emp name
      
       Step2:

Open visual studio and click open
File -> New -> Project … In Project Types which is in left pane select office 2007 and select “Share point 2007 sequential Workflow”

Step3:
Open workflow.cs file and double click onWorkflowActivated1


Step4:

Use this code inside that onWorkflowActivated1_Invoked function…
SPSite mySourceSite = new SPSite("http://productionsrv:1010/Lists/list1/AllItems.aspx/");
            SPWeb mySourceWeb = mySourceSite.OpenWeb();
            SPList mySourceList = mySourceWeb.Lists["list1"];
            SPListItem mySourceListItem = workflowProperties.Item;

            string SourceTitle = mySourceListItem["Title"].ToString();
            string SourceAge = mySourceListItem["Age"].ToString();
            string SourceDesig = mySourceListItem["Date"].ToString();
            string SourceEmpName = mySourceListItem["Employee name"].ToString();

            SPSite myDestinationSite = new SPSite("http://productionsrv:1010/Lists/list2/AllItems.aspx");
            SPWeb myDestinationWeb = myDestinationSite.OpenWeb();
            SPList myDestinationList = myDestinationWeb.Lists["list2"];
            SPListItem myDestinationListItem = myDestinationList.Items.Add();

            myDestinationListItem["Title"] = SourceTitle;
            myDestinationListItem["Age"] = SourceAge;
            myDestinationListItem["Date"] = SourceDesig;
            myDestinationListItem["Emp name"] = SourceEmpName;
            myDestinationWeb.AllowUnsafeUpdates = true;
            myDestinationListItem.Update();
            myDestinationWeb.AllowUnsafeUpdates = false;
Step5:

Build and deploy in visual studio, which will open your site automatically to that list site.
Now values entered in list1 will be copied to list2 automatically…!



0 comments:

Post a Comment