Sharepoint 2013 Custom Ribbon Action To Update a List Item Field

In this post I’ll show how to develop a Custom Ribbon Action, that iterates through selected document library (with a custom column named Category) items and updates  a field value (category).

I want to do another post to  get the value from a modal dialog, but for now, the updated value will be hard coded.

I started creating a SharePoint 2013 empty project in Visual Studio 2012 (selected the deployment as a farm solution, but could also be sandbox).

Add a new module to my project called “CustomAction”, and add the following code, to elements.xml file:

 

<?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="MoveDocs Ribbon Button">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition
          Location="Ribbon.Library.Share.Controls._children">
          <Button Id="Ribbon.Library.Share.NewRibbonButton"
            Command="MoveDocsButtonCommand"
            LabelText="UpdateCategory"
            TemplateAlias="o2" />
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler
          Command="MoveDocsButtonCommand"
          CommandAction="javascript:
            var context;
            var currentItem ;
          
        
          function onQuerySucceededUpdate (sender, args) {
              alert('item updated');
          }    

          function onQueryFailedUpdate(sender, args) {
            alert('Error occured' + args.get_message());
          }  
          
          function UpdateCategory(val)
          {
            context = SP.ClientContext.get_current();
            var web = context.get_web();
             context.load(web);
             var currentlibid = SP.ListOperation.Selection.getSelectedList();
             var currentLib = web.get_lists().getById(currentlibid);
             var selectedItems = SP.ListOperation.Selection.getSelectedItems(context);
             for(var i in selectedItems)
              {
                currentItem = currentLib.getItemById(selectedItems[i].id);
                context.load(currentItem);
                if(currentItem != null) {
                  currentItem.set_item('Category', val);
                  currentItem.update();
                }
                context.executeQueryAsync(Function.createDelegate(this, onQuerySucceededUpdate), Function.createDelegate(this, onQueryFailedUpdate));
              }// End for
          
          }//End UpdateCategory

          
          UpdateCategory('Cat1');
           
"/>
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
  
  <Module Name="CustomAction">
  </Module>
</Elements>
Advertisements

Tags: ,

2 responses to “Sharepoint 2013 Custom Ribbon Action To Update a List Item Field”

  1. Joe Wilson says :

    Obrigado Raquel, thanks for sharing your Custom Ribbon Action. Hope all’s well in Lisbon.

  2. Rob says :

    Thank you very much for sharing this! I was searching for something very similar for a couple of hours already. Great work!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: