vendredi 14 décembre 2012

How to activate custom action based on SharePoint Right

This javascript function is a helper to enable or disable custom action for SharePoint 2010 based on ContentTypeID, Permissions and number of selected element.

You can call this function in the CommandUIHandler EnabledScript attribute

Create a custom action for Ribbon :

  <CustomAction Id="Ribbon.AlertPerso"
              Location="CommandUI.Ribbon" Title="Alert Perso">
        <CommandUIDefinition Location="Ribbon.Documents.Manage.Controls._children">
          <Button Id="Ribbon.CreerAlerter.Button"
                  LabelText="Créer une alerte personnalisée"
                  Command="AlerteCommandeDoc" />
        <CommandUIHandler Command="AlerteCommandeDoc"
                          CommandAction="javascript:window.location='votre page cible."
<!-- Replace the first attribute by you content type id, the second one with the SPPermission needed, and the thrid with the number of element that have to be selected (list of sharepoint permission kind : -->            EnabledScript="javascript:ActiveTypeContenuDroit('0x0101002D02DF72FAFB4EAB9446F92337C759AE01',SP.PermissionKind.,1);"/>

Ajout de la fonction dans core.js :

//Id of SP.ListItem
var ItemSelectedID;
//Content Type ID
var ItemSelectedContentType;
var ItemSelectedItem;
//Right on the item.
var ItemSelectedRight;

//Check if the custom action is enabled

//ContenuID : Content Type Id that the item must match.
//Permission : Permissions needed, if null the custom action will be enabled.
function ActiveTypeContenuDroit(ContenuID, Permission, TypeAction) {
    var result = false;
    var selectedItems = SP.ListOperation.Selection.getSelectedItems();
    var countItems = CountDictionary(selectedItems);

    //when the action is available for all items.
    if (TypeAction == 0 && countItems == TypeAction) {
        result = true;
    } else {
        //when the action is available when only one item is selected.
        if (countItems == TypeAction && TypeAction == 1) {
            //when the objet is not defined
            if (this.ItemSelectedID == null) {
                var listGuid = SP.ListOperation.Selection.getSelectedList();
                ItemSelectedID = selectedItems[0]['id'];
                ListItemContentTypePermission(this.ItemSelectedID, listGuid);
            //when the charged item is not previous item that was charged
            else if (this.ItemSelectedID != selectedItems[0]['id']) {
                this.ItemSelectedID = selectedItems[0]['id'];
                var listGuid = SP.ListOperation.Selection.getSelectedList();
                ListItemContentTypePermission(this.ItemSelectedID, listGuid);
            //compare with the content type id parameter
            else if (this.ItemSelectedContentType != null) {
                if (ItemSelectedContentType.toString().indexOf(ContenuID.toString()) != -1) {
                    //Check rigth on the object.
                    if (Permission != null && ItemSelectedRight != null) {
                        if (ItemSelectedRight == null) {
                            alert("ItemSelectedRight null");
                        else {
                            //Check rigth on the object.
                            var permettre = ItemSelectedRight.has(Permission);
                            if (permettre) {
                                { result = true; }
                    else {
                        result = true;
        else {
            //when more than one selected value is possible
            if (TypeAction > 1) {
                    // .. action when two item selected.
                alert("not defined ActiveTypeContenuDroit " + TypeAction);
                result = false;
    return result;

//Initialize SP.ListItem and get the property  Title  ContentTypeId EffectiveBasePermissions
function ListItemContentTypePermission(ItemId, listGuid) {
    var clientContext = new SP.ClientContext();
    var web = clientContext.get_web();
    var lists = web.get_lists();
    var list = lists.getById(listGuid);
    ItemSelectedItem = list.getItemById(ItemId);
    clientContext.load(ItemSelectedItem, 'Title', 'ContentTypeId', 'EffectiveBasePermissions');
    clientContext.executeQueryAsync(onListItemContentTypePermission, failedListItemContentTypePermission);

//Get the needed property.
function onListItemContentTypePermission(sender, args) {
    ItemSelectedRight = ItemSelectedItem.get_effectiveBasePermissions();
    ItemSelectedContentType = ItemSelectedItem.get_item('ContentTypeId');

function failedListItemContentTypePermission(sender, args) {
    alert('Request failed. \nError: ' + args.get_message() + '\nStackTrace: ' + args.get_stackTrace());

SharePoint Client Side Object Model (CSOM)

Un petit coup de chapeau pour ce très bon post qui trait du CSOM dans SharePoint 2010, SharePoint 2007, SharePoint 2013.

Et notamment sur la récupération du SP.ClientContext< ainsi que le fonctionnement des executeQueryAsync