Thursday, 10 October 2019

CSOM: How to copy all items from one SharePoint list to another?

using F = System.Windows.Forms;
using Microsoft.SharePoint.Client;
using PnP = OfficeDevPnP.Core;

PnP.AuthenticationManager authManagerTarget = new PnP.AuthenticationManager();
                using (ClientContext clientContextTarget = authManagerTarget.GetWebLoginClientContext(txtTargetSite.Text))
                {
                    List targetList = clientContextTarget.Web.Lists.GetByTitle(drpTargetList.SelectedItem.ToString());
                    clientContextTarget.Load(targetList);
                    clientContextTarget.ExecuteQuery();

                    PnP.AuthenticationManager authManagerSource = new PnP.AuthenticationManager();
                    using (ClientContext clientContextSource = authManagerSource.GetWebLoginClientContext(txtSourceSite.Text))
                    {
                        List sourceList = clientContextSource.Web.Lists.GetByTitle(drpSourceList.SelectedItem.ToString());
                        ListItemCollection sourceItems = sourceList.GetItems(CamlQuery.CreateAllItemsQuery());
                        clientContextSource.Load(sourceItems);
                        clientContextSource.ExecuteQuery();

                        progressBar1.Maximum = sourceItems.Count;

                        foreach (ListItem sourceItem in sourceItems)
                        {
                            ListItemCreationInformation newItem = new ListItemCreationInformation();
                            ListItem targetItem = targetList.AddItem(newItem);
                            FieldCollection sourceFields = sourceList.Fields;
                            clientContextSource.Load(sourceFields);
                            clientContextSource.ExecuteQuery();
                            foreach (Field field in sourceFields)
                            {
                                if (field.InternalName != "Attachments" && field.InternalName != "ContentType" &&
                                     !field.Hidden && !field.ReadOnlyField &&
                                     sourceItem[field.InternalName] != null &&
field.FieldTypeKind.ToString() != "URL" && field.FieldTypeKind.ToString() != "WorkflowStatus")
                                {
                                    try
                                    {
                                        targetItem[field.InternalName] = sourceItem[field.InternalName];
                                    }
                                    catch (Exception ex)
                                    {
                                        this.LogError(ex);
                                    }
                                }
                            }
                            targetItem.Update();
                            clientContextTarget.ExecuteQueryRetryAsync(10, 10000);
                            progressBar1.Value += 1;
                        }
                    }
                }

No comments:

Post a Comment