简体   繁体   中英

Import excel data to lookup columns in SharePoint List

I got more than 6000 data in excel which need to be imported to an existing SharePoint List containing lookup columns.

For example if I have column name Country in excel, I want the data in this column to be inserted in the lookup column Country in SharePoint List. I tried using access db and it didn't work.

Is there any other way using JSOM or access other than Powershell ? (cannot use powershell or any server side coding).

I think the lookup fields in the lists are different than those in excel. You cannot link any lookup or formulas from excel to a list column as from what I remember, also mapping excel column to list lookup field would not work as well. Probably you may have to add the data from the list and then create similar 'Country' field in the list, but as SharePoint List lookup field by hand. Doubt this can be automated without CSOM code and usage of some excel tooling like Open XML SDK.

What you should do is save your Excel file to a CSV-file (avoiding the openXML approach which will makes things more complex than they are). Then in c# you can use System.IO.File to read the file and loop through each row

string[] _fileData = System.IO.File.ReadAllLines(_filePath);

_fileData will be a multi-dimensional array, for row and columns.

Filling in the lookup field, you can use CSOM to achieve this. See this sample code i copied from: https://karinebosch.wordpress.com/2015/05/11/setting-the-value-of-a-lookup-field-using-csom/

 CamlQuery camlQueryForItem = new CamlQuery();
 camlQueryForItem.ViewXml = string.Format(@"<View>
          <Query>
              <Where>
                 <Eq>
                     <FieldRef Name='{0}'/>
                     <Value Type='{1}'>{2}</Value>
                 </Eq>
               </Where>
           </Query>
    </View>", lookupFieldName, lookupFieldType, value);

  listItemCollection listItems = list.GetItems(camlQueryForItem);
  clientContext.Load(listItems, items => items.Include
                                    (listItem => listItem["ID"],
                                     listItem => listItem[lookupFieldName]));
  clientContext.ExecuteQuery();

  if (listItems != null)
  {
      ListItem item = listItems[0];
      lookupValue = new FieldLookupValue();
      lookupValue.LookupId = Int.Parse(item["ID"].ToString());

So you first get the listItem through a CAML query, and then set the value through the FieldLookupValue object

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM