Getting multiple columns in one SPGetStaticFromDisplay call

Feb 26, 2012 at 10:03 PM
Hi there, Is it possible to get multiple columns returned in a single SPGetStaticFromDisplay call? For instance, the example on your site has a single variable thisStaticName returned from a single call: var thisStaticName = $().SPServices.SPGetStaticFromDisplay ({ webURL: "", listName: "", columnDisplayName: "" }); I want to do something like: var thisStaticNames = $().SPServices.SPGetStaticFromDisplay ({ webURL: "", listName: "", columnDisplayNames: "['Title','State']" }); Thanks Joel
Coordinator
Feb 26, 2012 at 10:45 PM

Joel:

Not as the function is currently written, but that would be a good enhancement. I'll copy this to a work item for the next release.

M.

Coordinator
Feb 26, 2012 at 10:47 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Coordinator
Jun 17, 2012 at 4:41 AM

Added multi-column capability to both SPGetStaticFromDisplay and SPGetDisplayFromStatic in v0.7.2ALPHA5

As suggested, the syntax is:

$().SPServices.SPGetStaticFromDisplay ({
  webURL: "/",
  listName: "My List Name",
  columnDisplayNames: ['Title', 'State']
});

M.

Sep 10, 2012 at 12:01 PM

Hi Marc

I've been testing this new method in the v0.7.2ALPHA5 release and it's just rocking. Thanks a lot for this.

In the last week I've been using it to replace a display form which was accidentally deleted  by a user from a DispForm.aspx page and the IT department was giving the business area no love to fix it. It works on any list.

Some of the characters, date, name columns need some cleanup in the display but I thought I'd give you a heads up to show you how I'm using it anyway.

To try it out just create a List, delete the form from the DispForm.aspx page (oops!) and then add the code in a new CEWP (with the right jquery and spservices file references of course!) .

Thanks again.

Joel

    <div id="tasksUL" width="100%"></div>

    <script type="text/javascript" src="jQuery1.7.2-min.js">
    </script>
    <script type="text/javascript" src="SPServices-0.7.1a-min.js">
    </script>

    <script type="text/javascript">
    //<![CDATA[
//Nominate some columns to filter out of display

var listOfUnwantedDisplayNames = ["Property Bag", "LinkTitle", "Approval Status","Level", "owshiddenversion", "Unique Id", "Name", "URL Path", "Attachments"];

//Get the ID number from the query string
var queryStringVals = $().SPServices.SPGetQueryString();
var lastID = queryStringVals["ID"];
//alert(lastID);

//get the List Name (internal name) from the url
var thisList = $().SPServices.SPListNameFromUrl();
//alert(thisList);
//
//
////get the List Name (display name) from the url

var thisListName = location.pathname.split("/");
var listStringPos = jQuery.inArray("Lists",thisListName) + 1;
var listNameURLString = thisListName[listStringPos];
//get the site URL from the query string
var thisSite = $().SPServices.SPGetCurrentSite();
//alert(thisSite);
//
//get the current query string
var currentQueryString = encodeURIComponent(location.href);

//build up the URL for the edit form
var editFormURL = thisSite + "/Lists/" + listNameURLString + "/EditForm.aspx?ID=" + lastID + "&Source=" + currentQueryString;
                     

var editFormHTML = "<table class='ms-toolbar'><tr><td class='ms-toolbar'><a class='ms-toolbar' href=" + editFormURL + 
    "&Source=" + currentQueryString + ">Edit Item</a><\/td>&nbsp;<td><\/td><\/tr><\/table>";
                             

//]]>
    </script>

    <script type="text/javascript">
    //<![CDATA[

$(document).ready(function(){
    
    var attachmentsText = "";
    var liHtml = "";
    var camlQ = "<Query><Where><Eq><FieldRef Name='ID'/><Value Type='Text'>" + lastID + "</Value></Eq></Where></Query>";
    $().SPServices({
	operation: "GetListItems",
        webURL: thisSite,
	listName: thisList,
        CAMLQuery: camlQ,
	async: false,   


        completefunc: function (xData, Status) {
            //alert(xData.responseText);
            //array to build up the attachments html
            var attachmentTextHTML = [];
            
            $(xData.responseXML).SPFilterNode("z:row").each(function() {
                
                //check the attachments flag
                var intAttachments = parseInt( $(this).attr("ows_Attachments"));
                
               
                //if there are attachments
                
                if(intAttachments > 0){
                    $().SPServices({
                        operation: "GetAttachmentCollection",
                        async: false,
                        webURL: thisSite,
                        listName: thisList,
                        ID : lastID,
                        completefunc: function (xData, Status) {
                        
                            //alert(xData.responseText);
                            
                            var attachmentText = "";
                            attachmentTextHTML.push("<tr><td class='ms-formlabel'>Attachments<\/td><td class='ms-formbody'>");
                            $(xData.responseXML).SPFilterNode("Attachments").children().each(function(){
                                
                                var attachmentText = $(this).text();
                                var attachmentTextFileName = attachmentText.slice(attachmentText.lastIndexOf("/") + 1);
                                //alert(attachmentTextFileName);
                                attachmentTextHTML.push("<li>" + "<a href='" + encodeURI(attachmentText) +
                                    "'>" + attachmentTextFileName + "<\/a><\/li>");
            
                            });
                            attachmentTextHTML.push("<\/td><\/tr>");
                            attachmentTextHTML = attachmentTextHTML.join("");
                            //alert(attachmentTextHTML);
                        }
                    });
                }
                
                //now create an array to hold the z:row attributes
                
                var arr = [];
                var elem = this;
                var attrs = elem.attributes;
                var colNames = [];
                var attrName = "";
                
                //loop through the attributes to get the internal col names
                
                for(var i=0, len = attrs.length; i < len; i++){
                    attrName = attrs.item(i).nodeName;
                    attrName = attrName.substring(4);
                    colNames.push(attrName);
                    
                }
                //now get the display names from the internal names
                
                thisDisplayNames = $().SPServices.SPGetDisplayFromStatic({	
                    webURL: thisSite,
                    listName: thisList,
                    columnStaticNames: colNames
                });
                
                //get rid of useless columns
                
                for(var m in thisDisplayNames ){
                    var isInArray = jQuery.inArray(thisDisplayNames[m],listOfUnwantedDisplayNames);
                    if(isInArray !== -1){
                        delete thisDisplayNames[m];
                    }
                }
                
                //build up some html for the columns and their values
                arr.push("<table>");
                
                for(var j=0, leng = attrs.length; j < leng; j++){
                    attrName = attrs.item(j).nodeName;
                    attrName = attrName.substring(4);
                    if(thisDisplayNames[attrName]){
                        arr.push("<tr id='" + attrName + "'><td class='ms-formlabel'>" + thisDisplayNames[attrName] + 
                            "<\/td><td class='ms-formbody'>" + attrs.item(j).nodeValue + "<\/td/><\/tr>");
                    }
                    
                }
                arr.push(attachmentTextHTML);
                arr.push("<\/table>");
                
                //turn the html values into a string
                
                var tableHTML = arr.join("");
                
                //append to the div
                $("#tasksUL").append(tableHTML);
                
                //add the edit button
                $("#tasksUL").prepend(editFormHTML);
                
            });
        }
    });
});

//]]>
    </script>