用于读取值的功能区命令

Ribbon Command to read values

我的 javascript 不是最好的,想知道是否有人可以帮助我解决这个问题。本质上,我有一个带有名为 PGCount 的整数列的库,我希望能够单击此按钮并将其添加到定义的变量 pgcount 的值中,它目前处于警报状态,但我对此有更大的计划,如果只是为了获得期望的结果。

很遗憾,它正在计算第一项。

这是整个模块

<?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>

如有任何帮助,我们将不胜感激!

好吧,我必须说,在使用它一整天后,我感到非常兴奋! 这还不包括类似项目遇到的相同问题,因为这会加载多个项目。总是会发生冲突。

现在我知道你们中的很多人都很酷 java 头脑会到处说这是错误的,我知道,尤其是检查项目计数等等,但我认为这不是太破旧的,我知道它会帮助别人。如果愿意,请随时整理代码:)

<?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>