SharePoint 2010 CustomActions:EnabledScript和executeQueryAsync

时间:2011-01-28 15:35:32

标签: sharepoint-2010 custom-action

我有一个 CustomAction ,它会在功能区上添加一个按钮。我的 CommandHandler 指定 EnabledScript ,用于确定何时启用/禁用按钮。见下文:

<CommandUIHandlers>
 <CommandUIHandler
  Command="CreateSiteGroup"
  CommandAction="javascript:CreateCreateSiteGroup();" 
  EnabledScript="javascript:CreateCreateSiteGroupButtonEnabled();">
 </CommandUIHandler>
</CommandUIHandlers>

CreateCreateSiteGroupButtonEnabled()函数只需返回true / false,但我必须在其中使用 executeQueryAsync 函数,以便我可以加载属性一些对象。显然,因为现在该方法是异步执行的,所以永远不会返回true / false值。

我的代码如下所示:

function CreateContributorsGroupButtonEnabled() {
 this.clientContext = new SP.ClientContext.get_current();
 this.web = this.clientContext.get_web();
 this.clientContext.load(this.web, "Title");
 this.clientContext.executeQueryAsync(Function.createDelegate(this, this.OnContextSucceeded), Function.createDelegate(this, this.OnQueryFailed));
}

function OnContextSucceeded() {
 this.siteGroups = web.get_siteGroups();
 this.clientContext.load(this.siteGroups);
 this.clientContext.executeQueryAsync(Function.createDelegate(this, this.OnQuerySucceeded), Function.createDelegate(this, this.OnQueryFailed));
}

function OnQuerySucceeded() {
 var contributorsGroupTitle = String.format("{0} Contributors", web.get_title());
 var siteGroupEnumerator = this.siteGroups.getEnumerator();

 while (siteGroupEnumerator.moveNext()) {
  var siteGroup = siteGroupEnumerator.get_current();

  if (siteGroup.get_title().toLowerCase() == contributorsGroupTitle.toLowerCase())
   return false;
 }

 return true;
}

有谁知道如何从异步函数返回true / false?或者,我如何判断具有特定名称的网站群组是否已存在没有使用 executeQueryAsync

1 个答案:

答案 0 :(得分:2)

当使用异步调用来检查功能区中是否必须有一个按钮时,您必须在异步方法完成时告诉功能区以检查是否必须更新按钮的状态。

在Core.js中,您可以找到名为RefreshCommandUI()的函数。此功能使功能区刷新,并在功能区按钮上调用EnableScript功能。

在代码中,必须设置一个变量来跟踪按钮的状态(启用或禁用)。

此致,Anita