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)
            {
            }
        }
    }
}

10 comments:

  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
  2. Thanks for the interview preparation tips — really helpful! Along with preparing well, job seekers should also look at current openings. You can explore some trending jobs in Bengaluru here. Good luck to all!

    ReplyDelete
  3. If you're looking to kickstart your career in the corporate sector, there are some great Latest Jobs in Gurugram that are actively hiring across various domains.

    ReplyDelete
  4. This doesn’t mean you have to drop all your current friends (although do take note of who makes you feel joyful and who doesn’t and then a ct accordingly) what I’m suggesting is that you only discuss your precious dreams with those who lift them up with you.Stan Store

    ReplyDelete
  5. Electricity powers machines in textile and garment industries. same day electricity service texas

    ReplyDelete
  6. 7ADE1FA4F4
    For those looking to add vibrant designs to their apparel, dtf transfers offer a high-quality and durable solution. These transfers allow for detailed and long-lasting prints that stand out on any fabric. Whether you're customizing t-shirts or other garments, using dtf transfers can significantly enhance your creative options. They are easy to apply and provide professional-looking results every time.

    ReplyDelete

Featured Post

Create SharePoint Folder Structure in Destination (Only If Not Exists)

Why This Script Is Safe You can run it multiple times It will not create duplicate folders It will only create missing folders S...

Popular posts