1,804 articles and 14,388 comments as of Tuesday, December 28th, 2010

Tuesday, September 15, 2009

SharePoint: How can I save a custom view and reuse it in another document

I saw the question come up over on Mark Miller’s site about how to save a custom View for use in another document library, so I decided to play around with it some to see what I could come up with for a solution to this.

Basically, what I’ve found is that there appears to be two ways to approach this:

First, you can do the “Save library as a template” option which will copy over any custom views you have…which sounds simple enough for creating any new libraries, but what about if you have an existing library?  How do we get the view transferred over?

To do this we’ll have to use the second option of using SharePoint Designer to add in a new “view page” to an existing site’s document library.

For this example, I’ll be using two sites.  The first site (where I’ll be creating the “master” view) is called “Test site 1″ (URL is “http://portal/ts1“) which has the default document library “Shared Documents”.  The second site will be called “Test site 2″ (URL is “http://portal/ts2) and also has a default document library called “Shared Documents”.

The view I’ll be creating is called “MyView” (and “MyNewView”) and will display the following columns:

  • “Name (linked to document with edit menu)”
  • “Created”
  • “Version”

This is a simple test, but will illustrate how to apply the custom view to another library.

First, in the “master” document library (”Shared Documents” in the “Test site 1″ site), create the view using the columns listed above and name it “Test”.  Once you have the view created, open the site (”Test site 1″) in SPD (SharePoint Designer), and navigate to the “Shared Documents > Forms” folder, and open the “Test.aspx” file (the view we just created).

Next, open another instance of SPD and open “Test site 2″.  Once it’s open, navigate to the “Shared Documents > Forms” folder.  Once there, “right-click” on the “Forms” folder and choose “New > ASPX” to create a new “view” file (name it “MyView” for this test), then “double-click” the “MyView.aspx” file to open it.

Now comes the fun part!!

Switch back to the “Test.aspx” file and copy its entire html (in the html view, “right-click” and select all – “ctrl+a” – whichever method you like), then switch back to the “MyView.aspx” file and replace its entire html with what you just copied (basically, replace the entire html for the “MyView.aspx” file with the html from the “Test.aspx” file).

Next, we need to change the “guid” that is being referenced in the “old” html (ID pointer to the document library) to that of the new document library.  The easiest way to do this is to open the site in a browser, go to the document library, then go to “Settings > Document Library Settings”.  In the address bar, the URL will have the guid as the last part of the address.

Example:
http://Portal/Test1/_layouts/listedit.aspx?List=%7B35A23714%2D25E3%2D42A5%2DB4BA%2D868F31906FA2%7D

Guid would be the last part of the URL:
%7B35A23714%2D25E3%2D42A5%2DB4BA%2D868F31906FA2%7D

Modify the guid to be in the following format:
{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}

This means replacing the html equivalents to the real characters as such:
{35A23714-25E3-42A5-B4BA-868F31906FA2}

  • %7B becomes {
  • %2D becomes -,
  • %7D becomes }

Do this for both document libraries then switch back to the “MyView.aspx” file and perform a simple “find-and-replace” on the old guid (replace the guid associated with the document library from the “Test site 1″ library to the guid for the document library in the “Test site 2″ site).  There should only be one replace (approximately on line #53).  Also, (two lines down from the guid – should be approx. line#55) you need to update the “Url” attribute to be the correct site (”ts2″ instead of “ts1″ – you can also use the “find-and-replace” method to change this), and on the same line change the “Display Name” attribute to be what you want displayed in the dropdown for the name of the view (optional – you can leave as is if you want).

Once you’ve made the changes, save the file.  You’ll see a popup informing you that “The URL `Shared Documents/Forms/MyView.aspx’ is invalid.  It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web”.  I believe this is because there may still be a reference to the old library (or site) somewhere in the page (which I wasn’t able to find), but the easy workaround is to just save the page with a different name (for this I chose “MyNewView”).  Once it is saved as the new name, you can delete the “MyView.aspx” page since we won’t be using it.

Once you have the above completed, switch back to the “Test site 2″ site and go to the document library.  Refresh the page and look at the available views (you’ll notice a new listing with the name you gave it during the “find-and-replace” part, which is now available).

From here you can simply apply the view and use it as normal.

So far, I’ve ran through this about 6 or so times and haven’t found any immediate problems, but with anything custom you do to modify the site (especially through SPD) there may be something that does come up, so if you run into any problems, post a comment here and we’ll look and see if we can find out what’s happening.

- Dink

Dessie LunsfordDessie Lunsford
Points-of-Sharing

 

Please Join the Discussion

3 Responses to “SharePoint: How can I save a custom view and reuse it in another document”
  1. PAC says:

    You can find a bookmarklet exactly for this need on Jonathan Roussel’s blog (http://blog.jonathanroussel.com/2009/09/retrieve-list-and-view-guids-with.html).

    It’s huge help to retrieve list and view GUID and it’s already in the right format so no need to convert html chars anymore !!!

  2. Caleb says:

    A little easier way to get the GUID for a library is to go into Document Library Settings and then click on Audience Targeting Settings. For some reason, the URL for this page is not encoded and therefore easy to copy and paste without modifying characters. This works for lists as well.

  3. Jonathan says:

    Thanks Dessie for the article

    PAC, thank you for noting that my bookmarklets could help retrieving the list guid in this walkthrough.

    Dessie, I have no intent to spam, I just want to relink to my post without the extra ) that causes the link above to fail : http://blog.jonathanroussel.com/2009/09/retrieve-list-and-view-guids-with.html


Notify me of comments to this article:


Speak and you will be heard.

We check comments hourly.
If you want a pic to show with your comment, go get a gravatar!