Skip to content

Instantly share code, notes, and snippets.

@OllyHodgson
Last active October 6, 2015 19:07
Show Gist options
  • Save OllyHodgson/3039254 to your computer and use it in GitHub Desktop.
Save OllyHodgson/3039254 to your computer and use it in GitHub Desktop.
Scrapes a Sharepoint 2010 Content Type page to get the column names and display names. Outputs some HTML or the FieldNames needed for a page layout file. Exceedingly limited, but does what I need it to.
/*
Run this against a content type page to get the HTML for the columns!
els = array of elements
type = "li"|"td"|"th"|"FieldRef"
*/
function create(el, type) {
var fieldname,
displayname,
outputHTML = '';
for (var i=10;i<el.length;i++) {
/* Drag the field name out of the relevant URL, then decode it */
fieldname = el[i].querySelector("a").getAttribute("href");
fieldname = fieldname.substring(fieldname.indexOf('Field=')+6,fieldname.indexOf('&Fid'));
fieldname = decodeURIComponent(fieldname);
if (Number(fieldname.charAt(0)) > 0) {
fieldname = "_x003" + fieldname.charAt(0) + "_" + fieldname.substring(1);
}
/* Drag a friendly title straight off the page */
displayname = el[i].textContent;
/* Now build the HTML */
switch (type) {
case "li":
outputHTML = outputHTML + '<li><h3>' + displayname + '</h3><p><xsl:value-of disable-output-escaping="yes" select="@' + fieldname + '" /></p></li>';
break;
case "th":
outputHTML = outputHTML + '<th class="type2" hidden="hidden">' + displayname + '</th>';
break;
case "td":
outputHTML = outputHTML + '<td class="type2" hidden="hidden"><xsl:value-of disable-output-escaping="yes" select="@' + fieldname + '" /></td>';
break;
case "FieldRef":
outputHTML = outputHTML + '<FieldRef Name="' + fieldname + '" />';
break;
default:
console.log('Type must be "li", "td", "th" or "FieldRef".');
break;
}
outputHTML = outputHTML + '\r\n';
}
return(outputHTML);
}
var els = document.querySelectorAll("#columnstable tr[class] td:first-of-type");
console.log(create(els,"li"));
console.log(create(els,"th"));
console.log(create(els,"td"));
console.log(create(els,"FieldRef"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment