Wednesday, 10 November 2010

Code example for EnabledScript element in Ribbon XML

Andrew Connel explains how to enable and disable buttons in the Ribbon based on a condition.

I need to check a field value on the selected item and the item permission and then enable / disable multiple buttons. I assume that you have some knowledge about the Ribbon XML structure :).


//Should only be enabled when IssueStatus == Report
      <CommandUIHandler
Command="Tab.CloseIssue"
CommandAction="javascript:EditDeviation('{ListId}');"
EnabledScript="javascript:SingleEnable('Report',SP.PermissionKind.editListItems);"/>

//Should always be enabled
      <CommandUIHandler
Command="Tab.CurrentStatus"
CommandAction="javascript:DisplayStatus('{ListId}');"
EnabledScript="javascript:SingleEnable('All',,SP.PermissionKind.viewListItems);"/>


function SingleEnable(EnableForStatus, PermissionKind) {

var result = false;

var selectedItems = SP.ListOperation.Selection.getSelectedItems();
var ci2 = CountDictionary(selectedItems);
if (ci2 == 1) {

if (this.IssueRibbonItemId == null) {

this.IssueRibbonItemId = selectedItems[0]['id'];
var listGuid = SP.ListOperation.Selection.getSelectedList();
getStatus(this.IssueRibbonItemId, listGuid);

}
else if (this.IssueRibbonItemId != selectedItems[0]['id']) {

this.IssueRibbonItemId = selectedItems[0]['id'];
var listGuid = SP.ListOperation.Selection.getSelectedList();
getStatus(this.IssueRibbonItemId, listGuid);
}
else if(this.IssueRibbonStatus != null){

if (this.IssueRibbonStatus == EnableForStatus || EnableForStatus == '') {
if(this.IssueItem.get_effectiveBasePermissions().has(PermissionKind)){
result = true;
}
}
}
}

return result;
}

function getStatus(ItemId, listGuid) {

var clientContext = new SP.ClientContext();
var web = clientContext.get_web();
var lists = web.get_lists();
var list = lists.getById(listGuid);
this.IssueItem = list.getItemById(ItemId);

clientContext.load(IssueItem, 'IssueStatus', 'EffectiveBasePermissions');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onStatusQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}


function onStatusQuerySucceeded(sender, args) {
this.IssueRibbonStatus = this.IssueItem.get_item("IssueStatus");
RefreshCommandUI(); //Makes the EnabledScrip method to be enabled again.
}

function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

Monday, 1 November 2010

Some TaxonomyField things and code

Before a new term can be used for tagging in a web site it needs to be added to the hidden list "TaxonomyHiddenList".
Several suggestions how to add a new term with code has been posted and this is my conclusion:

Adding a new term to an item:

TaxonomyField tf = item.Fields["ColumnName"] as TaxonomyField;
tf.SetFieldValue(item,term);
item.Update();

This doesn´t work for new items:

int hiddenTermId = -1;
int[] wssIds = TaxonomyField.GetWssIdsOfTerm(site, TermStore.Id, TermSet.Id, Term.Id, false, 1);

if(wssIds.Length > 0)
hiddenTermId = wssIds[0];

TaxonomyFieldValue tfv = new TaxonomyFieldValue(termId + ";#" + term.Name + TaxonomyField.TaxonomyGuidLabelDelimiter + term.Id.ToString())

or this:

string termString = String.Concat("-1", SPFieldMultiColumnValue.Delimiter, term.GetDefaultLabel(1033));
TaxonomyFieldValue tfv = new TaxonomyFieldValue(termString);

Add a comment if you have any question.