1

I need to migrate a SharePoint list with "Related Items" column field content of all list items to a completely new SharePoint environment.

It does not work when saved the template of the list and imported into the new tenant.

0

You can achieve it via CSOM API.

Here is a simple demo for your reference:

    /// <summary>
    /// retreive data of data table from list
    /// </summary>
    /// <param name="siteUrl"></param>
    /// <param name="listName"></param>
    /// <param name="columnNameArr"></param>
    /// <returns></returns>
    public static DataTable retreiveDataFromListToDataTable(string siteUrl, string listName, string[] columnNameArr)
    {
        ClientContext clientContext = new ClientContext(siteUrl);
        List list = clientContext.Web.Lists.GetByTitle(listName);
        FieldCollection fields = list.Fields;
        CamlQuery query = new CamlQuery();
        ListItemCollection items = list.GetItems(query);

        //load data
        clientContext.Load(list);
        clientContext.Load(fields);
        clientContext.Load(items);
        clientContext.ExecuteQuery();

        //create a dataTable which will insert into the database
        DataTable dataTable = new DataTable("table");

        //init the structure of dataTable
        foreach (string columnName in columnNameArr)
        {
            DataColumn dataColumn = new DataColumn(columnName);
            dataTable.Columns.Add(dataColumn);
        }

        //fill the dataTable
        foreach (ListItem item in items)
        {
            DataRow dataRow = dataTable.NewRow();
            foreach (string columnName in columnNameArr)
            {

                Object obj = item[columnName];
                dataRow[columnName] = (item[columnName] == null ? "" : item[columnName].ToString());

            }
            dataTable.Rows.Add(dataRow);
        }

        return dataTable;
    }

    /// <summary>
    /// recovery data of list from data table
    /// </summary>
    /// <param name="siteUrl"></param>
    /// <param name="listName"></param>
    /// <param name="columnNameArr"></param>
    /// <param name="dataTable"></param>
    public static void recoveryDataFromDataTableToList(string siteUrl, string listName, string[] columnNameArr, DataTable dataTable)
    {
        ClientContext clientContext = new ClientContext(siteUrl);
        List list = clientContext.Web.Lists.GetByTitle(listName);
        FieldCollection fields = list.Fields;
        CamlQuery query = new CamlQuery();
        ListItemCollection items = list.GetItems(query);

        //load data
        clientContext.Load(list);
        clientContext.Load(fields);
        clientContext.Load(items);
        clientContext.ExecuteQuery();


        foreach(DataRow row in dataTable.Rows)
        {
            ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
            ListItem oListItem = list.AddItem(itemCreateInfo);
            foreach (string columnName in columnNameArr)
            {
                string data = row[columnName].ToString();                                       
                oListItem[columnName] = data;                  
                oListItem.Update();

                clientContext.ExecuteQuery(); 
            }
        }         

    }

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.