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?

  • Please check internal name of target column. – Trimantra Software Solution Jan 16 '17 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 Jan 16 '17 at 10:18
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();
  • Title column still remains empty, I have created a new list and then adding items, but not able to copy title column – KumarV Jan 16 '17 at 9:25
  • make sure you have exact same column internal name as 'Title', Also do you have that column in AllItems view ? – Gaurravs Jan 16 '17 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 Jan 16 '17 at 11:45

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.