将附件从列表项复制到另一个列表中的另一个列表项 [英] Copy attachment from list item to another list item in another list

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

问题描述

我有一个来源和目的地列表,我需要一种方法将列表中的附件复制到新的列表项。


复制值相当容易,但我不知道要复制附件的对象。


以下是我用来从c#网络表单中获取文件并将其上传到sharepoint列表的代码。哪个工作正常。

 foreach(uploadAttachment_name1.PostedFiles中的var文件)
{
byte [] contents = new byte [Convert.ToInt32 (file.ContentLength)];
Stream fStream = file.InputStream;
fStream.Read(contents,0,Convert.ToInt32(file.ContentLength));
fStream.Close();
MemoryStream mStream = new MemoryStream(contents);
AttachmentCreationInformation aci = new AttachmentCreationInformation();
aci.ContentStream = mStream;
aci.FileName = file.FileName;
附件附件= oListItem.AttachmentFiles.Add(aci);
clientContext.Load(attachment);
clientContext.ExecuteQuery();
}
uploadAttachment_name1.Dispose();

查看列表项和附件我可以将其作为好吧


下面是我必须使用客户端上下文对象从列表中查看/检索附件的代码。

 clientContext.Load(targetListItem,item => item [" Title"]); 
clientContext.Load(附件);
clientContext.ExecuteQuery();
if(targetListItem.AttachmentFiles.Count> 0)
{
foreach(targetListItem.AttachmentFiles中的var附件)
{
path = attachment.ServerRelativeUrl; //获取文件的名称,在最后一次斜杠后搜索值的值
pos = path.LastIndexOf(" /")+ 1;
filename = path.Substring(pos,path.Length - pos);
lblSupportingDocuments.Text + ="< a href ='" + ConfigurationManager.AppSettings [" BaseURL"]。ToString()+ attachment.ServerRelativeUrl +"'target ='_ blank'>" + filename +"< / a>< br />" ;;
}
}

但问题是我想把这些附件添加到新列表中另一个列表中的项目。


我不确定如何做到这一点。


非常感谢任何反馈或帮助。

解决方案

您好,


检查Akshay Belure分享的解决方案。


< pre class ="prettyprint"> private static void UpdateAttachments(ClientContext srccontext,
ClientContext dstcontext,int srcItemID,int destItemID,string listName)
{
try
{
//从文件获取附件
Web srcweb = srccontext.Web;
srccontext.Load(srcweb);
srccontext.ExecuteQuery();
string src = string.Format(" {0} / lists / {1} / Attachments / {2}",
srcweb.Url,listName,srcItemID);
文件夹attachmentsFolder = srcweb.GetFolderByServerRelativeUrl(src);
srccontext.Load(attachmentsFolder);
FileCollection attachments = attachmentsFolder.Files;
srccontext.Load(附件);
srccontext.ExecuteQuery();

if(attachments.Count> 0)
{
foreach(附件中的Microsoft.SharePoint.Client.File附件)
{
ClientResult<流> 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(listName);
ListItem destitem = destlist.GetItemById(destItemID);
dstcontext.Load(destitem);
dstcontext.ExecuteQuery();
附件a = destitem.AttachmentFiles.Add(attachFileInfo);
dstcontext.Load(a);
dstcontext.ExecuteQuery();
stream2.Close();
}
}
}
catch(exception ex)
{
//记录异常
}
}


https://www.codeproject.com/Tips/1119844/Programatically-Copy-all-SharePoint-List-Items-to


最诚挚的问候,


Lee


I have a source and a destination list and I need a way to copy the attachments from a list from to a new list item.

Copying values is fairly easy, but I don't know who to copy the attachments.

Below is the code I use to take the file from the c# web form and upload it to the sharepoint list. Which is working fine.

                foreach (var file in uploadAttachment_name1.PostedFiles)
                {
                    byte[] contents = new byte[Convert.ToInt32(file.ContentLength)];
                    Stream fStream = file.InputStream;
                    fStream.Read(contents, 0, Convert.ToInt32(file.ContentLength));
                    fStream.Close();
                    MemoryStream mStream = new MemoryStream(contents);
                    AttachmentCreationInformation aci = new AttachmentCreationInformation();
                    aci.ContentStream = mStream;
                    aci.FileName = file.FileName;
                    Attachment attachment = oListItem.AttachmentFiles.Add(aci);
                    clientContext.Load(attachment);
                    clientContext.ExecuteQuery();
                }
                uploadAttachment_name1.Dispose();

The to view the list item and attachments I can do it as well

Below is the code I have to 'view/retrieve the attachments from the list using the client context object.

                clientContext.Load(targetListItem, item => item["Title"]);
                clientContext.Load(attachments);
                clientContext.ExecuteQuery();
                if (targetListItem.AttachmentFiles.Count > 0)
                {
                    foreach (var attachment in targetListItem.AttachmentFiles)
                    {
                        path = attachment.ServerRelativeUrl; //get the name of the file, search string of value after last slash
                        pos = path.LastIndexOf("/") + 1;
                        filename = path.Substring(pos, path.Length - pos);
                        lblSupportingDocuments.Text += "<a href='" + ConfigurationManager.AppSettings["BaseURL"].ToString() + attachment.ServerRelativeUrl + "' target='_blank'>" + filename + "</a><br/>";
                    }
                }

But my problem is that I want to take these attachments and add them to a new list item in another list.

I am not sure how this can be done.

Any feedback or assistance would be greatly appreciated.

解决方案

Hi,

Check the solution shared by Akshay Belure.

private static void UpdateAttachments(ClientContext srccontext, 
              ClientContext dstcontext, int srcItemID, int destItemID, string listName)
        {
            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(listName);
                        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
            }
        }

https://www.codeproject.com/Tips/1119844/Programatically-Copy-all-SharePoint-List-Items-to

Best Regards,

Lee


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

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