0

I would like to open a new window when the user clicks a button so that the user can specify a destination url (some doc library in a subsite). Is this possible to do with a sandboxed solution? One of my peers recommended I look into copying files with an event receiver, but the only ones I've had experience with were ItemAdding/ItemAdded. How would I trigger the event when the item is selected and the button on the ribbon is clicked?

I would preferably like to iterate for each selected item, but if I can even do something like this for one item, I'd be fine with that.

edit: The OOTB send to and explorer view options don't satisfy my user's requirements as they would like to flag an error if the destination url doesn't contain a certain string.

1

This is how you can copy selected documents by clicking on a ribbon button to destination Library. You need to create a custom action which will get the selected item ids and pass them to a pop-up page. The page may contain a textbox for entering the destination Url and a submit button. The button click handler will read the selected items and copy them to destination library. Here is an example of the Custom Action:

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
   Id="COB.SharePoint.Ribbon.NewControlInExistingGroup"
   Location="CommandUI.Ribbon.ListView"
   RegistrationType="List"
   RegistrationId="100">
    <CommandUIExtension>
      <CommandUIDefinitions>
        <CommandUIDefinition Location="Ribbon.ListItem.Workflow.Controls._children">
          <Button Id="COB.SharePoint.Ribbon.NewControlInExistingGroup.Notify"
                  Command="COB.Command.NewControlInExistingGroup.Notify"
                  Sequence="21" 
                  Image16by16="/_layouts/$Resources:core,Language;/images/formatmap16x16.png" 
                  Image16by16Top="-48" Image16by16Left="-240"
                  Image32by32="/_layouts/$Resources:core,Language;/images/formatmap32x32.png" 
                  Image32by32Top="-448" Image32by32Left="-384"
                  Description="Uses the notification area to display a message."
                  LabelText="Approve/Reject Selection"
                  TemplateAlias="o1"/>
        </CommandUIDefinition>
      </CommandUIDefinitions>
      <CommandUIHandlers>
        <CommandUIHandler
          Command="COB.Command.NewControlInExistingGroup.Notify"
          EnabledScript="javascript:enableApprovalAll();"
          CommandAction="javascript: showApproveAll(); "/>
      </CommandUIHandlers>
    </CommandUIExtension>
  </CustomAction>
  <CustomAction
   Id="COB.Command.NewControlInExistingGroup.Notify.Script"
   Location="ScriptLink"
   ScriptSrc ="/_layouts/SharePoint.ApproveRejectTest/Scripts/ApproveReject.js"/>
</Elements>

This is the code in JS file which gets selected item ids and passes them to a popup page. Note that in the example the page is stored in /_layouts folder. However, since you want a sandboxed solution you will need to put the page in a module. And finally the code to copy documents need to be written in the code behind of the page.

function showApproveAll() {
    var ctx = new SP.ClientContext.get_current();
    var ItemIds = "";
    //get current list id
    var listId = SP.ListOperation.Selection.getSelectedList();
    //get all selected list items
    var selectedItems = SP.ListOperation.Selection.getSelectedItems(ctx);

    //collect selected item ids
    for (var i = 0; i < selectedItems.length; i++) {
        ItemIds += selectedItems[i].id + ",";
    }

    //prepare cutom approval page with listid 
    //and selected item ids passed in querystring
    var pageUrl = SP.Utilities.Utility.getLayoutsPageUrl(
        '/SharePoint.ApproveRejectTest/ApproveAll.aspx?ids=' + ItemIds + '&listid=' + listId);
    var options = SP.UI.$create_DialogOptions();
    options.width = 420;
    options.height = 250;
    options.url = pageUrl;
    options.dialogReturnValueCallback = Function.createDelegate(null, OnDialogClose);
    SP.UI.ModalDialog.showModalDialog(options);
}

//used to determine whether the 'approve/reject selection' 
//ribbon will be enalbed or disabled
function enableApprovalAll() {
    var ctx = new SP.ClientContext.get_current();
    return SP.ListOperation.Selection.getSelectedItems(ctx).length > 1;
}


//called on dialog closed
function OnDialogClose(result, target) {
    //if ok button is clicked in dialog, reload the grid.
    if (result == SP.UI.DialogResult.OK) {
        location.reload(true);
    }
}

The following reference shows how documents can be approved/rejected in bulk. This is not about copying documents. However, this will help you achieve what you want as you will need to apply the same logic in getting documents copied. http://ranaictiu-technicalblog.blogspot.in/2011/05/sharepoint-2010-approvereject-multiple.html

| improve this answer | |
  • Thanks for this example, but my copy cannot be done with JS. The only js method I have seen work effectively for copying documents is the CopyIntoItems web service which captures the binary/field info associated with documents by a url. The document url I need to use is a previous version (stored with the https:www.mysite.com/mylibrary/_vti_history/513/docName.docx). It works for the current versions of documents (https:www.mysite.com/mylibrary/docName.docx), but I do not receive a response when using GetItem from the first url. So I was hoping to get the binary with C#. – Dinerdo Apr 19 '14 at 3:41

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.