Skip to content

Instantly share code, notes, and snippets.

Embed
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.3
// @grant none
// @match http://*.yourdomain-to-match-here/*
//// ICON POSITION: open /_layouts/1033/images/formatmap16x16.png (e.g. paint.net), find top left of desired icon (always negative x and y)
//// GIST URL: https://gist.github.com/tiagoduarte/11992165b8cf08062316cc794ada1e0f
//// 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==
//v1.1 - added open site permissions icon
//v1.3 - new folder icon
// 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);
});
}
function CmdViewCount()
{
$('#getViewCount').click(function ()
{
//alert("ViewCount: " + $(".ms-listviewtable tbody .ms-itmhover .s4-itm-cbx").length);
if(ctx.TotalListItems == null)
alert("View Count is null: Try mouseover on the view.");
else
alert("View Count (this page only): " + ctx.TotalListItems);
});
}
//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('List Item Count: ' + 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 CmdOpenSitePermissions()
{
$('#openSitePermissions').click(function () {
window.location.href = _spPageContextInfo.webServerRelativeUrl + "/_layouts/15/user.aspx";
});
}
function CmdCreateFolder()
{
$('#createFolder').click(function () {
///Forms/Upload.aspx?RootFolder=%2Fsites%2Frmc%2Fglobaldoctest&Type=1
window.location.href = ctx.listUrlDir + "/Forms/Upload.aspx?RootFolder=" + encodeURIComponent(ctx.listUrlDir) + "&Type=1";
});
}
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('getViewCount', 'Get current view ItemCount', '-208px','-113px', CmdViewCount);
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', '-0px','-224px', CmdCheckedOutDocs);
AddDevToolsButton('getIframeLink', 'Get a link for this page', '-224px','-160px', CmdIframeLink);
AddDevToolsButton('signInAsAnotherUser', 'Sign in as a different user', '-112px','-160px', CmdSignInAsAnotherUser);
AddDevToolsButton('createFolder', 'Create Folder', '-248px','-16px', CmdCreateFolder);
AddDevToolsButton('openSitePermissions', 'Open site permissions', '-200px','-16px', CmdOpenSitePermissions);
//AddDevToolsButton('saveSiteAsTemplate', 'Save this site as a template', '0px','-112px', CmdSaveTemplate);
});
}
addJQuery(main);
@tiagoduarte

This comment has been minimized.

Copy link
Owner Author

tiagoduarte commented Sep 22, 2016

V0.7 updates:

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

This comment has been minimized.

Copy link
Owner Author

tiagoduarte commented Jan 25, 2017

v0.8 updates:

  • add current list itemcount
@tiagoduarte

This comment has been minimized.

Copy link
Owner Author

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
@tiagoduarte

This comment has been minimized.

Copy link
Owner Author

tiagoduarte commented Oct 6, 2017

v1.0 updates:

  • added "see checked out files" button
@tiagoduarte

This comment has been minimized.

Copy link
Owner Author

tiagoduarte commented Sep 12, 2018

v1.1 updates:

  • added "open site permissions" button
@tiagoduarte

This comment has been minimized.

Copy link
Owner Author

tiagoduarte commented Jan 10, 2019

v1.3 updates:

  • add new folder icon

  • icon updates (find alternatives for duplicate icons)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.