1

Below code is working fine, but inside for-loop if I put alert,then moving multiple file but if I remove, only one file moving.

I do not want any alert/pop up. Please suggest.

        <Groups Id="Ribbon.MoveFiles.Groups">
          <Group
          Id="Ribbon.MoveFiles.MoveFilesCustomGroup"
          Description="This is a EMC group!"
          Sequence="52"
           Title="Move Files From Draft To Client"
          Template="Ribbon.Templates.EMCCustomTemplateLayout">
            <Controls Id="Ribbon.MoveFiles.MoveFilesCustomGroup.Controls">
              <Button
              Id="Ribbon.MoveFiles.MoveFilesCustomGroup.MoveFilesDoc"
              Command="MoveFiles.MoveFilesDoc"
              Sequence="17"

              LabelText="Move selected files – Draft to Client"
              Image32by32="/Style Library/petronas/images/Archive.png"

              TemplateAlias="cust2"/>

            </Controls>
          </Group>
        </Groups>
      </Tab>
    </CommandUIDefinition>
    <CommandUIDefinition Location="Ribbon.Templates._children">
      <GroupTemplate Id="Ribbon.Templates.FilesMoveTemplateLayout">
        <Layout
        Title="OneLargeTwoMedium"
        LayoutTitle="OneLargeTwoMedium">
          <Section Alignment="Top" Type="OneRow">
            <Row>
              <ControlRef DisplayMode="Large" TemplateAlias="cust1" />
            </Row>
          </Section>
          <Section Alignment="Top" Type="OneRow">
            <Row>
              <ControlRef DisplayMode="Large" TemplateAlias="cust2" />
            </Row>

          </Section>
        </Layout>
      </GroupTemplate>
    </CommandUIDefinition>
  </CommandUIDefinitions>

  <CommandUIHandlers>
    <CommandUIHandler
     Command="MoveFiles.MoveFilesDoc"
    CommandAction="javascript:
            var context = SP.ClientContext.get_current();
            var web = context.get_web();
            context.load(web);
            var _destinationlib = web.get_lists().getByTitle('Client (Final) Folder');              
            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);

            if(count==0){
            alert('Please choose at least  one file to move from Draft Folder.');
            }
            for(var i in selectedItems)
            {

                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() + '/' +  'Documents' +  '/' + File.get_name();
                               // alert('File destination Now moving to: ' + _destinationlibUrl);
                                File.moveTo(_destinationlibUrl, 1);
                                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);

                                });
                            }
                        },
                        function (sender, args) {
                            alert('Error occured' + args.get_message());
                        }
                    );
            }"/>

  </CommandUIHandlers>
</CommandUIExtension>

  • Your problem is not clear. Can you add explain it more? – Atish Dipongkor - MVP Apr 12 '16 at 9:48
  • The trick is to avoid making any asynchronous calls from within in a loop. Refer to Viraj's answer. – Thriggle Apr 26 '16 at 15:37
0

Try this code:

   var files = [];
   var context = SP.ClientContext.get_current();
   var web = context.get_web(); context.load(web);
   var _destinationlib = web.get_lists().getByTitle('Client (Final) Folder');
   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);
   if (count == 0)
   {
       alert('Please choose at least  one file to move from Draft Folder.');
   }
   for (var i in selectedItems)
   {
       var currentItem = currentLib.getItemById(selectedItems[i].id);
       context.load(currentItem); var fle = currentItem.get_file();

       files.push(fle);
       context.load(fle);
   }
   context.executeQueryAsync(
       function (sender, args)
       {
           for (var i = 0; i < files.length; i++)
           {
               var File = files[i];
               if (File != null)
               {
                   var _destinationlibUrl = web.get_serverRelativeUrl() + '/' + 'Documents' + '/' + File.get_name(); File.moveTo(_destinationlibUrl, 1);
                   notifyId = SP.UI.Notify.addNotification('Moving file ' + File.get_serverRelativeUrl() + ' to ' + _destinationlibUrl, true);
                   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);
                   });
               }
           }
       }, function (sender, args) {
           alert('Error occured' + args.get_message());
  });

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.