2

I am trying to get selected rows in the list and select ids for the pdf generator. Debug stops after context.load(rows) and nothing happens after that till I reload the page. If remove code for selecting many ids and select just one and write context.load(row) - it works good. So how to get more than one row?

Elements.xml:

    <CustomAction Id="CustomAction"
        RegistrationId="100"
        RegistrationType="List"
        Location="CommandUI.Ribbon"
        Rights="ManageLists"
        Sequence="25"
        Title="Generate pdf">
        <CommandUIExtension>
            <CommandUIDefinitions>
                <CommandUIDefinition Location="Ribbon.ListItem.Actions.Controls._children">
                    <Button Id="GeneratePdf"
                        Alt="Generate pdf by template"
                        Description="Generate pdf by template"
                        Sequence="25"
                        Command="GeneratePdf"
                        LabelText="Generate pdf"
                        TemplateAlias="o1" />
                </CommandUIDefinition>
            </CommandUIDefinitions>
            <CommandUIHandlers>
                <CommandUIHandler Command="GeneratePdf"
                    EnabledScript="javascript:
                    function enableGeneratePdf() {
                        var items = SP.ListOperation.Selection.getSelectedItems();
                        return (items.length >= 1);
                    }
                    enableGeneratePdf();"
                    CommandAction="javascript:
                    function getSelectedIds() {
                        var ctx = SP.ClientContext.get_current();
                        var items = SP.ListOperation.Selection.getSelectedItems(ctx);
                        var selectedItems = '';
                        for (index in items) {
                            selectedItems = selectedItems + ',' + items[index].id;
                        }
                        return selectedItems.substring(1, selectedItems.length);
                    }
//GeneratePdf('{SelectedListId}');
                    GeneratePdf(getSelectedIds());" />
            </CommandUIHandlers>
        </CommandUIExtension>
    </CustomAction>
    <Control Id="AdditionalPageHead" ControlSrc="~/_controltemplates/generator.ascx" Sequence="25"/>

here is generator.ascx:

    <script language="ecmascript" type="text/ecmascript">
        var docsList;
        var web;
        var context;
        var rows;
//var row;
        var columnName = "Title";

        function GeneratePdf(rowNumbers) {
            context = new SP.ClientContext.get_current();
            web = context.get_web();
            docsList = web.get_lists().getByTitle("Documents");
            var ids = rowNumbers.split(',');
            rows = new Array(ids.length);
            var count = 0;      
            for (index in ids) {
                rows[count] = docsList.getItemById(index);
                count++;
            }
            //row = docsList.getItemById(rowNumber);
            context.load(web);
            context.load(docsList);
            context.load(rows);
            //context.load(row);
            context.executeQueryAsync(OnDocumentsListsLoaded);
        }

        function OnDocumentsListsLoaded() {
            var ids = '';
            for (index in rows) {
                ids = ids + ',' + row.get_item(columnName);
            }
            //var id = row.get_item(columnName);
            window.location = "/_layouts/Documents/DownloadGeneratedPdf.aspx?ids=" + ids.substring(1, ids.length);
        }
    </script>
5
  • Have you tried to call context.load on each row instead of on the array? Jul 27, 2012 at 12:36
  • Does the DownloadGeneratedPdf.aspx really expect a list of Titles in the query parameter ids? Jul 27, 2012 at 12:41
  • @PerJakobsen 1)yes, the same result 2) yes, the first column called 'Title', but this is id actually at developing stage
    – WISEMAN
    Jul 27, 2012 at 12:48
  • In the function OnDocumentsListsLoaded the for loop defines the "index" variable, but "row" is used instead.
    – Matthias
    Jul 27, 2012 at 14:17
  • @Matthias thank you, but it is not a critical error, ecma script supports this code and will fail at this line, but OnDocumentsListsLoaded function does not start
    – WISEMAN
    Jul 30, 2012 at 7:19

2 Answers 2

0

This should work:

var items = SP.ListOperation.Selection.getSelectedItems();

var ctx = SP.ClientContext.get_current();
var web = ctx.get_web();
var currentListGUID = SP.ListOperation.Selection.getSelectedList();

var list = web.get_lists().getById(currentListGUID);

var rows = new Array();
for(var i = 0; i < items.length; i++)
{                           
  var row = list.getItemById(items[i].id);
  ctx.load(row);
  rows.push(row);
}

ctx.executeQueryAsync(function ()
{                    
 var ids = '';
 for (var i = 0; i < rows.length; i++)
 {
   var r = rows[i];
   ids = ids + ',' + r.get_item('Title');
 }
 alert(ids);
},

function(sender, args)
{
  alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());
});
1
  • Thanks for your code, but after first for loop (before ctx.executeQueryAsync) the rows variable has length 0.
    – WISEMAN
    Jul 30, 2012 at 7:11
0

Sorry guys, there was my mistake. Here is working code of generator.ascx:

    function GeneratePdf(rowNumbers) {
        context = new SP.ClientContext.get_current();
        web = context.get_web();
        docsList = web.get_lists().getByTitle("Documents");
        var ids = rowNumbers.split(',');
        rows = [];

        context.load(web);
        context.load(docsList);
        for (i in ids) {
            rows[i] = docsList.getItemById(ids[i]);
            context.load(rows[i]);
        }

        context.executeQueryAsync(OnDocumentsListsLoaded);
    }

    function OnDocumentsListsLoaded() {
        var ids = [];
        for (i in rows) {
            ids.push(rows[i].get_item(columnName));
        }
        window.location = "/_layouts/Documents/DownloadGeneratedPdf.aspx?ids=" + JSON.stringify(ids);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.