Thursday 3 November 2011

Manipulating the Custom Send To Destination in document library with javascript

You all know that you can send a copy of a document from one document library to another by using the dropdown on the “Name” column and selecting “Send to > Other location”.
Some of you also know that you can add a predefined “send to location” under Document library settings > Advanced settings.
The irritating fact is that you can only add one custom location here.

Here is a method for setting this custom send to location by javascript – and being able to manipulate it for each view – or by a dropdown select. To set the location for a view – put this code in a CEWP below the list view:
1<script type="text/javascript" src="../../Javascript/jquery-1.3.2.min.js"></script>
2<script type="text/javascript">
3// Name displayed on the link
4ctx.SendToLocationName = "My custom send to location";
5// The full url to the document library to send to - i used ctx.HttpRoot as it reflects the current root
6ctx.SendToLocationUrl = ctx.HttpRoot + "/TestLibrary";
7</script>
IMG
To have a dropdown select, use this code:
01<script type="text/javascript" src="../../Javascript/jquery-1.3.2.min.js"></script>
02<script type="text/javascript">
03// Define array of locations
04var mySendToArr = ['<select location>|',
05         'Archive|http://www.mysendtolocation.com/Archive',
06         'Publish|http://www.mysendtolocation.com/Publish',
07         'AnotherPlace|http://www.mysendtolocation.com/AnotherPlace'];
08
09// Build the dropdown
10var mySelect = $("<select id='myCustomSendToSelector'></select>");
11$.each(mySendToArr,function(idx,item){
12    var split = item.split('|');
13    var opt = $("<option></option>");
14    opt.text(split[0]);
15    opt.val(split[1]);
16    mySelect.append(opt);
17});
18
19// Place the dropdown
20$("#onetidPageTitle").append("<div style='font-size:10px;float:right;margin-top:-15px' id='insertMySendToSelectHere'>Send to location:&nbsp;</div>");
21$("#insertMySendToSelectHere").append(mySelect);
22
23// Add onchange
24$("#myCustomSendToSelector").change(function(){
25    var SendToLocationName = $(this).find('option:selected').text();
26    var SendToLocationUrl = $(this).find('option:selected').val();
27    if(SendToLocationUrl!=''){
28        ctx.SendToLocationName = SendToLocationName;
29        ctx.SendToLocationUrl = SendToLocationUrl;
30    }else{
31        ctx.SendToLocationName = "";
32        ctx.SendToLocationUrl = "";
33    }
34});
35</script>
This script adds a dropdown above the view selector like this
More info at
http://sharepointjavascript.wordpress.com/2010/02/17/manipulating-the-custom-send-to-destination-in-document-library-with-javascript/

No comments:

Post a Comment