2

I have the following code in which i trying to get both the list GUID (working) and the list View GUID by the name of the view, which is not working.

function getLists() {


lists = web.get_lists();
context.load(lists, "Include(Id, Title, Hidden, ItemCount, Views)");
context.executeQueryAsync(onSuccess, onFail);

}

function onSuccess() {
var ul = $("#listsList");
var listsEnum = lists.getEnumerator();
while (listsEnum.moveNext()) {
    var list = listsEnum.get_current();
    if (list.get_title() == "XXXXXXXXXXXXXX") {
        var title = list.get_title();
        var view = list.get_views().getByTitle("Excel Export");
        var elem = $("<li>" + title + " with GUID " + list.get_id().toString() + " view GUID:" + viewID + "</li>");
        ul.append(elem); // append the details to a list
    }
}


}

function onFail(sender, args) {
alert("Request failed: " + args.get_message());
}

I'm getting the following error in JS

Uncaught TypeError: Object [object Object] has no method 'get_view'

Appreciate any feedback.

I'm now using var view = list.get_views().getByTitle("Excel Export"); but this is bringing back [object Object]. Do i need to parse the object??

I have marked tyshock's response as the answer as it was his comment on enumerating through the list views that gave me my final solution.I also found the following code very useful in arriving at my working code - found here.

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<script type="text/ecmascript" language="ecmascript">

var viewCollection = null;
function runCode() {

    var clientContext = new SP.ClientContext.get_current();
    if (clientContext != undefined && clientContext != null) {
        var web = clientContext.get_web();

        var listCollection = web.get_lists();
        var list = listCollection.getByTitle("Tasks");
        this.viewCollection = list.get_views();

        var viewInfo = new SP.ViewCreationInformation();
        viewInfo.set_title('MyView');
        this.viewCollection.add(viewInfo);

        clientContext.load(this.viewCollection);
        clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
    }
}

function onQuerySucceeded() {
    var viewInfo = 'Tasks list current views: \n\n';
    var viewEnumerator = this.viewCollection.getEnumerator();
    while (viewEnumerator.moveNext()) {
        var view = viewEnumerator.get_current();
        viewInfo += view.get_title() + '\n';
    }
    alert(viewInfo);
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

</script>

 <input id="Button1" type="button" value="Run Code" onclick="runCode()" />

 </asp:Content>

S

3 Answers 3

3

"To fetch the collection of views, you need to use:

var views = list.get_views();

get_views()

If you have a reference to the list, you can get the specific view by name using:

list.get_views().getByTitle("YOUR VIEW TITLE");

There is a getView(GUID) method on the List object, and it requires a GUID as its parameter, not the list name.

7
  • Thanks but i only know the View name and not the GUID
    – Stephen
    Feb 12, 2014 at 22:20
  • You can use the enumerator to loop through the views and identify the one with the matching name. At that point, you can reference its GUID. You can also call 'getByTitle()' from the view collection to get the named view. I edited my answer to reflect that.
    – tyshock
    Feb 12, 2014 at 22:34
  • Thanks tyshock...i will give that a try...a little unsure how to enumerate through the views...
    – Stephen
    Feb 12, 2014 at 22:41
  • Updated my question...trying to use list.get_views().getByTitle("YOUR VIEW TITLE"); but get an [object Object]?
    – Stephen
    Feb 13, 2014 at 0:24
  • It sounds like that object is indeed the view. Try to access view.get_title () and it should be your view name. See the following page for the object properties: msdn.microsoft.com/en-us/library/office/jj245986.aspx
    – tyshock
    Feb 13, 2014 at 1:21
4

In your example there is an error on line (as the message says):

var viewID = list.get_view("Excel Export");

There is no method named get_view(), but SP.List.getView Method and it returns SP.View and accepts view identifier (SP.Guid) as an argument

If you would like to get view by title, the best candidate, would be, probably:

var view = SP.List.get_views().getByTitle(viewTitle);

How to get View via CSOM

SP.List.getView Method:

var view = SP.List.getView(viewId)

SP.List.defaultView property:

var view = SP.List.get_defaultView()

SP.ViewCollection.getById Method:

var view = SP.List.get_views().getById(viewId);

SP.ViewCollection.getByTitle Method:

var view = SP.List.get_views().getByTitle(viewTitle);

Example

function getListView(url,listTitle,viewTitle,OnSuccess,OnError) {

    var context = new SP.ClientContext(url);
    var list = context.get_web().get_lists().getByTitle(listTitle);
    var view = list.get_views().getByTitle(viewTitle);
    context.load(view);

    context.executeQueryAsync(function(){
       OnSuccess(view);
    },OnError);        
}

getListView('https://conntoso.sharepoint.com/',
'Phones',
'Manufacturer',
function(view){
    console.log(view.get_id().toString());
},
function(sender,args){
    console.log('Error occured:' + args.get_message());
}
);
3
  • Thanks but I doing all this in JSOM and can use the SP.List
    – Stephen
    Feb 12, 2014 at 22:20
  • SP.List is just the class name of List client object in JSOM, in your case it is: var list = ... Feb 12, 2014 at 22:41
  • Thanks for the input i tried a few things out but ended up with a [object] [object] output thin i will need to try the enumerate method tyshock mentions.
    – Stephen
    Feb 12, 2014 at 22:43
1

I ended up using this solution:

function ResolveListGuidFromName(webUrl, listName, viewName){
var guid;
var filter = "View[DisplayName='"+ viewName +"']";
$().SPServices({
    operation: "GetViewCollection",
    async: false,
    webURL: webUrl,
    listName: listName,
    completefunc:function (xData, Status) {
        guid = $(xData.responseXML).find(filter).attr("Name");
    }
});
return guid;
}

It requires the use of SPServices.

Original code had been posted here: LINK

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.