0

In javascript, i need to know the current view. I found this post but in 2010 it doesn't work: How to get the current custom view name of my SharePoint List When i do context.view, it's undefined.

Here is the complete code in the navigate to url of my custom ribbon action on a list

    javascript:   
//it doesn't work
document.writeln('<script src="http://w50018003l/Style Library/js/jquery211.js" type="text/javascript"></script>'); 
var viewName = $(".s4-titletext h2 span span a span:first").html();
alert(viewName); 
var context = SP.ClientContext.get_current();
var web = context.get_web();
var lists = web.get_lists();
var listId = SP.ListOperation.Selection.getSelectedList(); 
var list = lists.getById(listId);
var viewcollection = list.get_views();
var view = list.get_views().getByTitle("All Items");
var vf = [];
var viewFields = view.get_viewFields();
context.load(viewFields);
context.load(viewFields);
context.executeQueryAsync(OnSuccess,OnFailure);
function OnSuccess(){
    var e = viewFields.getEnumerator(); 
    while (e.moveNext()) {
        var fieldName = e.get_current();
        vf.push(fieldName);
        var listField = vf.join();
    }
    var selectedItems = SP.ListOperation.Selection.getSelectedItems(); 
    var items = [];
    for (var i in selectedItems) {
        var id = selectedItems[i].id;
        alert(id);
        var item = list.getItemById(id);
        items.push(item);
        for(var x=0;x<vf.length;x++){
            context.load(item, vf[x]);
        }
    }
    context.executeQueryAsync(OnSuccessLoadItem,OnFailureLoadItem);
    function OnSuccessLoadItem(){
        var fileExport = "data:application/csv;charset=utf-8,";
        var csv = "";
        for(var x=0;x<vf.length;x++){
            csv+=vf[x]+";";
        }
        csv+="\n";
        for(var cpt=0;cpt<items.length;cpt++){
            for(var x=0;x<vf.length;x++){
                var value = items[cpt].get_item(vf[x]);
                if(value == null){
                    csv+=";";
                }
                else
                {
                    var conditional = typeof value.get_lookupId === 'function';
                    if(conditional){
                    var item_requester_name = value.get_lookupValue(); 
                    var item_requester_id = value.get_lookupId();
                    csv+=item_requester_name+";";
                    }
                    else{
                        csv+=items[cpt].get_item(vf[x])+";";
                    }
                }
            }
            csv+="\n";
        }
        window.open(fileExport+ encodeURIComponent(csv));
    }
    function OnFailureLoadItem(){
    alert("failure loadItem");
    }
}
function OnFailure(){
    alert("failure viewfields");
}
4

Ribbon should already contain this information. You can get the view name like this:

SP.Ribbon.PageManager.get_instance().getPageComponentById('WebPartWPQ2')._data.ViewName

_data property contains all kinds of fun stuff.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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