0

I am using csom to add items to target list from source list but I am not able to copy items of only title column.

srcContext = new ClientContext(mpObject.SourceSiteCollection.SiteURL);
                targetContext = new ClientContext(mpObject.TargetSiteCollection.SiteURL);

                List<Field> srcFieldCollections = new List<Field>();
                List srcList = srcContext.Web.Lists.GetByTitle(listName);
                srcContext.Load(srcList);
                srcContext.ExecuteQuery();

                CamlQuery camlQuery = new CamlQuery();
                camlQuery.ViewXml = "<View/>";

                ListItemCollection srcListItemCollection = srcList.GetItems(camlQuery);
                srcContext.Load(srcListItemCollection);
                srcContext.ExecuteQuery();
                ListItemCreationInformation targetItemCreationInfo = new ListItemCreationInformation();
                ListItem targetListItem = targetList.AddItem(targetItemCreationInfo);

                foreach (ListItem sourceItem in srcListItemCollection)
                {

                    targetListItem["Title"] = "Title";

                }
                targetListItem.Update();
                targetContext.ExecuteQuery();

But it works when I do it for other column. So am I missing anything there?

2
  • Please check internal name of target column. Commented Jan 16, 2017 at 8:01
  • Your code adds only ONE item to the target list, and then loops throw all source items, setting the title of the unique target item just added... Is this the entire code? Is this correct?
    – Cecilia
    Commented Jan 16, 2017 at 10:18

1 Answer 1

2

Try below code. which will copy all the field values from source to destination.

srcContext = new ClientContext(mpObject.SourceSiteCollection.SiteURL);
                targetContext = new ClientContext(mpObject.TargetSiteCollection.SiteURL);

            List<Field> srcFieldCollections = new List<Field>();
            List srcList = srcContext.Web.Lists.GetByTitle(listName);
            srcContext.Load(srcList);
            srcContext.ExecuteQuery();

            CamlQuery camlQuery = new CamlQuery();
            camlQuery.ViewXml = "<View/>";

            ListItemCollection srcListItemCollection = srcList.GetItems(camlQuery);
            srcContext.Load(srcListItemCollection);
            srcContext.ExecuteQuery();
            ListItemCreationInformation targetItemCreationInfo = new ListItemCreationInformation();
            ListItem targetListItem = targetList.AddItem(targetItemCreationInfo);

            foreach (ListItem sourceItem in srcListItemCollection)
            {
              foreach (Field field in sourceItem.Fields)
              {
                targetListItem[field.InternalName] = sourceItem[field.InternalName] ;
              }
              targetListItem["Title"] = sourceItem["Title"] ;
            }
            targetListItem.Update();
            targetContext.ExecuteQuery();
3
  • Title column still remains empty, I have created a new list and then adding items, but not able to copy title column
    – KumarV
    Commented Jan 16, 2017 at 9:25
  • make sure you have exact same column internal name as 'Title', Also do you have that column in AllItems view ?
    – Gaurravs
    Commented Jan 16, 2017 at 11:01
  • yes internal name is same as when list is created , title column is created by default. and also yes this title column is available in all item view.
    – KumarV
    Commented Jan 16, 2017 at 11:45

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.