Quantcast
Channel: RLV Blog
Viewing all articles
Browse latest Browse all 79

Edit SharePoint property bags with SpPropertyBag.js bookmarklet

$
0
0

SpPropertyBag.js is a drop-in JavaScript for editing SharePoint web properties (the “property bag”). Simply paste the script into the JavaScript console and run it to open a dialog where you can view, edit, add, and remove properties:

SpPropertyBag.js

Even easier is to use it as a bookmarklet in your web browser that you can click when you need access to the web properties! Simply paste this code into a new link in your browser’s toolbar:

javascript:(function()%7Bwindow.spPropertyBag%3D%7B%7D%2Cwindow.spPropertyBag.init%3Dfunction()%7B%22use%20strict%22%3Bfunction%20e(e)%7Bvar%20t%2Co%2Cn%3D%5B%5D%3Bfor(t%20in%20e)e.hasOwnProperty(t)%26%26(o%3Dtypeof%20e%5Bt%5D%2C%22string%22%3D%3D%3Do%26%26n.push(%7Bprop%3At%2Cvalue%3Ae%5Bt%5D.replace(%2F%22%2Fg%2C%22%26quot%3B%22)%7D))%3Bn.sort(function(e%2Ct)%7Breturn%20e.prop.localeCompare(t.prop)%7D)%3Bvar%20p%2Ci%3Ddocument.createElement(%22div%22)%2Cd%3D'%3Chr%3E%3Ctable%20style%3D%22margin%3A%201em%3B%22%3E'%3Bfor(p%3D0%3Bp%3Cn.length%3Bp%2B%2B)d%2B%3D%22%3Ctr%3E%22%2Cd%2B%3D'%3Ctd%20style%3D%22text-align%3A%20right%3B%20padding-top%3A%2015px%3B%22%3E%3Cb%3E'%2Bn%5Bp%5D.prop%2B%22%3C%2Fb%3E%3C%2Ftd%3E%22%2Cd%2B%3D'%3Ctd%20style%3D%22padding-top%3A%2015px%3B%22%3E%3Cinput%20id%3D%22prop'%2Bp%2B'%22%20style%3D%22width%3A240px%3B%20%22%20type%3D%22text%22%20value%3D%22'%2Bn%5Bp%5D.value%2B'%22%3E%3C%2Finpu%3E%3C%2Ftd%3E'%2Cd%2B%3D'%3Ctd%20style%3D%22padding-top%3A%2015px%3B%22%3E%3Cbutton%20onclick%3D%22window.spPropertyBag.setProperty(%5C''%2Bn%5Bp%5D.prop%2B%22'%2C'prop%22%2Bp%2B%22')%3B%20return%20false%3B%5C%22%3EUpdate%3C%2Fbutton%3E%3C%2Ftd%3E%22%2Cd%2B%3D'%3Ctd%20style%3D%22padding-top%3A%2015px%3B%22%3E%3Cbutton%20style%3D%22color%3A%20red%3B%20min-width%3A%201em%3B%22%20onclick%3D%22window.spPropertyBag.deleteProperty(%5C''%2Bn%5Bp%5D.prop%2B%22'%2C'prop%22%2Bp%2B%22')%3B%20return%20false%3B%5C%22%3EX%3C%2Fbutton%3E%3C%2Ftd%3E%22%2Cd%2B%3D%22%3C%2Ftr%3E%22%3Bd%2B%3D%22%3C%2Ftable%3E%22%2Cd%2B%3D%22%3Chr%3E%3Ch3%3EAdd%20a%20new%20property%3A%3C%2Fh3%3E%22%2Cd%2B%3D'%3Cdiv%20style%3D%22margin%3A%201em%3B%20padding-bottom%3A%202em%3B%22%3EKey%3A%20%3Cinput%20id%3D%22newKey%22%3E%3C%2Finpu%3E'%2Cd%2B%3D'%26nbsp%3B%26nbsp%3B%26nbsp%3BValue%3A%20%3Cinput%20id%3D%22newValue%22%3E%3C%2Finpu%3E'%2Cd%2B%3D'%26nbsp%3B%3Cbutton%20onclick%3D%22window.spPropertyBag.addProperty()%3B%20return%20false%3B%22%3EAdd%3C%2Fbutton%3E%3C%2Fdiv%3E'%2Cd%2B%3D%22%3Cdiv%3E%3C%2Fdiv%3E%22%2Ci.innerHTML%3Dd%2COpenPopUpPageWithDialogOptions(%7Btitle%3A%22Property%20Bag%20Editor%22%2Chtml%3Ai%2CallowMaximized%3A!0%2CshowClose%3A!0%2CautoSize%3A!0%2CdialogReturnValueCallback%3Afunction()%7Br%26%26window.location.reload()%7D%7D)%7Dvar%20t%3Dnew%20SP.ClientContext.get_current%2Co%3Dt.get_web()%2Cn%3Do.get_allProperties()%3Bt.load(o)%2Ct.load(n)%3Bvar%20r%3D!1%3Bt.executeQueryAsync(function()%7Be(n.get_fieldValues())%7D%2Cfunction(e%2Ct)%7BSP.UI.Notify.addNotification(%22Failed%20to%20get%20web%20properties...%3Cbr%3E%22%2Bt.get_message())%7D)%2Cwindow.spPropertyBag.executeChanges%3Dfunction()%7Bt.get_web().update()%2Ct.executeQueryAsync(function()%7Bconsole.log(%22Web%20properties%20successfully%20modified%22)%7D%2Cfunction()%7Bconsole.error(%22Failed%20to%20set%20web%20property!%22)%7D)%7D%2Cwindow.spPropertyBag.setProperty%3Dfunction(e%2Ct)%7Bvar%20o%3Ddocument.getElementById(t).value%3Bn.set_item(e%2Co)%2Cwindow.spPropertyBag.executeChanges()%7D%2Cwindow.spPropertyBag.deleteProperty%3Dfunction(e%2Ct)%7Bif(confirm(%22Are%20you%20sure%20you%20want%20to%20remove%20this%20property%3F%22))%7Bvar%20o%3Ddocument.getElementById(t).parentNode.parentNode%3Bo.parentNode.removeChild(o)%2Cn.set_item(e)%2Cwindow.spPropertyBag.executeChanges()%2Cr%3D!0%7D%7D%2Cwindow.spPropertyBag.addProperty%3Dfunction()%7Bvar%20e%3Ddocument.getElementById(%22newKey%22).value%2Ct%3Ddocument.getElementById(%22newValue%22).value%3Bdocument.getElementById(%22newValue%22).value%3D%22%22%2Cdocument.getElementById(%22newKey%22).value%3D%22%22%2Cn.set_item(e%2Ct)%2Cwindow.spPropertyBag.executeChanges()%7D%7D%2Cwindow.spPropertyBag.init()%7D)()

Here is the full script. It is also available for forking at GitHub.

/*
 * SpPropertyBag.js
 * by rlv-dan (https://github.com/rlv-dan)
 * License: GPL3
*/

window.spPropertyBag = {};
window.spPropertyBag.init = function() {
	"use strict";

	var ctx = new SP.ClientContext.get_current();
	var web = ctx.get_web();
	var allProperties = web.get_allProperties();
	ctx.load(web);
	ctx.load(allProperties);

	var reloadRequired = false;

	ctx.executeQueryAsync(function () {
		showPropertiesDialog(allProperties.get_fieldValues());
	}, function (req,err) {
		SP.UI.Notify.addNotification("Failed to get web properties...<br>" + err.get_message());
	});


	window.spPropertyBag.executeChanges = function() {
		ctx.get_web().update();
		ctx.executeQueryAsync(function () {
			console.log("Web properties successfully modified");
		}, function () {
			console.error("Failed to set web property!");
		});
	};
	window.spPropertyBag.setProperty = function(key, inputId) {
		var value = document.getElementById(inputId).value;
		allProperties.set_item(key, value);
		window.spPropertyBag.executeChanges();
	};
	window.spPropertyBag.deleteProperty = function(key, inputId) {
		if (confirm('Are you sure you want to remove this property?')) {
			var table = document.getElementById(inputId).parentNode.parentNode;
			table.parentNode.removeChild(table);

			allProperties.set_item(key);
			window.spPropertyBag.executeChanges();
			reloadRequired = true;
		}
	};
	window.spPropertyBag.addProperty = function() {
		var key = document.getElementById("newKey").value;
		var value = document.getElementById("newValue").value;
		document.getElementById("newValue").value = "";
		document.getElementById("newKey").value = "";
		allProperties.set_item(key, value);
		window.spPropertyBag.executeChanges();
	};

	function showPropertiesDialog(props) {
		var p, type;
		var items = [];
		for(p in props) {
			if (props.hasOwnProperty(p)) {
				type = typeof(props[p]);
				if(type === "string") {
					items.push({"prop": p, "value": props[p].replace(/"/g, '&quot;')});
				}
			}
		}
		items.sort(function(a, b) {
			return a.prop.localeCompare(b.prop);
		});


		var html = document.createElement('div');
		var h = '<hr><table style="margin: 1em;">';
		var i;
		for(i=0; i<items.length; i++) {
			h += '<tr>';
			h += '<td style="text-align: right; padding-top: 15px;"><b>' + items[i].prop + '</b></td>';
			h += '<td style="padding-top: 15px;"><input id="prop' + i + '" style="width:240px; " type="text" value="' + items[i].value + '"></inpu></td>';
			h += '<td style="padding-top: 15px;"><button onclick="window.spPropertyBag.setProperty(\'' + items[i].prop + '\',\'prop' + i +'\'); return false;">Update</button></td>';
			h += '<td style="padding-top: 15px;"><button style="color: red; min-width: 1em;" onclick="window.spPropertyBag.deleteProperty(\'' + items[i].prop + '\',\'prop' + i +'\'); return false;">X</button></td>';
			h += '</tr>';
		}
		h += '</table>';

		h += '<hr><h3>Add a new property:</h3>';
		h += '<div style="margin: 1em; padding-bottom: 2em;">Key: <input id="newKey"></inpu>';
		h += '&nbsp;&nbsp;&nbsp;Value: <input id="newValue"></inpu>';
		h += '&nbsp;<button onclick="window.spPropertyBag.addProperty(); return false;">Add</button></div>';
		h += '<div></div>';

		html.innerHTML = h;

		OpenPopUpPageWithDialogOptions({
		 title: "Property Bag Editor",
		 html:html,
		 allowMaximized: true,
		 showClose: true,
		 autoSize: true,
		 /*width: 800,*/
		 dialogReturnValueCallback: function(dialogResult) {
			if(reloadRequired){
				window.location.reload();
			}
		 }
		});
	}
};
window.spPropertyBag.init();

 


Viewing all articles
Browse latest Browse all 79

Trending Articles