Spservices views and columns

Aug 15, 2011 at 2:18 PM

<select id="Library"></select>
<select id="Views"></select>

<script language="javascript" type="text/javascript">
$(document).ready(function () {
$().SPServices({
operation: "GetListCollection",
completefunc: function (xData, status) {
$(xData.responseXML).find('List').each(function() {
var listTitle = $(this).attr('Title');
$('#Library').append('<option value=' + listTitle + '>' + listTitle + '</option>');
});
}
});

$('#Library').change(function () {

$().SPServices({
operation: "GetViewCollection",
async: false,
listName: $('#Libary').val(),
completefunc: function (xData, Status) {
$(xData.responseXML).find("[nodeName='z:row']").each(function () {
var viewTitle = $(this).attr("Title");
$("#Views").append('<option value=' + viewTitle + '>' + viewTitle + '</option>');
});
}
});

$().SPServices({
operation: "GetListItems",
async: false,
listName: $('#Library').val(),
CAMLViewFields: "<ViewFields><FieldRef Name='Title' /></ViewFields>",
completefunc: function (xData, Status) {
$(xData.responseXML).find("[nodeName='z:row']").each(function () {
var liHtml = "<li>" + $(this).attr("ows_Title") + "</li>";
$("#tasksUL").append(liHtml);
});
}
});


});
});
</script>
<ul id="tasksUL"/>

 

trying to display the views and columns of the selected library.  First of all, the views aren't showing, but the list items are.  Also, is there a way to get the viewfields dynamically, there will be another dropdown for those?

 

What do you think?

Thanks.

Coordinator
Aug 15, 2011 at 10:12 PM

zaryk:

GetViewCollection doesn't return z:row objects; that's a peculiarity of GetListItems. You should check the SDK for the expected return formats.

You can use GetList to get the columns in any list.

M.

Aug 16, 2011 at 3:40 PM

$().SPServices({
                operation: "GetViewCollection",
                async: false,
                listName: $(this).val(),
                completefunc: function (xData, Status) {
                    $(xData.responseXML).find("View").each(function () {
                        var viewTitle = $(this).attr("DisplayName");
                        if (!$.Enumerable.From(vwExclude).Any(function (x) { return viewTitle.indexOf(x) != -1 }))
                            $("#Views").append('<option value=' + viewTitle + '>' + viewTitle + '</option>');
                    });
                }
            });

 

It now shows the views.  Well, Actually, I need GetListAndView, but ViewFields isn't working

$().SPServices({
                operation: "GetListAndView",
                async: false,
                listName: $(this).val(),
                viewName: $("#Views").val(),
                completefunc: function (xData, Status) {
                    $(xData.responseXML).find("ViewFields").each(function () {
                        var columnTitle = $(this).attr("Name");
                        $("#Columns").append('<option value=' + columnTitle + '>' + columnTitle + '</option>');
                    });
                }
            });

 

 

Also, I'm guessing no way to tell GetListCollection to get just Document Library?

Coordinator
Aug 16, 2011 at 3:44 PM

I think to accomplish what you seem to be aiming at, you'll need to combine several operations. Keep in mind that you can call a second operation inside the .each() for the first. For instance, you can call GetList for each of the lists in the .each() for the GetListCollection operation.

M.

Nov 6, 2011 at 5:26 PM

I know this is an old post but I just wanted to reply to zaryk's question about just getting Document Libraries for anyone else looking into this.  I'm just trying this out now but it looks like the ServerTemplate attribute will be set to "101" if the list is actually a Document Library.  "100" for an actual list, "109" for a Picture Library, "108" for Discussion Board, "102" for a survey.