Thursday, June 12, 2014

Copy/Move/Update/Save the List items from Source List to Destination List using ProcessBatchData in SharePoint

Copy/Move/Update/Save the List items from Source List to Destination List using ProcessBatchData in SharePoint.
http://msdn.microsoft.com/en-us/library/office/ms461938(v=office.14).aspx
http://msdn.microsoft.com/en-us/library/office/ms480784(v=office.14).aspx
ProcessBatchData
In list1, list2 column name should be same like Title, USER_NAME, PLACE. column name are case sensitive.
in .aspx page:-
------------------------
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="test.Layouts.test.test"
    DynamicMasterPageFile="~masterurl/default.master" %>

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">  
    <table>
        <tr>
            <td align="center">
                <asp:Button ID="btnClose" Font-Bold="true" runat="server" Text="Move Items from list1 to list2"
                    OnClick="btnMove_Click" />
            </td>
        </tr>
    </table>
</asp:Content>
<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
    Application Page
</asp:Content>
<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea"
    runat="server">
    My Application Page

</asp:Content>

In .aspx.cs page:-
---------------------------
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Text;
using System.Data;
using System.Web;

namespace test.Layouts.test
{
    public partial class test : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void btnMove_Click(object sender, EventArgs e)
        {
            try
            {
                SPWeb web = SPContext.Current.Web;
                SPList source_list = web.Lists["list1"];
                SPList destination_list = web.Lists["list2"];

                SPQuery query = new SPQuery();
                query.RowLimit = 2000;
                StringBuilder sbQuery = new StringBuilder();
                sbQuery.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");
                string command =
                    "<Method>" +
                        "<SetList Scope=\"Request\">{0}</SetList>" +
                        "<SetVar Name=\"ID\">New</SetVar>" +
                        "<SetVar Name=\"Cmd\">Save</SetVar>" +
                        "<SetVar Name=\"urn:schemas-microsoft-com:office:office#Title\">{1}</SetVar>" +
                        "<SetVar Name=\"urn:schemas-microsoft-com:office:office#USER_NAME\">{2}</SetVar>" +
                        "<SetVar Name=\"urn:schemas-microsoft-com:office:office#PLACE\">{3}</SetVar>" +
                    "</Method>";

                string deleteCommand =
                    "<Method>" +
                        "<SetList Scope=\"Request\">{0}</SetList>" +
                        "<SetVar Name=\"ID\">{1}</SetVar>" +
                        "<SetVar Name=\"Cmd\">Delete</SetVar>" +
                    "</Method>";

                //string CopyCommand =
                //   "<Method>" +
                //       "<SetList Scope=\"Request\">{0}</SetList>" +
                //       "<SetVar Name=\"ID\">{1}</SetVar>" +
                //       "<SetVar Name=\"Cmd\">Copy</SetVar>" +
                //   "</Method>";

                SPListItemCollection itmColl;
                do
                {
                    itmColl = source_list.GetItems(query);
                    if (itmColl.Count >= 0)
                    {
                    }
                    query.ListItemCollectionPosition = itmColl.ListItemCollectionPosition;
                    DataTable dtData = new DataTable();
                    dtData = itmColl.GetDataTable();
                    foreach (DataRow dr in dtData.Rows)
                    {
                        sbQuery.AppendFormat(command, destination_list.ID, Convert.ToString(dr["Title"]), Convert.ToString(dr["USER_NAME"]), Convert.ToString(dr["PLACE"]));
                        sbQuery.AppendFormat(deleteCommand, source_list.ID, Convert.ToString(dr["ID"]));
                        //sbQuery.AppendFormat(CopyCommand, source_list.ID, Convert.ToString(dr["ID"]));
                    }

                } while (query.ListItemCollectionPosition != null);

                sbQuery.Append("</Batch>");
                web.ProcessBatchData(sbQuery.ToString());
            }
            catch (Exception ex)
            {
            }
        }
    }
}

1 comment:

  1. Today, I was just browsing along and came upon your blog. Just wanted to say good blog and this article helped me a lot, due to which I have found exactly I was looking. 토토커뮤니티

    ReplyDelete

Featured Post

Mention a Channel or Team – Power Automate

Mention a Channel or Team – Power Automate graph.microsoft.com/v1.0/teams/{teamId}/channels/{channelId}/messages Channel: ---------- {   &qu...

Popular posts