4

I got below code in another forum which show how to move bulk documents from a document library to another specific document library

Just wanted to know where I need to change/modify code to that files can be moved from document library to any document library. like I select 10 files from doc lib A and should option that where I want to move it. As this is not for any specific library and have 20 library.

    <?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
    <CustomAction
    Id="CopyingFilesButton"
    Location="CommandUI.Ribbon"
    RegistrationId="101"
    RegistrationType="List"
    Sequence="5"
    Title="Copy Documents">
        <CommandUIExtension>
            <CommandUIDefinitions>
                <CommandUIDefinition
                Location="Ribbon.Documents.Manage.Controls._children">
                    <Button Id="Ribbon.Documents.Manage.CopyDocuments"
                    Command="CopyDocumentsButtonCommand"
                    LabelText="Move To Supplier Invoices"
                    Image32by32="/_layouts/images/MoveToSupplierInvoices/multiplemovebutton.png"
                TemplateAlias="o1" />"
            </CommandUIDefinition>
        </CommandUIDefinitions>
        <CommandUIHandlers>
            <CommandUIHandler
            Command="CopyDocumentsButtonCommand"
            CommandAction="javascript:
                var context = SP.ClientContext.get_current();
                var web = context.get_web();
                context.load(web);
                var _destinationlib = web.get_lists().getByTitle('Shared Documents');
                context.load(_destinationlib);
                var notifyId;
                var currentlibid = SP.ListOperation.Selection.getSelectedList();
                var currentLib = web.get_lists().getById(currentlibid);
                var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
                var count = CountDictionary(selectedItems);
                for(var i in selectedItems)
                {
                    alert('Now moving ' + i);
                    var currentItem = currentLib.getItemById(selectedItems[i].id);
                    context.load(currentItem);
                    var File = currentItem.get_file();
                    context.load(File);
                    //Excecuting executeQueryAsync to get the loaded values
                    context.executeQueryAsync
                        (
                            function (sender, args) {
                                if(File != null) {
                                    var _destinationlibUrl =  web.get_serverRelativeUrl() + _destinationlib.get_title() + '/' +  File.get_name();
                                    alert('Now moving to: ' + _destinationlibUrl);
                                    File.moveTo(_destinationlibUrl, true);
                                    notifyId = SP.UI.Notify.addNotification('Moving file ' + File.get_serverRelativeUrl() + ' to ' + _destinationlibUrl, true);
                                    //Excecuting executeQueryAsync to copy the file
                                    context.executeQueryAsync(
                                        function (sender, args) {
                                            SP.UI.Notify.removeNotification(notifyId);
                                            SP.UI.Notify.addNotification('File moved successfully', false);
                                        },
                                        function (sender, args) {
                                            SP.UI.Notify.addNotification('Error moving file: ' + args.get_message(), false);
                                            SP.UI.Notify.removeNotification(notifyId);
                                            showError(args.get_message());
                                    });
                                }
                            },
                            function (sender, args) {
                                alert('Error occured' + args.get_message());
                            }
                        );
                }"/>
        </CommandUIHandlers>
    </CommandUIExtension>
</CustomAction>
1

The code you shared copies the documents in the "Shared Documents" destination Library. However, as I understand your issue, you don't want to hard code the library name. Instead you want to select the library name out of several possible libraries. This can be done by creating a page preferably a layouts page and placing a drop down box containing the names of libraries and a Submit button. When a user selects a library and clicks on a Submit button, the code in the the layouts page (Server side code) will execute and move the files to destination library. The selected items are passed to this layouts page from client object model. This is how the Custom Action for that looks

<CustomAction
   Id="COB.Command.NewControlInExistingGroup.Notify.Script"
   Location="ScriptLink"
   ScriptSrc ="/_layouts/SharePoint.ApproveRejectTest/Scripts/ApproveReject.js"/>

See this example, where multiple documents are approved/rejected using the same concept. http://ranaictiu-technicalblog.blogspot.com/2011/05/sharepoint-2010-approvereject-multiple.html

UPDATE

The client object model code will get the selected list of IDs and pass them to the layouts page in the form of Query string along with listID. In the page layout, you will have code on Page load which will load names of libraries in the drop down. The event handler for submit button will read the list ID and item IDs from query string and also read the selected list from the drop down and finally the code for doing actual code from moving the files.

Sample code to read ListID and item IDs

var context = SP.ClientContext.get_current();
    var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
    var listId = SP.ListOperation.Selection.getSelectedList();
    var itemIds = "";
    for (var i = 0; i < selectedItems.length; i++) {
        itemIds += selectedItems[i].id + ",";
    }
    var pageUrl = SP.Utilities.Utility.getLayoutsPageUrl(
        '/NY.ExportVersionHistory/ExportVersionHistory.aspx?ID=' + itemIds + '&List=' + listId);

Sample code to read the ID from the code behind of layouts page:

if (Request["List"] != null && Request["ID"] != null)
                {
                    listID = Request["List"].ToString();
                    itemIDs = Request["ID"].ToString().Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                }
  • Please confirm if you mean below: In a new page layout I would have a webpart which contains all document library , submit button and source library. I need to select files from library say 10 files, and select library name from dropdown menu and click on submit. Then 10 files from current library get moved to selected library – Brishal Mar 4 '14 at 13:19
  • See my updated answer. – Nadeem Yousuf Mar 4 '14 at 13:51

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.