0

I am trying to make a windows form app(VS 2017, C#, Windows Forms) on my Win10 (.NET version 4.6 installed). The app needs to copy a list from SharePoint 2007 List to SharePoint Online List (2016 onwards).

I can get the Web Service pulling through and can see this on my Win10 via Postman so I know the web service data is accessible.

The app has an issue though when building and running it throws the following error: Cannot find the following file Microsoft.SharePoint.Library, Version= 15.0.0.0

I understand this is probably due to 2007 being a .NET ver 2.0 and I am trying on to run on 4.5.1.

Firstly, can this be done in a windows form app at all? Two are there any workarounds for my error?

If it doesn't work I will have to try building the app on the SP 2007 box (Win 2003) and pulling the SPO online List in there instead... :-(

My app code is as follows:

 using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    //using System.Linq;
    using System.Text;
    //using System.Threading.Tasks;
    using System.Windows.Forms;

    using Microsoft.SharePoint;
    using System.Xml;
    using System.Diagnostics;
    using Microsoft.SharePoint.Library;

    namespace DataMergeDT
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            label1.Text = "Old List";
            label2.Text = "New List";
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void updateVicBtn_Click(object sender, EventArgs e)
        {
            try
            {
                EventLog.WriteEntry("Application", "Updating Locations", EventLogEntryType.Information);
                using (SPSite srcSite = new SPSite("https://intranet.company.com/"))
                {
                    using (SPWeb srcWeb = srcSite.OpenWeb("/bookings"))
                    {
                        //using (SPSite destSite = new SPSite("htt ps://intranet.company.com/t/vsts"))
                        using (SPSite destSite = new SPSite("https://company.sharepoint.com/sites/dev-sm/Lists/Locations"))
                        {

                            using (SPWeb destWeb1 = destSite.OpenWeb())
                            {


                                SPList destLocList = destWeb1.Lists[label1.Text];
                                if (destLocList != null)
                                {
                                    SPList srcLocList = srcWeb.Lists[label2.Text];

                                    if (srcLocList != null && srcLocList.ItemCount > 0)
                                    {

                                        if (destLocList != null && destLocList.ItemCount > 0)
                                        {

                                            SPQuery query;

                                            foreach (SPListItem destItem in destLocList.Items)
                                            {
                                                string strTitle = destItem["Loc"].ToString() + " (Loc)";

                                                // debugging set lable to Loc name
                                                lblLoc.Text = strTitle;
                                                query = new SPQuery();
                                                query.Query = "<Where><Eq><FieldRef Name='Title'/>" +
                                                "<Value Type='Text'>" + strTitle + "</Value></Eq>" +
                                                "</Where>";

                                                //<Eq><FieldRef Name='Office Type'/><Value Type='Choice'>Loc</Value> </Eq>
                                                SPListItemCollection srcItemColl = srcLocList.GetItems(query);
                                                SPListItem srcItem;
                                                if (srcItemColl != null && srcItemColl.Count > 0)
                                                {
                                                    srcItem = srcItemColl[0];
                                                }
                                                else
                                                {
                                                    srcItem = srcLocList.Items.Add();
                                                    srcItem["Title"] = strTitle;
                                                    srcItem["Office Type"] = "HQ";
                                                }

                                                if (srcItem != null)
                                                {
                                                    if (destItem["Address"] != null)
                                                        srcItem["Address"] = destItem["Address"].ToString();
                                                    if (destItem["Category"] != null)
                                                        srcItem["Category"] = destItem["Category"].ToString();

                                                    if (destItem["Post Code"] != null)
                                                        srcItem["Postcode"] = destItem["Post Code"].ToString();
                                                    if (destItem["Region"] != null)
                                                        srcItem["Region"] = destItem["Region"].ToString();





                                                    if (destItem["Opening"] != null)
                                                        srcItem["Opening Hours"] = destItem["Opening"].ToString();
                                                    if (destItem["Council region"] != null)
                                                        srcItem["Council region"] = destItem["Council region"].ToString();
                                                    if (destItem["eMail"] != null)
                                                    {
                                                        srcItem["Email"] = destItem["eMail"].ToString();
                                                    }
                                                    if (destItem["Customer telephone number"] != null)
                                                        srcItem["Customer telephone number"] = destItem["Customer telephone number"].ToString();
                                                    if (destItem["Internal telephone number"] != null)
                                                    {
                                                        if (destItem["Internal telephone number"].ToString().Length > 0)
                                                        {
                                                            srcItem["Internal telephone number"] = destItem["Internal telephone number"].ToString();
                                                            srcItem["Telephone"] = destItem["Internal telephone number"].ToString();
                                                        }
                                                    }
                                                    else
                                                    {
                                                        if (destItem["Customer telephone number"] != null)
                                                            srcItem["Telephone"] = destItem["Customer telephone number"].ToString();
                                                    }


                                                    srcItem.Update();
                                                    // if telephone number is still blank in locations list take internal number, if this is blank use customer number
                                                    if (srcItem["Telephone"] == null)
                                                    {
                                                        if (srcItem["Internal telephone number"] != null)
                                                        {
                                                            if (srcItem["Internal telephone number"].ToString() != "")
                                                            {
                                                                srcItem["Telephone"] = srcItem["Internal telephone number"].ToString();
                                                            }
                                                            else
                                                            {
                                                                if (srcItem["Customer telephone number"] != null)
                                                                    srcItem["Telephone"] = srcItem["Customer telephone number"].ToString();
                                                            }
                                                        }
                                                        else
                                                        {
                                                            if (srcItem["Customer telephone number"] != null)
                                                                srcItem["Telephone"] = srcItem["Customer telephone number"].ToString();
                                                        }
                                                    }
                                                    //srcItem.Update();
                                                    lblLoc.Text = "";
                                                }
                                            }

                                            MessageBox.Show("Updated Sucessfully");

                                        }
                                    }
                                }
                            }

                        }
                    }
                }
                EventLog.WriteEntry("Application", "Finished updating Locations", EventLogEntryType.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.InnerException.ToString());
            }
            finally
            {

            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }
    }
}
  • It can be done, I'll see if I can find my old working application that did that. I had done a c# app, that had read items and added item to a different list. – Quinn Johns May 29 '18 at 14:41
  • Oh wow, that would be ace Quinn! I will post my current code too just in case it's my logic/programming that's wrong. – j03doeson May 29 '18 at 14:52
0

If this is just one-time copy of the list, you could do it with MS Access, without any coding.

Open your SP2007 list with MS Access (there should be the ribbon button for this in the list view), right click the list name in the MS Access "All Access Objects" left pane, choose Export->SharePoint List and then enter the url of your SharePoint online site where you want it to be copied. MS Access will create the new list with the same structure as the old one and copy the data. It could take some time until it is copied, depending on the list size.

  • 1
    Hello Damjan, Many thanks for your suggestion. Unfortunately, it's not and it was tool to be made usable over and over again. Thanks anyway – j03doeson May 29 '18 at 14:50

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.