0

My javascript is not the best and was wondering if someone could help me out with this. Essentially I have a library with an integer column called PGCount, I want to be able to click this button and it adds to the value of the defined variable pgcount, it is currently alert but I have greater plans for it, if only to get the desired results.

Sadly it is counting the first item twice.

Here is the whole module

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
  Id="Ribbon.Library.Actions.AddAButton"
  Location="CommandUI.Ribbon"
  RegistrationId="101"
  RegistrationType="List"
  Title="PGCount">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition
           Location="Ribbon.Library.Share.Controls._children">
          <Button Id="Ribbon.Library.Share.NewRibbonButton"
            Command="CountPGCount"
            LabelText="Page Count"
            TemplateAlias="o2" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler Command="CountPGCount" 
                          CommandAction="javascript: 
                            var listitem; 
                            var pgcounts = 0;
                            getWebProperties();
                            function getWebProperties() {
                                var ctx = new SP.ClientContext.get_current();
                                var currentWeb = ctx.get_web();
                                var currentListGuid = SP.ListOperation.Selection.getSelectedList();
                                var currentList = currentWeb.get_lists().getById(currentListGuid);
                                var selectedItems = SP.ListOperation.Selection.getSelectedItems();
                                for (i in selectedItems) {
                                    listitem = currentList.getItemById(selectedItems[i].id);
                                    ctx.load(listitem);
                                    ctx.executeQueryAsync(Function.createDelegate(listitem, function () {
                                        var c = listitem.get_fieldValues().PGCount;
                                        pgcounts+=c;     
                                    }), null);
                                };}
                                 setTimeout(function () {
                                  alert(pgcounts);
                                }, 3000);"
        EnabledScript="javascript:SP.ListOperation.Selection.getSelectedItems().length >= 1;" />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>

  <Module Name="Module1">
  </Module>
</Elements>

Any help would be appreciated!

1 Answers1

0

Well I must say I am trilled to bits after working with this all day! That is not counting the same issue experienced with similar projects as this load multiple items. A conflict always occurs.

Now I know a lot of you cool java heads are gonna say that's wrong here and there and I know that, especially checking the item counts and so on, but I don't think it is too shabby and it will I know help someone. Do feel free to tidy up the code if willing :)

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
  Id="Ribbon.Library.Actions.AddAButton"
  Location="CommandUI.Ribbon"
  RegistrationId="101"
  RegistrationType="List"
  Title="PGCount">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition
           Location="Ribbon.Library.Share.Controls._children">
          <Button Id="Ribbon.Library.Share.NewRibbonButton"
            Command="CountPGCount"
            LabelText="Page Count"
            TemplateAlias="o2" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler Command="CountPGCount" CommandAction="javascript:

                                var rows = new Array();
                                var countofitems = -1;
                                var countogpgcounts = 0;

                                getpgcounts();

                                function getpgcounts() {
                                    var context = new SP.ClientContext.get_current();
                                    var web = context.get_web();
                                    var lists = web.get_lists();
                                    var listId = SP.ListOperation.Selection.getSelectedList();
                                    var list = lists.getById(listId);
                                    var selectedItems = SP.ListOperation.Selection.getSelectedItems();

                                    rows = [];

                                    for (var i in selectedItems) {
                                        var id = selectedItems[i].id;
                                        rows[i] = list.getItemById(id);
                                        context.load(rows[i]);
                                        countofitems++;
                                    }

                                    context.executeQueryAsync(Function.createDelegate(this, show),Function.createDelegate(this, showError));
                                }

                                function show() {

                                    for (i in rows) {
                                        var thiscount = rows[i].get_item('PGCount');
                                        countogpgcounts += thiscount;
                                        if (i == countofitems) {
                                            alert(countogpgcounts);
                                        }
                                    }
                                }

                                function showError(sender, args) {
                                    throw new Error('request failed: ' + args.get_message() + '\n' + args.get_stackTrace());
                                }
"


        EnabledScript="javascript:SP.ListOperation.Selection.getSelectedItems().length >= 1;" />
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>

  <Module Name="Module1">
  </Module>
</Elements>