Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
inject additional javascript browser operations for developers
// ==UserScript==
// @name SharePoint 2010/2013 Dev Tools
// @namespace TiagoDuarte
// @description Adds new buttons near the ribbon for common useful actions
// @version Version 1.0
// @grant none
// @match http://*.yourdomain-to-match-here/*
////enable the line below to support sites without jquery (ootb). but may cause problems with sites that have jquery already
//// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// ==/UserScript==
// a function that loads jQuery and calls a callback function when jQuery has finished loading
function addJQuery(callback) {
var script = document.createElement("script");
script.setAttribute("src", "//code.jquery.com/jquery-2.0.0.min.js");
script.addEventListener('load', function() {
var script = document.createElement("script");
script.textContent = "window.jQ=jQuery.noConflict(true);(" + callback.toString() + ")();";
document.body.appendChild(script);
}, false);
document.body.appendChild(script);
}
function main() {
// Note, jQ replaces $ to avoid conflicts.
//associate buttons with functions and icons
//NOTE: /_layouts/15/1033/images/formatmap16x16.png != /_layouts/1033/images/formatmap16x16.png
function AddDevToolsButton(id, btn_title, img_x, img_y, callback)
{
var rootElement = '';
//dialogs only
if ((id == 'getIframeLink') && (window.location.href.indexOf('IsDlg') > -1))
{
rootElement = '.ms-cui-topBar2';
}
else
{
rootElement = '#siteactiontd';
}
//add new element to DOM
var toAdd = '<span class="s4-breadcrumb-anchor" id="' + id + '"><span class="ms-cui-img-16by16 ms-cui-img-cont-float" unselectable="on"><img alt="' + btn_title + '" title="' + btn_title + '" style="top: ' + img_y + '; left: ' + img_x + ';" src="/_layouts/1033/images/formatmap16x16.png" alt="" unselectable="on" /></span></span>';
$(rootElement).append(toAdd);
//bind callback to new element
eval(callback) ();
}
//command functions
function CmdExplorerView()
{
$('#customOpenInExplorer').click(function () {
var urlToOpen = 'file://' + (location.hostname + (location.port ? ':' + location.port : '') + location.pathname.substring(0, location.pathname.lastIndexOf('/')).replace('/Forms', ''));
window.prompt('Here is the path for file explorer', urlToOpen);
});
}
//command functions
function CmdItemCount()
{
$('#getListCount').click(function () {
//var urlToOpen = 'file://' + (location.hostname + (location.port ? ':' + location.port : '') + location.pathname.substring(0, location.pathname.lastIndexOf('/')).replace('/Forms', ''));
var itemcount = 0;
var context = SP.ClientContext.get_current();
var web = context.get_web();
var currentList = web.get_lists().getByTitle(ctx.ListTitle);
context.load(currentList);
context.executeQueryAsync(
//success
Function.createDelegate(this, function(sender, args){
alert('ItemCount: ' + currentList.get_itemCount());
}),
//failure
Function.createDelegate(this, function(sender, args){
//print error
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
})
);
});
}
function CmdWebTemplateID()
{
$('#getWebTemplateID').click(function () {
window.prompt('Here is this site Template ID', g_wsaSiteTemplateId);
});
}
function CmdSaveTemplate()
{
$('#saveSiteAsTemplate').click(function () {
var currentUrl = (window.location.href.substring(0, window.location.href.lastIndexOf('/')));
currentUrl = currentUrl.substring(0, currentUrl.lastIndexOf('/'));
window.location.href = currentUrl + "/_layouts/savetmpl.aspx";
});
}
function areYouSure(message)
{
return confirm('Are you sure you want to: ' + message);
}
function RecycleList()
{
$('#recycleList').click(function () {
if(!areYouSure("Recycle the current list?"))
{
return;
}
var context = SP.ClientContext.get_current();
var web = context.get_web();
var currentList = web.get_lists().getByTitle(ctx.ListTitle);
context.load(currentList);
currentList.recycle();
context.executeQueryAsync(
//success
Function.createDelegate(this, function(sender, args){
alert("List recycled!");
//refresh
window.location.href = _spPageContextInfo.webServerRelativeUrl;
}),
//failure
Function.createDelegate(this, function(sender, args){
//print error
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
})
);
});
}
function CmdCheckedOutDocs()
{
$('#seeCheckOutDocs').click(function () {
window.location.href = window.location.href + '?SortField=CheckoutUser&SortDir=Desc';
});
}
function CmdRefreshPage()
{
$('#refreshThisPage').click(function () {
window.location.href = window.location.href.replace('#', '');
});
}
function CmdSignInAsAnotherUser()
{
$('#signInAsAnotherUser').click(function () {
//TODO: navigate to http://{YourSharePointSiteURL}/_layouts/CloseConnection.aspx?Loginasdifferentuser=true?Source=ENCODEDURI
var signInUrl = "";
//detect version of sharepoint
if(_spPageContextInfo.webUIVersion === 4)
{
signInUrl = "/_layouts/closeConnection.aspx?loginasanotheruser=true";//2010
}
else
{
signInUrl = "/_layouts/15/closeConnection.aspx?loginasanotheruser=true";//2013
}
var sourceUrl = window.location.href;
//encode uri, plus encode ".aspx" since dot is not being encoded
var encodedSourceUrl = encodeURIComponent(sourceUrl).replace(/\./g, '%2E');
//support root site and tier 2 site collections
var currentWebUrl = _spPageContextInfo.webServerRelativeUrl;
if(currentWebUrl === "/")
{
currentWebUrl = "";
}
var navigateUrl = currentWebUrl + signInUrl + "&Source=" + encodedSourceUrl;
//console.log("navigating to " + navigateUrl);
window.location.href = navigateUrl;
});
}
function CmdIframeLink()
{
$('#getIframeLink').click(function () {
var iframeUrl = "not found";
//url is the page itself
if(window.location.href.indexOf('IsDlg') == -1)
{
iframeUrl = window.location.href.replace('#', '');
}
else
{
//url is the iframe's url (not the form's url, which is a redirect)
//iframe = _layouts/listform
//form = upload.aspx (redirected from listform)
//get parent window while inside an iframe
var parentWindow = window.parent.document;
var iframes = $('iframe', parentWindow);
for (var i = 0; i < iframes.length; i++)
{
if ((iframes[i] !== null) && (iframes[i].src !== null))
{
if (iframes[i].src.indexOf('http://') > - 1)
{
iframeUrl = iframes[i].src.replace("&IsDlg=1", "");
}
}
}
}
window.prompt('Here is the URL for this page/frame', iframeUrl);
});
}
//bindings
$(document).ready(function () {
/*
//sprite v15
//AddDevToolsButton("id_of_new_element", "button title", "vertical_axis", ""horizontal_axix, button_callback);
//user http://getspritexy.com/ to find location within formatmap16x16.png
AddDevToolsButton('customOpenInExplorer', 'Open with Explorer', '-185px', '-53px', CmdExplorerView);
AddDevToolsButton('getListCount', 'Get current list ItemCount', '-288px', '-91px', CmdItemCount);
AddDevToolsButton('recycleList', 'Recycle this list', '-271px', '-270px', RecycleList);
AddDevToolsButton('getWebTemplateID', 'Get Web Template ID', '-150px', '-35px', CmdWebTemplateID);
AddDevToolsButton('refreshThisPage', 'Refresh Page (no resubmit)', '-296px', '-55px', CmdRefreshPage);
AddDevToolsButton('getIframeLink', 'Get a link for this page', '-108px', '-72px', CmdIframeLink);
AddDevToolsButton('signInAsAnotherUser', 'Sign in as a different user', '-175px', '-1px', CmdSignInAsAnotherUser);
//AddDevToolsButton('saveSiteAsTemplate', 'Save this site as a template', '-112px', '0px', CmdSaveTemplate);
*/
//sprite v14
//AddDevToolsButton("id_of_new_element", "button title", "vertical_axis", ""horizontal_axix, button_callback);
//user http://getspritexy.com/ to find location within formatmap16x16.png
AddDevToolsButton('customOpenInExplorer', 'Open with Explorer', '-176px','-112px', CmdExplorerView);
AddDevToolsButton('getListCount', 'Get current list ItemCount', '-223px','-240px', CmdItemCount);
AddDevToolsButton('recycleList', 'Recycle this list', '-222px', '-111px', RecycleList);
AddDevToolsButton('getWebTemplateID', 'Get Web Template ID', '-80px','-48px', CmdWebTemplateID);
AddDevToolsButton('refreshThisPage', 'Refresh Page (no resubmit)', '-192px','-240px', CmdRefreshPage);
AddDevToolsButton('seeCheckOutDocs', 'See Checked Out Documents', '-224px','-160px', CmdCheckedOutDocs);
AddDevToolsButton('getIframeLink', 'Get a link for this page', '-224px','-160px', CmdIframeLink);
AddDevToolsButton('signInAsAnotherUser', 'Sign in as a different user', '-112px','-160px', CmdSignInAsAnotherUser);
//AddDevToolsButton('saveSiteAsTemplate', 'Save this site as a template', '0px','-112px', CmdSaveTemplate);
});
}
addJQuery(main);
Owner

tiagoduarte commented Sep 22, 2016

V0.7 updates:

  • moved to JQuery v2
  • improved Sign in as a different user button
Owner

tiagoduarte commented Jan 25, 2017

v0.8 updates:

  • add current list itemcount
Owner

tiagoduarte commented Feb 16, 2017

v0.9 updates:

  • add support for v14/v15 sprite
  • add recycle current list button
  • "fix" icon posx/posy order in function call
Owner

tiagoduarte commented Oct 6, 2017

v1.0 updates:

  • added "see checked out files" button
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment