Posted on:
Categories: SharePoint;Office 365
Description:

​​Scen​​​​ario

You have a Community Site or Discussion Board Add-in. Out-of-the-box, it uses the AllItems.aspx page layout, named the Subject view.

The discussion board orders its content by most recent. At one point, you want to create your own custom view – something to cater to your requirements, sort by date or group, for example. 

You remember somewhere that the Management view is also available for out-of-the-box discussion boards. So you look to the URL and replace the AllItems.aspx with Management.aspx. Ah, satisfaction. All the familiar controls and options have now resurfaced.

Proble​​m

You go through the motions with ellipses beside the list search box and create a new view based off of Management.aspx because, frankly, that's the only choice. You could design it from scratch in Designer but that's too much work. So now you have a clone of Management.aspx. That's great! You have plenty of options, except when you click on a topic, it takes you to a list view with the list title being the discussion title. What gives?!

Simple. Management view is for just that - managing. The fact is, each discussion thread is really just a folder. Of course, what happens when you double click a folder? You access its inner contents. And that's exactly what SharePoint is doing.

You want to access the thread via your new management view clone? Tough luck, nope, can't do it, impossible… is what SharePoint (Online) often tells us developers. But if you listen between the lines, there is a way. It requires some coding… that is if you don't want to redirect it manually every single time.

Solution

First, let's talk about how to do it manually and we'll talk automation. What you want to do is go to the default Subject view. Click on any thread and copy down the URL into notepad.

Next, go to the new custom view. Just like before, click any thread. Copy that sucker down too.

Now, let's do a little Frankenstein-ing. Take the first part of the URL from the subject view up until the query string marker, the "?", and paste it somewhere.

 Next, find the RootFolder and FolderCTID parameter pairs in the custom view URL and stitch them onto the first part.

The result is the URL to the discussion post.

 

So, redirecting a page to a post is easy once we know what we're dealing with. Simply write up Javascript to get the first string, which is really just the current URL, then parse out the two parameters, join them, and relocate the window object to the new URL. Once completed, we can just put that script onto any custom view page layout. Since posts only differ in the query string and use the same page layout, putting the script in one place will make it propagate to all past, present, and future posts within that view.

Here's the code snippet:

function redirect() { 
 var rootFolder = getQueryStringParameter('RootFolder');
 var folderCTID = getQueryStringParameter('FolderCTID'); 

 if(rootFolder != "" && folderCTID != ""){

  var temp = location.pathname.split('/');
  temp.pop();
  var page = [temp.join('/'),"/Flat.aspx"].join('');  
  var url = [location.protocol, '//', location.host, page].join('');    

  var redirectUrl = [
   url,
   "?RootFolder=",
   rootFolder,
   "&FolderCTID=",
   folderCTID
  ].join('');  

  window.location.replace(redirectUrl)
 }
}

function getQueryStringParameter(param) {
 if(document.URL.split("?")[1] != null){
  var params = document.URL.split("?")[1].split("&");
  for (var i = 0; i < params.length; i = i + 1) {
   var singleParam = params[i].split("=");
   if (singleParam[0] === param) {
    return singleParam[1];
   }
  }
 }
 return ("");  
}

window.onload = function () {
 redirect();
};

Et voilà! You now have a plethora of options from the management view, while maintaining the expected behavior from the default Subject view.