2

I'm trying to approve selected items from a list by pressing a custom button in the Ribbon. I can get the selected items. I can get the fields of the selected items.I can even update those fields.

The problem is approving an Item. I can't find a way to do it. I've tried something similar to what it's done with CSOM in C# but my only option is the javascript object model. How do I get the Approved status? How do I change it? (note that I have content approval enabled)

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

        for (var idx in selectedItems) {                       
            var selectedItem = list.getItemById(parseInt(selectedItems[idx].id));

            var listItem = selectedItem;
            context.load(selectedItem);
            context.executeQueryAsync(Function.createDelegate(this, onQuerySucceeded), Function.createDelegate(this, onQueryFailed)); }

function onQuerySucceeded() {
            alert("onQuerySucceeded!");
            console.log(selectedItem.get_item("ModerationStatus"));
            if (selectedItem.ModerationInformation != null) {
                selectedItem.ModerationInformation.Status = SPModerationStatusType.Approved;
                selectedItem.ModerationInformation.Comment = "This is comment generated";
                selectedItem.update();
            }                   

            context.executeQueryAsync(function () {
                alert("urray!");
            }, function () {
                alert("fail");
            });
        };

        function onQueryFailed() {
            alert("fail");
        };
  • I'm not familiar with the Microsoft API. I'm using the web services to deal with the Sharepoint list. With Web Services you'd have to set the column "_ModerationStatus" (with underscore) to 0 to have your item approved.... If you don't mind using a third party library you can look at aymkdn.github.io/SharepointPlus/symbols/… -- but it hasn't been tested with Sharepoint 2013/Office 365 – AymKdn Jun 8 '14 at 9:37
  • Thanks for your help AymKdn. But I prefer to stay away from 3rd parties as much as I can. – Txugo Jun 9 '14 at 11:03
4

The following example demonstrates how to approve items selected in List View via JSOM

function approveItems(success,error) {
   var context = SP.ClientContext.get_current();
   var listId = SP.ListOperation.Selection.getSelectedList();
   var list = context.get_web().get_lists().getById(listId);
   var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);

   for (var idx in selectedItems) {                       
       var selectedItem = list.getItemById(parseInt(selectedItems[idx].id));

        selectedItem.set_item('_ModerationStatus',0);
        selectedItem.set_item('_ModerationComments','');
        selectedItem.update();
   }  

   context.executeQueryAsync(
       function(){
           success();
       }, 
       error
   );   

}

Note: the approval is performed as a batch operation for selected items

Usage

approveItems(
 function(){
     console.log('Approved');
 },
 function(sender,args){
     console.log(args.get_message());
 }
);
  • that did it! thanks. the documentation on msdn is so poor that I couldn't find anything about this. the best that I have is:msdn.microsoft.com/en-us/library/jj163201.aspx. Can you advise me a better link about JSOM? – Txugo Jun 9 '14 at 12:22
  • 1
    MSDN doesn't cover all the cases when working with JSOM I'm afraid. I would say SP JavaScript library code base in some cases is the only way to figure out.. – Vadim Gremyachev Jun 9 '14 at 18:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.