I received this code from one of my readers – Ed MacIntosh with this message:
Alex,
This is a great web part that someone at my company shared that I think your readers would really find useful.
It provides a dropdown menu to choose a list in your site and upon selection displays the field name as next to the filed internal name for all fields in the list.
You drop it in a CEWP and it will let you pick any list in your site collection to list all columns by “DisplayName”, “FieldInternalName” and “FieldType” like this:
The only thing I have changed is the style of the table to “width:auto”, so all credit goes to Ed and his colleague.
<select id="spLists" onchange="displayFieldData(this.options[this.selectedIndex].value)"/> <div id="spListsSelected"></div> <table id="spListFieldTable" cellpadding="0" cellspacing="0" style="width: auto"></table> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script> <script type="text/javascript"> var siteURL = 'http://' + window.location.host + L_Menu_BaseUrl + '/_vti_bin/lists.asmx'; $(document).ready(function(){ var soapEnv = "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> <soap:Body> <GetListCollection xmlns='http://schemas.microsoft.com/sharepoint/soap/' /> </soap:Body> </soap:Envelope>"; $.ajax({ url: siteURL,beforeSend: function(xhr) { xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetListCollection"); }, type: "POST", async: false, dataType: "xml", data: soapEnv, complete: processResult, contentType: "text/xml; charset="utf-8"" }); }); function processResult(xData, status) { $("#spLists").append("<option>Select a List</option>"); $(xData.responseXML).find("List").each(function() { var liHtml = "<option value='" + $(this).attr("Title") + "'>" + $(this).attr("Title") + "</option>"; $("#spLists").append(liHtml); }); } function displayValue(splistname) { $("#spListsSelected").text(splistname); } </script> <script type="text/javascript"> var arrSkipFieldTypesOf = ['Computed']; var arrIncludeOverrideFields = ['Title','Author','Created','Modified','Editor']; function displayFieldData(listname) { if(listname == "" || listname == undefined) return false; var soapEnv = "<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'> <soap:Body> <GetList xmlns='http://schemas.microsoft.com/sharepoint/soap/'> <listName>" + listname + "</listName> </GetList> </soap:Body> </soap:Envelope>"; $.ajax({ url: siteURL, beforeSend: function(xhr) { xhr.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetList"); }, type: "POST", async: false, dataType: "xml", data: soapEnv, complete: processResult2, contentType: "text/xml; charset="utf-8"" }); } function processResult2(xData, status) { $("#spListFieldTable tr").remove(); $("#spListFieldTable").append("<tr style='font-weight:bold;'><td style='padding-right:10px'>DisplayName</td><td style='padding-right:10px'>FieldInternalName</td><td style='padding-right:10px'>FieldType</td></tr>"); $(xData.responseXML).find("Field").each(function() { if (($.inArray($(this).attr('Name'),arrIncludeOverrideFields)>-1) || ($(this).attr('FromBaseType')!='TRUE' && $(this).attr('Sealed')!='TRUE' && $(this).attr('DisplayName')!=undefined && $.inArray($(this).attr('Type'),arrSkipFieldTypesOf)==-1)) { var trHtml = "<tr>"; trHtml += "<td style='padding-right:10px'>" + $(this).attr("DisplayName") + "</td>"; trHtml += "<td style='padding-right:10px'>" + $(this).attr("Name") + "</td>"; trHtml += "<td style='padding-right:10px'>" + $(this).attr("Type") + "</td>"; trHtml += "</tr>"; $("#spListFieldTable").append(trHtml); } }); $("#spListFieldTable tr:odd").css("background-color","rgb(206,206,206)"); } </script>
Alexander
Nice – Thanks!
Copy/paste, worked as advertised – 5 stars!
Thanks to George Franklin for sharing this with us at our company. Hope you don’t mind me passing it on.
Note – if you change the siteVar declaration (line 6) to remove the “http:” (so it begins “var siteURL = ‘//’ + …”), it’ll be a more robust script (i.e. it won’t fail on https: sites).
Great script Ed, and thank you Alex for all of your clever innovation!
PS I’m running this with jQuery 1.7 (without any problems)