0

I've tried to compare two SharePoint Online lists. Both of them have an "email" as list item and if the email is equal then it should import the list item "Steuernummer" (German word for Tax number) which is in list1 to the other list. I've tried everything and I really don't know what to do.

Here is my Code:

using System;
using System.IO;
using System.Configuration;
using System.Collections.Generic;
using System.Reflection;
using System.Diagnostics;
using System.Linq;
using CsvHelper;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Utilities;
using Microsoft.SharePoint.Client.Taxonomy;

namespace ImportListFromCSV {
    class Program {

static void Main() {

        IList<GesetzlicherVertreter> gvMap = GetGV();
        IList<Funktionaere> gvMap2 = GetFK();
        // creates a new SharePoint context 
        // passing the parameters
        Uri siteUri = new Uri(ConfigurationManager.AppSettings["SrcUrl"].ToString());
        Uri siteUri2 = new Uri(ConfigurationManager.AppSettings["DestUrl"].ToString());
        using (ClientContext clientContext = new ClientContext(siteUri.ToString())) {
            using (ClientContext clientContext2 = new ClientContext(siteUri2.ToString())) {
                // selects the list by title
                List list = clientContext.Web.Lists.GetByTitle("Gesetzliche Vertreter");
                List list2 = clientContext2.Web.Lists.GetByTitle("Funktionaere");
                CamlQuery query = new CamlQuery {
                    ViewXml = "<View><Query><OrderBy><FieldRef Name='Steuernummer' /></OrderBy></Query></View>"
                };

                ListItemCollection collListItem = list.GetItems(query);
                clientContext.Load(collListItem);
                ListItemCollection collListItem2 = list2.GetItems(query);
                clientContext2.Load(collListItem2);

                /* clientContext2.Load(collListItem,
                 items => items.Include(
                 item => item["Steuernummer"],
                 item => item["E-Mail"]));
               */
                //clientContext2.Load(collListItem2,
                //items => items.Include(
                //item => item["Steuernummer"],
                //item => item["Email"]));

                //Add new item to list


                //Persist changes

                clientContext2.ExecuteQuery();
                foreach (ListItem item in collListItem2) {
                    ListItemCreationInformation itemCreateInfo2 = new ListItemCreationInformation();
                    ListItem listItem = list.AddItem(itemCreateInfo2);

                    listItem.Update();
                    clientContext2.ExecuteQuery();

                }

                //}
            }
        }
    }

    private static IList<GesetzlicherVertreter> GetGV() {
        Uri siteUri = new Uri(ConfigurationManager.AppSettings["SrcUrl"].ToString());
        using (ClientContext clientContext = new ClientContext(siteUri.ToString())) {

            List list = clientContext.Web.Lists.GetByTitle("Gesetzliche Vertreter");
            ListItemCollection collListItem = list.GetItems(CamlQuery.CreateAllItemsQuery());
            clientContext.Load(collListItem);
            clientContext.ExecuteQuery();

            return collListItem.Cast<ListItem>().Select(x => new GesetzlicherVertreter {
                ID = x.Id
            }).ToList();

        }
    }

    private static IList<Funktionaere> GetFK() {
        Uri siteUri2 = new Uri(ConfigurationManager.AppSettings["DestUrl"].ToString());
        using (ClientContext clientContext2 = new ClientContext(siteUri2.ToString())) {

            List list2 = clientContext2.Web.Lists.GetByTitle("Funktionaere");
            ListItemCollection collListItem2 = list2.GetItems(CamlQuery.CreateAllItemsQuery());
            clientContext2.Load(collListItem2);
            clientContext2.ExecuteQuery();

            return collListItem2.Cast<ListItem>().Select(x => new Funktionaere {
                ID = x.Id
            }).ToList();

        }
    }
}

class GesetzlicherVertreter {
    public int ID { get; set; }
}


class Funktionaere {
    public int ID { get; set; }
}
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.