将带有附件的列表项从源列表复制到目标列表 [英] Copy list items from source list to destination list with attachments

查看:58
本文介绍了将带有附件的列表项从源列表复制到目标列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够将列表项从列表A复制到列表B,但是如何使用客户端对象模型复制带有附件的列表项.下面是我到目前为止使用的代码.

i am able to copy list items from List A to List B, but how can i copy with attachments using Client Side Object Model. Below is the code i have used so far.

静态void Main(string [] args)
static void Main(string[] args)

推荐答案

此处是一个供您参考的演示:

using Microsoft.SharePoint;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Utilities;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplicationCommon
{
    class Program
    {
        static void Main(string[] args)
        {

            //specify your site url
            string siteUrl = "http://wakaka:17710/sites/publish_site";
            ClientContext clientContext = new ClientContext(siteUrl);
            List listFrom = clientContext.Web.Lists.GetByTitle("CustomList");
            List listTo = clientContext.Web.Lists.GetByTitle("ListB");
            Site site = clientContext.Site;
            NetworkCredential credential = new NetworkCredential("spsvc", "Access1", "CONTOSO");
            clientContext.Credentials = credential;

            CamlQuery camlQuery = new CamlQuery();           
            ListItemCollection items = listFrom.GetItems(camlQuery);
            Folder rootFolder = listFrom.RootFolder;


            //load data
            clientContext.Load(site);
            clientContext.Load(listFrom);
            clientContext.Load(listTo);
            clientContext.Load(items);
            clientContext.Load(rootFolder);
            clientContext.ExecuteQuery();

            foreach (ListItem item in items)
            {

                ListItemCreationInformation newItemInfo = new ListItemCreationInformation();
                ListItem newItem = listTo.AddItem(newItemInfo);
                newItem["Title"] = item["Title"];
                newItem["Name"] = item["Name"];
                newItem["Gender"] = item["Gender"];
                newItem["home"] = item["home"];
                newItem.Update();
                clientContext.ExecuteQuery();

                try
                {
                    Folder attFolder = clientContext.Web.GetFolderByServerRelativeUrl(rootFolder.ServerRelativeUrl + "/Attachments/" + item.Id);
                    clientContext.Load(attFolder);
                    clientContext.ExecuteQuery();
                    FileCollection files = attFolder.Files;
                    clientContext.Load(files);
                    clientContext.ExecuteQuery();

                    if (files.Count > 0)
                    {              

                        foreach (Microsoft.SharePoint.Client.File oFile in files)
                        {
                            FileInfo myFileinfo = new FileInfo(oFile.Name);
                            string myFileExtension = myFileinfo.Extension.ToLower();
                            WebClient client1 = new WebClient();
                            client1.Credentials = credential;
                            byte[] fileContents = client1.DownloadData("http://wakaka:17710" + oFile.ServerRelativeUrl);

                            //init a AttachmentCreationInformation object
                            AttachmentCreationInformation attInfo = new AttachmentCreationInformation();
                            attInfo.FileName = myFileinfo.Name;
                            attInfo.ContentStream = new MemoryStream(fileContents);

                            //Add to File
                            Attachment att = newItem.AttachmentFiles.Add(attInfo); 
                            clientContext.Load(att);
                            clientContext.ExecuteQuery();
                            

                        }
                    }
                }
                catch (Exception e){
                     //there is not any attachment in this item                   
                    
                }       
                               
            }
            Console.WriteLine("success");
            Console.ReadLine();                                      
        }
      
    }
}

结果截图:

最诚挚的问候,

刘李


这篇关于将带有附件的列表项从源列表复制到目标列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆