automatically switch views when opening a sharepoint folder RRS feed

  • Question

  • Hi there,

    I have spent several days making this auto view switch for a sharepoint folder but still cannot get it work...After search on the web, I found two suggested ways:

    1. http://www.endusersharepoint.com/2009/01/14/automatically-switching-views-when-opening-a-sharepoint-folder/

    basically it works by inserting a content editor web part into the page, and add the following javascript to switch views:

    <script type="text/javascript">
    var splitStr = document.location.pathname.split("/");
    var newStr="";


    var tbls = document.getElementsByTagName("table");

        var linkHref = document.getElementById(tbls[i].id).getElementsByTagName("a");
        var linkText = document.getElementById(tbls[i].id).innerText;

        for (j=0;j<linkHref.length;j++)
          linkHref[j].href = document.location.pathname.replace("AllItems.aspx","")+tbls[i].CType+".aspx?RootFolder="+newStr+linkText;

    I followed what the article says but didn't get the view switched when I open the folder of a custom content type.

    2. I also tried what says in http://www.mikhaildikov.com/2008/05/sharepoint-folders-need-more-love.html

    Each custom folder content type will have a view that displays metadata specific to the type of document contained in the folder. These folder views are marked as default but are assigned to the specific folder content type.

    this does not work on my PC either.

    Are those methods browser dependent or environment dependent? I appreciate if anybody can provide some help on this!! Thanks!

    Wednesday, April 27, 2011 11:40 PM

All replies

  • Where did you place the CEWP? Did you place it at the bottom of the page like instructed.
    Planet Technologies || SharePoint Task Force
    Thursday, April 28, 2011 2:15 AM
  • Hi Roland,

    Thank you very much for your reply. I finally got it work:

    in stead of

    linkHref[j].href = document.location.pathname.replace("AllItems.aspx","")+tbls[i].CType+".aspx?RootFolder="+newStr+linkText;

    on my PC, the dynamically created link should be

    CId = tbls[i].CId;

    linkHref[j].href = document.location.pathname.replace("AllItems.aspx","")+tbls[i].CType+".aspx?RootFolder="+newStr+"subdir%2fsubsubdir&FolderCTID="+CId;


    Now my problem is that "AllItems.aspx" seems to be global to all the sub folders of the document library "topFolder". for example, from "document.location.pathname " I always got "/sites/abc/topFolder/Forms/AllItems.aspx". However, I'm trying to apply this auto view switch on some subfolders, and that's why I have to add ""subdir%2fsubsubdir" to the newly created link. For example, my folder structure is like this: Folder1 is the sub folder of topFolder, Folder11 is the sub folder of Folder1. However, right now I hard-coded "subdir%2fsubsubdir" (eg. "Folder1%2fFolder11"). I'm wondering how I can programmably got this sub folder's path so that I don't need to hard code the path, which will have a global effect. Thanks!

    Thursday, April 28, 2011 5:47 PM
  • Hi,

    Looks like the above script doesn't works in SP 2010. Please help...


    Wednesday, March 28, 2012 11:56 AM