title: How To Change SharePoint Document Library View On The Fly Using Code
body: <p>For example, I have a document library that hold contracts for multiple projects.</p>
  
  <p>So:</p>
  
  <blockquote>
    <p>DocLib
    Contract_For_Project_A.pdf (metada: code: PRJA)
    Contract_For_Project_B.pdf (metada: code: PRJB)
    etc.</p>
  </blockquote>
  
  <p>In requirement term, maybe something like "I want to store project specific document like contracts in any document library belonging to a division or department, etc.  I should not be restricted to store project specific document in only the project site since it might make more sense to associate them to its related department / division, etc."</p>
  
  <p>And there is a user requirement that "as a user, I want to be able to only view project specific document based on the project code metadata that I will choose when I arrive in the portal and all documents that are shown to me should be filtered by the chosen project code metadata (in any document library that contain project specific documents)</p>
  
  <p>The question is how can we do this in SharePoint?</p>
  
  <p>I'm thinking of storing the chosen project metadata in a Session variable from a webpart and then pick this up once I get to the document library, perhaps add another web part in there that will pick up the project metadata from session and redirect the page to the right view that is already setup to filter documents based on the project metadata...</p>
  
  <blockquote>
    <p>DocLib
    Views
    ViewForProjectA
    ViewForProjectB
    etc.</p>
  </blockquote>
  
  <p>Is this a good approach or is there a better approach?</p>
  
  <p>In this scenario it will basically load the default view w/ the redirector webpart first, so there will be a redirect which might not be desirable.  Is there a way to do this in a straightforward way without having to do client redirect?</p>