SPServices Helper Function: Resolve the Guid of a View

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.

Advertisement
This entry was posted in Development, jQuery, SharePoint, SPServices. Bookmark the permalink.

9 Responses to SPServices Helper Function: Resolve the Guid of a View

  1. Jernej says:

    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!

  2. Jernej says:

    “I have tried with GetViewCollection”* mistake

  3. Mike Jones says:

    Exactly what i needed. Thanks!

  4. Chintan says:

    Is there a way to get specific view using SPService?

    • gavinbarron says:

      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?

  5. $().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)
    });
    });

    }

    }
    });

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.