List View name

Apr 2, 2015 at 1:59 PM
Hello folks.

I'm simply trying to find a method to display a list's view name either in a CEWP or in the List title.

I've researched a lot and there are a lot of snippets out there that, frankly, im unable to fully put into practise. My method is to find a javascript, paste into my CEWP, set an alert for some var then if it works great, if not, I continue on. I thought window.location... would work but couldnt find anything there.

Yes, it is a very newbie way of doing things but I'm not a coder but it's worked for me so far.

Anyway, I recall SPServices is quite powerful and relatively simple to employ and was wondering if there was a utility in here that would return a simple view name.

Holy smokes, why does Microsoft have to make everything so incomplete, convoluted and over-engineered?

Thanks
Coordinator
Apr 2, 2015 at 2:23 PM
Do you mean the view name that is at the top of the page? Or a list of the available views?In other words, can you be more specific?

There's probably a call you can make to the Lists Web Service to get what you need. Look at this page of the documentation:
https://spservices.codeplex.com/wikipage?title=Lists

M.
Apr 2, 2015 at 4:25 PM
Hi Marc. Wow, thats a real quick response, thanks!

I'm refering to the name of the view at the top right of the list view (WSS, don't laugh, I only work here).

Some of our view names are longer then the dropdown shows and can be quite non-intuitive to know which view you're on on a quick glance. That's why I'd like to add it to the title of the list suffixed with the name of the view.

Yes, I can manually add it very simply, I just want something dynamic that is reusable.

So https://spservices.codeplex.com/wikipage?title=Lists you say. I will look into it.

Thanks again.

CW
Apr 2, 2015 at 5:04 PM
Yes, I was looking at that exact thing this morning.

It's a bit too much work to use that method as it appears I'd have to create a feature? We're not permitted to use that technique to enhance the system. Thet've even turned off workflow for us, so I'm pretty much confined to a simple script in a CEWP. I just thought there was a simple one liner one could use.

Thanks though.

CW
Coordinator
Apr 2, 2015 at 5:33 PM
You don't need to deploy a feature to use SPServices. I think you can get away with just a little jQuery, though.

Try this:
var viewName = $("a[id^='ViewSelectorMenu']").text();
M.
Apr 2, 2015 at 6:37 PM
Will you stop being Mr. Fantastic! :)

Thanks for that. I included it with your original "Hello" script, however, it seems to render the script non-operational. If I comment it out, all the other alerts work fine. I tested it simply using alert(viewName);
<script type="text/javascript" src="http://xxxx/org/1275526/priv/jQuery/jquery.min.js"></script>
<script type="text/javascript" src="http://xxxx/org/1275526/priv/jQuery/jquery.SPServices.min.js"></script>
<script type="text/javascript">

alert ("js on.");
$(document).ready(function() {
alert ('jQuery on.');
var thisSite = $().SPServices.SPGetCurrentSite();
alert ('Hello World! Your site is : ' + thisSite );

var thisUserAccount = $().SPServices.SPGetCurrentUser();
alert ('And you are: ' + thisUserAccount);

var thisUserID = $().SPServices.SPGetCurrentUser({
    fieldName: "Title",
    debug: true
var viewName = $("a[id^='ViewSelectorMenu']").text();

});
alert (thisUserID);
alert (viewName);
});

</script>
Apr 2, 2015 at 7:03 PM
Well I've been fooling around with the syntax and so far got the whole script to output the alerts now, only the viewName is giving a blank alert.

I changed:
var viewName = $("a[id^='ViewSelectorMenu']").text();
to:
var viewName = $("a[id^='ViewSelectorMenu-title']").text();
Coordinator
Apr 2, 2015 at 10:51 PM
Sorry; typo on my end:
var viewName = $("a[id$='ViewSelectorMenu']").text();
This says: find an anchor tag (a) with an id that ends with 'ViewSelectorMenu'. (The "hat" (^) means 'starts with'.) You can see these selectors here:
http://api.jquery.com/category/selectors/

M.
Apr 2, 2015 at 11:52 PM
Edited Apr 3, 2015 at 5:37 PM
Thanks Marc. I was actually going to ask where to resesrch all this. There's so much out there, one sometimes is unsure where to start.