I have a SharePoint 2016 on-premises site with a custom list. One of the fields in this list is a lookup to my Shared Documents library. All of the documents in this library are contained in sub-folders. I am populating this list from an existing database using CSOM. I have successfully created all of the records in the database and am now attempting to come back and fill out the Documents field for each record. To do this, I am enumerating through the items in the custom list and looking up each one in the database. If it has a document associated with it, I am using the following code to attempt to fill out the Documents field for the item:

            using (var spContext = new ClientContext("https://mysite/mysubsite")) {
            var list = spContext.Web.Lists.GetByTitle("Issues");
            //issueId is a unique identifier for the issue passed into this function
            var query = new CamlQuery {
                ViewXml = $"<View><Query><Where><Eq><FieldRef Name='Issue_x0020_Id' /><Value Type='Text'>{issueId}</Value></Eq></Where></Query></View>"
            var items = list.GetItems(query);
            if (items.AreItemsAvailable) {
                //The issueId is unique, so the items collection will only ever contain one item
                var item = items[0];
                //docId is an array of integers representing unique ids of documents in the Shared Documents library
                if (docId.Count() > 1) {
                    var lookupItems = new List<FieldLookupValue>();
                    foreach (var d in docId) {
                        lookupItems.Add(new FieldLookupValue {
                            LookupId = d
                    item["Documents"] = lookupItems;
                } else {
                    item["Documents"] = new FieldLookupValue {
                        LookupId = docId.First()

I don't get any errors in the above code, but when I go back and check the list item that was supposed to be modified, there are no documents in the field.

  • If there is only one Id, does the else part work? And if there are more than one, did you try convering the list to array before assigning? May 8, 2018 at 12:13
  • No, it does not work with only one document either. I haven't tried converting the list to an array yet.
    – DrewB
    May 8, 2018 at 12:55
  • Converting the list to an array didn't help either.
    – DrewB
    May 8, 2018 at 13:12

1 Answer 1


Turns out that code was working all along. The default view for a document library shows the Name as the first column. But when you put that document in a lookup, it doesn't allow you to select the Name field, only the title. So the documents were being linked to the Issues, but there was no title to see so the list looked empty. I wrote a quick script to copy the name to the title and now everything works perfectly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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