If you’re using the excellent SPServices library to wrap up access to the SharePoint web services from JavaScript, good stuff!
If, like me, you want to query a View on a given list you’ll need to pass the Guid of the view as the viewName parameter. Using the display name will give you the error: Parameter viewName is missing or invalid. 0x82000001
To resolve this you’ll need to call the GetViewCollection method for a given list and then parse the result to find your Guid. I’m a total newbie to jQuery and client side scripting so I found this a little tricky to get right, but I got there. So, here’s a helper for you use:
function ResolveListGuidFromName(webUrl, listName, viewName){ var guid; var filter = "View[DisplayName='"+ viewName +"']"; $().SPServices({ operation: "GetViewCollection", async: false, webURL: webUrl, listName: "Votes", completefunc:function (xData, Status) { guid = $(xData.responseXML).find(filter).attr("Name"); } }); return guid; }
Not too bad for my first hack at this stuff.
It doesn’t work
can you help me?
$(“document”).ready(function() {
function GetViewId(webUrl,listName,viewName) {
var id = “”;
var filter = “View[DisplayName=”+ viewName +”]”;
$().SPServices({
operation: “GetView”,
listName: listName,
viewName: viewName,
webUrl: webUrl,
async: false,
completefunc: function (xData, Status) {
id = $(xData.responseXML).find(“filter”).attr(“Name”);
}
});
return id;
}
var viewid = GetViewId(“/senat/1konstitutivnaseja/Lists/Vsebina dnevnega reda/a.aspx”,”Vsebina dnevnega reda”,”a”);
if(viewid!=null)
{
$(“#viewid”).append(viewid);
}
else
{
$(“#viewid”).append(“list not found”);
}
});
View guid:
that is my code, but my result is allways list not found!
At fist glance the problem appear to be that you are using the GetView call, if you look at my mechanism I use the GetVewCollection call and then find a match in the returned result.
From memory I went for GetViewCollection as the call to GetView expects the Guid supplied as the viewName parameter.
Yup, looking at the MSDN docs, http://msdn.microsoft.com/en-us/library/views.views.getview(v=office.12).aspx viewName is infact expecting the Guid.
i’ll try with GetVewCollection but is still the same story :(.,what about webUrl?is there something wrong?
Thnak you very much,
i appreciated!
yeah, try using the full Url, http://mywebapp/relativesiteUrl and drop the end bit that you have which is the path to the view page of your list.
“I have tried with GetViewCollection”* mistake
Exactly what i needed. Thanks!
Is there a way to get specific view using SPService?
I’m not exactly sure what you’re asking here, are you looking to load the data from a given list view or load the given view object? What are you using to identify the view, its GUID or it’s name?
$().SPServices({
operation: “GetViewCollection”,
async: false,
webURL: webUrl,
listName: listName,
completefunc:function (xData, Status) {
if(Status == ‘success’){
$(xData.responseXML).find(‘View’).each(function() {
$.each(this.attributes, function(i, attrib){
var name = attrib.name;
var value = attrib.value;
// do your magic 🙂
console.log(attrib.name +” = “+attrib.value)
});
});
}
}
});