V0.6.0 Alpha Test [View Operations]

Jan 31, 2011 at 9:34 PM

Although not an official "tester", I thought I would post my results of using some of the new View functions in SPServices.

I am working on a project where I have a need to allow the users to use personal or public views of a list. I am creating a printing function and attached a Print View button to the actions dropdown of the listview.

here is the first bit of code that I am using for this:

 

var viewguid, viewname, camlQuery;
    viewname = $("td[id*='onetViewSelector']").text();
    var xmldata1 = new ActiveXObject("MSXML.DomDocument");
    xmldata1.async = false;
    var xmldata2 = new ActiveXObject("MSXML.DomDocument");
    xmldata2.async = false;
    var xmldata3 = new ActiveXObject("MSXML.DomDocument");
    xmldata3.async = false;
    var xsldata1 = new ActiveXObject("MSXML.DomDocument");
    xsldata1.async = false;
    xsldata1.load(L_Menu_BaseUrl + "/PrintTemplates/CTSPrintList.xsl")
    // Get the view guid by polling the views by the display name. This will get the actual query text so personal views will still work
    $().SPServices({
        operation: "GetViewCollection",
        async: false,
        webURL: L_Menu_BaseUrl,
        listName: "Commitments",
        async: false,
        completefunc: function(xData, Status) {
            $(xData.responseXML).find("[nodeName=View]").each(function() {
                if ($(this).attr("DisplayName") == viewname) {
                    viewguid = $(this).attr("Name");
                }
            });
        }
    });
    // Have the viewguid so get the actual query
    $().SPServices({
        operation: "GetViewHtml",
        webURL: L_Menu_BaseUrl,
        listName: "Commitments",
        viewName: viewguid,
        async: false,
        debug: true,
        completefunc: function(xData, Status) {
            camlQuery = trimtotag(xData.responseXML.xml, "Where");                
        }
    });
    camlQuery = "<Query><Where>" + camlQuery + "</Where></Query>";

The key notes of this code are as follows:

viewname = $("td[id*='onetViewSelector']").text();  == This line simply gets the display name from the view dropdown of the listview.

The first SPServices call loops through all the views based on the user so it gets their personal views as well. It compares the display name to the one in the viewname variable and when it finds a match

it will assign the "Name" attribute to the viewguid variable which is of course the actual guid for the list.

The next SPServices call will get the HTML of this view which includes the actual CAML query! So using a function that I call trimtotag (below) I can get the true CAML query and then use it how I want!

function trimtotag(text, tagName) {
    var sTag = "<" + tagName + ">";
    var eTag = "</" + tagName + ">";
    var sTagIndex = text.indexOf(sTag);
    var eTagIndex = text.indexOf(eTag, sTagIndex + sTag.length);

    if ((sTagIndex >= 0) && (eTagIndex > sTagIndex))
        return text.substring(sTagIndex + sTag.length, eTagIndex);

    return null;
}

This function as I used above returns the contents of the Where clause so I just added that back via "<Query><Where> + camlQuery +</Where></Query>"

So I can always have the same ViewFields regardless of the query!