如何使用CSOM将列表共享点列表附件复制到文档库

时间:2020-09-16 19:12:04

标签: sharepoint-2013 csom

如何使用CSOM将列表附件复制到文档库中。

2 个答案:

答案 0 :(得分:0)

请使用此脚本。将要复制到项目数组中的项目ID添加为[id1,id2],并重命名您的库名称为“ PictureLibrary”,并替换folderPath列表名称为“ test”

        function CopyAtt(Items, itemIndex) {
        try {
                myContext = new SP.ClientContext.get_current();
                var myWeb = myContext.get_site().get_rootWeb(); 

                var folderPath = 'Lists/test/Attachments/' + Items[itemIndex];
                var Folder = myWeb.getFolderByServerRelativeUrl(folderPath);

                Files = Folder.get_files();
                myContext.load(Files);

                myContext.executeQueryAsync(Function.createDelegate(
                                        this, ExecuteLoadFileSuccess(Items, itemIndex);),
                                        Function.createDelegate(
                                        this, GetLeadsFail));                       
            }
            catch (err) {
                alert(err.Line);
            }
        }
        function GetLeadsFail(sender, args) {
            // Show error message
            alert('Request failed - ' + args.get_message());
        }

        function ExecuteLoadFileSuccess(Items, itemIndex, sender, args) {

            for (var p = 0; p < this.Files.get_count(); p++) {
                var file = Files.itemAt(p);
                var filename = file.get_name(); 
            }

            if (filename != null) {
                    var newUrl = 'PictureLibrary/' + filename;
                    file.copyTo(newUrl, true);
                    myContext.executeQueryAsync(Function.createDelegate(
                                            this, function(){ExecuteCopyOnSuccess(Items, itemIndex);}),
                                            Function.createDelegate(
                                            this, GetLeadsFail));
            }
        }

        function ExecuteCopyOnSuccess(Items, itemIndex, sender, args) {
            //Call CopyAtt() after copy files success.
            if (itemIndex <Items.length-1) {
               CopyAtt(Items, itemIndex+1);
            }
        }

        $(document).ready(function() {
            //save all Items ID in an array.
            var Items = [2,3,6,7,8,10];
            CopyAtt(Items, 0);
        }

答案 1 :(得分:0)

@ALI RAZA ZAFAR,

请参考以下CSS代码:

context.Load(context.Web, x => x.ServerRelativeUrl);
context.ExecuteQuery();
    
Console.WriteLine($"web url:{context.Web.ServerRelativeUrl}");
    
List targetList = context.Web.Lists.GetByTitle("myanno");           
ListItem oItem = targetList.GetItemById(3);
context.Load(targetList);
context.Load(oItem);
context.ExecuteQuery();
    
Attachment oAttachment = oItem.AttachmentFiles.GetByFileName("custom.txt");
context.Load(oAttachment);
context.ExecuteQuery();
    
Console.WriteLine(oAttachment.ServerRelativeUrl);
    
File attachfile = context.Web.GetFileByServerRelativeUrl(oAttachment.ServerRelativeUrl);
attachfile.CopyTo(context.Web.ServerRelativeUrl + "/mytestdoclib/test/customcopy.txt", true);
    
context.Load(attachfile);
context.ExecuteQuery();