Tuesday, 13 September 2011

SharePoint 2010 UI: custom action to launch "Move To" for a list item

Mood = :proud:

I've worked out how to create a custom menu action in a SharePoint 2010 list view that allows a user to move a single file (with meta-data intact) to another location similar to the functionality available in Site Content and Structure. In fact, I have made use of the Site Content and Structure page directly within a modal dialog. You can use SharePoint Designer to create a custom list item menu action. I won't go into detail here on how to do that as there is plenty of information available on the web. In the "Create Custom Action" dialog, tick the radio button next to "Navigate to URL:" and paste in the following javascript URL all on a single line (you will need to change the webId value to your own):

javascript:var listId='{ListId}'.toLowerCase().replace(/[{}]/g, ""); var webId='4836a39d-e154-4822-8826-8bcd5029cad2'; var dlg=SP.UI.ModalDialog.showModalDialog({ url: "{SiteUrl}/_Layouts/sitemanager.aspx?FilterOnly=1&SmtContext=SPList:" + listId + "?SPWeb:" + webId + ":&SmtContextExpanded=True&Filter=1&pgsz=100&vrmode=False", dialogReturnValueCallback: function(res, val) {location.reload(true);}}); var idString="SPListItem:{ItemId}?SPList:" + listId + "?SPWeb:" + webId; var item; var func; var doc; var win; setTimeout(func = function() {for (var i=0; i < window.top.frames.length; i++) {if (window.top.frames[i].location.href.indexOf("sitemanager.aspx") != -1) {win = window.top.frames[i]; doc = win.document;}}; if (doc == null || doc.forms.length == 0) {return setTimeout(func, 5000);}; for (var i=0; i < doc.forms[0].elements.length; i++) {var elem=doc.forms[0].elements[i]; if (elem.type == 'checkbox' && elem.id == idString) {item = elem; break;}}; if (item == null) {return setTimeout(func, 500);}; win.SmtSingleObjectMove(idString+":", false); item.checked=true; win.SmtGridSelectItem(item);}, 100); return false;


I had to use some setTimeout calls in order to ensure that the objects I was referring to were present. There is a particularly long timeout of 5 seconds that allows the dialog to hopefully complete rendering before it attempts to launch the popup with the tree view that actually selects the destination. All efforts to encapsulate this in the dialog's onload function met with failure. Perhaps one day I'll work that one out. Closing the dialog will refresh the view. This should work for any list or library in your SharePoint web application. Unfortunately there is no URL token corresponsing to the guid of the web application, so this will need to be provided as a hard-code value as I have done. I hope this is helpful to someone, as it took quite a lot of trial and error.

No comments:

Post a Comment

Flayman on LiveJournal (old)