1

I am copying list items from one list to another using the below function

protected void MigrateItem(ListItem item,ClientContext ctx,List targetList){
            ListItemCreationInformation listItemCreationInformation = new ListItemCreationInformation();
            ListItem listItem = targetList.AddItem(listItemCreationInformation);
            listItem["Title"] = item["Title"];
            //some other fields
            listItem.Update();
            ctx.ExecuteQuery();
        }

Here everything works just fine, but I am not able to see the attachments that were there in the source list. Which field should I use to get the attachments copied?

3

So I found this on the internet and made some tweaks to copy to different list and the below code is working

private static void UpdateAttachments(ClientContext srccontext,
              ClientContext dstcontext, int srcItemID, int destItemID, string listName,string destListName)
        {
            try
            {
                //getting attachment from files
                Web srcweb = srccontext.Web;
                srccontext.Load(srcweb);
                srccontext.ExecuteQuery();
                string src = string.Format("{0}/lists/{1}/Attachments/{2}",
                                  srcweb.Url, listName, srcItemID);
                Folder attachmentsFolder = srcweb.GetFolderByServerRelativeUrl(src);
                srccontext.Load(attachmentsFolder);
                FileCollection attachments = attachmentsFolder.Files;
                srccontext.Load(attachments);
                srccontext.ExecuteQuery();

                if (attachments.Count > 0)
                {
                    foreach (Microsoft.SharePoint.Client.File attachment in attachments)
                    {
                        ClientResult<Stream> clientResultStream = attachment.OpenBinaryStream();
                        srccontext.ExecuteQuery();
                        var stream = clientResultStream.Value;

                        AttachmentCreationInformation attachFileInfo =
                                                     new AttachmentCreationInformation();
                        Byte[] buffer = new Byte[attachment.Length];
                        int bytesRead = stream.Read(buffer, 0, buffer.Length);
                        System.IO.MemoryStream stream2 = new System.IO.MemoryStream(buffer);
                        attachFileInfo.ContentStream = stream2;
                        attachFileInfo.FileName = attachment.Name;

                        Web destweb = dstcontext.Web;
                        List destlist = destweb.Lists.GetByTitle(destListName);
                        ListItem destitem = destlist.GetItemById(destItemID);
                        dstcontext.Load(destitem);
                        dstcontext.ExecuteQuery();
                        Attachment a = destitem.AttachmentFiles.Add(attachFileInfo);
                        dstcontext.Load(a);
                        dstcontext.ExecuteQuery();
                        stream2.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                //Log exception
            }
        }

Usage

protected void MigrateItem(ListItem item,ClientContext ctx,List targetList){
            ListItemCreationInformation listItemCreationInformation = new ListItemCreationInformation();
            ListItem listItem = targetList.AddItem(listItemCreationInformation);
            listItem["Title"] = item["Title"];
            //some other fields
            listItem.Update();
            ctx.ExecuteQuery();
            UpdateAttachments(ctx, ctx, item.Id, listItem.Id, ListName,DestListName);
        }

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.