Skip Ribbon Commands
Skip to main content

Robin | zevenseas | SharePoint Blog

:

The zevenseas Community > Blogs > Robin | zevenseas | SharePoint Blog > Posts > Using the PageViewerWebPart to show a list or document library as a ListViewWebPart
August 23
Using the PageViewerWebPart to show a list or document library as a ListViewWebPart

@brianreeves was wondering how my buddy Baris had used a PageViewer WebPart to display a List/DocumentLibrary like a ListViewWebPart in his Automatically resizing Sharepoint Page Viewer Web Part to its content post. The idea behind this solution was to show a list or document library from a collaboration environment on a publishing environment.

So by giving the Url of a view of a list/documentlibrary the PageViewerWP displays the contents of that view. But when giving the Url you only want to see the contents as a ListViewWebPart and not the complete page right?

To achieve this I created a solution that does the following :

  1. Added custom action to lists and document libraries that makes a copy of a selected View
    1. private Guid CreatePublishingView(SPWeb web, Guid listGuid, Guid viewGuid)
      
      {            
      
          SPList list = web.Lists[listGuid];
      
          SPView originalView = list.Views[viewGuid];
      
          SPView publishingView = originalView.Clone("Publishing" + originalView.Title, originalView.RowLimit, originalView.Paged, false);                     
      
          publishingView.Update();
      
          list.Update();
      
          return publishingView.ID;
      
      }      
  2. While making the copy, I open up the .aspx page that is created and replace the referenced masterpage
    1. private void EditViewPage(SPWeb web, Guid listGuid, Guid publishingViewGuid)
      
      {
      
          //Get a reference to the list and the choosen view
      
          SPList list = web.Lists[listGuid];            
      
          SPView publishingView = list.Views[publishingViewGuid];
      
          //Get a reference to the actual .aspx page of the view
      
          SPFile file = web.GetFile(publishingView.ServerRelativeUrl);
      
          //Opening the .aspx
      
          System.IO.StreamReader streamReader = new System.IO.StreamReader(file.OpenBinaryStream());
      
          string fileInStream = streamReader.ReadToEnd();
      
          streamReader.Close();
      
          //doing a string replacement of the masterpage
      
          string oldMasterPage = "MasterPageFile=\"~masterurl/default.master\"";
      
          string newMasterPage = "MasterPageFile=\"~site/empty.master\"";
      
          //saving the .aspx back to the file object and perform an update
      
          fileInStream = fileInStream.Replace(oldMasterPage, newMasterPage);
      
          ASCIIEncoding asciiEncoder = new ASCIIEncoding();
      
          file.SaveBinary(asciiEncoder.GetBytes(fileInStream));
      
          file.Update();
      
      }
  3. Copying the empty.master file to the Web where the List or Document Library is at
    1. private void AddPageToFormLibrary(SPWeb web, Guid listGuid, SPFolder folder, string fileName)
      
      {
      
          try
      
          {
      
              //Getting the folder where the custom .browser file is
      
              string filePath = String.Format("{0}\\FEATURES\\{1}\\{2}", SPUtility.GetGenericSetupPath("Template"), featureName, fileName);
      
              FileInfo fi = new FileInfo(filePath);
      
              byte[] byt = new byte[Convert.ToInt32(fi.Length)];
      
              FileStream strm = fi.OpenRead();
      
              strm.Read(byt, 0, Convert.ToInt32(fi.Length));
      
              strm.Close();
      
              folder.Files.Add(fi.Name, byt, true);
      
          }
      
          catch (Exception error)
      
          {
      
              SPUtility.TransferToErrorPage("Adding Page to Library failed due to :" + error.Message.ToString());
      
          }
      
      } 
  4. And that’s it! Now we have a View that looks like a ListViewWebPart so we only have copy the Url of this view and use it in a PageViewerWebPart and we’re done! So the beauty of this is the user is able to modify the view just as a .. a view.. ;)

I hope this makes sense and I have to admit that it looks a bit hacky.. Let me know if you want to see how the empty.master looks like!

Comments

Brian Reeves

This is great...definitely NOT a hack job!  MY hack job was to create CSS CEWP code to remove all of the other items on the Web Part Page and display THAT in the Page Viewer Web Part. (which works quite well BTW for SharePoint End Users w/o programming skills :).

YOUR solution actually shows some programming thought!  :)

Again, great solution.
System Account on 24/08/2009 09:30

Robin

Thanks Brian.. :) Then again if your CSS CEWP works it's even better because no-code is a lot stronger than solutions that are coded!
System Account on 24/08/2009 22:39

Lucas Stark

Exactly what I was looking for.   I would be interested if you could share your empty.master file.

System Account on 03/10/2009 07:40

Robin

Hi Lucas, sure..

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="wssuc" TagName="Welcome" src="~/_controltemplates/Welcome.ascx" %>
<%@ Register TagPrefix="wssuc" TagName="DesignModeConsole" src="~/_controltemplates/DesignModeConsole.ascx" %>
<html dir="<%$Resources:wss,multipages_direction_dir_value%>" runat="server" xmlns:o="urn:schemas-microsoft-com:office:office" __expr-val-dir="ltr">
<head runat="server">

<WebPartPages:SPWebPartManager runat="server" />
<SharePoint:RobotsMetaTag runat="server" /><%-- The head section includes a content placeholder for the page title and links to CSS and JavaScript files that run on the server. --%>
<asp:ContentPlaceHolder runat="server" id="head">
 <title>
  <asp:ContentPlaceHolder id="PlaceHolderPageTitle" runat="server" />
 </title>
</asp:ContentPlaceHolder>
<Sharepoint:CssLink runat="server" />
<asp:ContentPlaceHolder id="PlaceHolderAdditionalPageHead" runat="server" >
<script type="text/JavaScript">
function resizeMe(that) {
window.resizeTo(document.body.scrollWidth, document.body.scrollHeight+10);
}
function startTimeOut(){
window.setTimeout("resizeMe()", 100);
}
if (window.addEventListener) //DOM method for binding an event
window.addEventListener("load", startTimeOut, false)
else if (window.attachEvent) //IE exclusive method for binding an event
window.attachEvent("onload", startTimeOut)
else if (document.getElementById) //support older modern browsers
window.onload=startTimeOut;
</script>
</asp:ContentPlaceHolder>

</head>
<body>
<form runat="server" onsubmit="return _spFormOnSubmitWrapper();">
<asp:Panel ID="Panel1" visible="false" runat="server">
<wssuc:Welcome id="explitLogout" runat="server" />
<SharePoint:SiteActions runat="server" AccessKey="<%$Resources:wss,tb_SiteActions_AK%>" id="SiteActionsMenuMain" PrefixHtml="&lt;div&gt;&lt;div&gt;" SuffixHtml="&lt;/div&gt;&lt;/div&gt;" MenuNotVisibleHtml="&amp;nbsp;">
             <CustomTemplate>
             <SharePoint:featuremenutemplate runat="server"
              FeatureScope="Site"
              Location="Microsoft.SharePoint.StandardMenu"
              GroupId="SiteActions"
              UseShortId="true"
              >
              <SharePoint:menuitemtemplate runat="server" id="MenuItem_Create"
               Text="<%$Resources:wss,viewlsts_pagetitle_create%>"
               Description="<%$Resources:wss,siteactions_createdescription%>"
               ImageUrl="/_layouts/images/Actionscreate.gif"
               MenuGroupId="100"
               Sequence="100"
               UseShortId="true"
               ClientOnClickNavigateUrl="~site/_layouts/create.aspx"
               PermissionsString="ManageLists, ManageSubwebs"
               PermissionMode="Any" />
              <SharePoint:menuitemtemplate runat="server" id="MenuItem_EditPage"
               Text="<%$Resources:wss,siteactions_editpage%>"
               Description="<%$Resources:wss,siteactions_editpagedescription%>"
               ImageUrl="/_layouts/images/ActionsEditPage.gif"
               MenuGroupId="100"
               Sequence="200"
               ClientOnClickNavigateUrl="javascript:MSOLayout_ChangeLayoutMode(false);"
               />
              <SharePoint:menuitemtemplate runat="server" id="MenuItem_Settings"
               Text="<%$Resources:wss,settings_pagetitle%>"
               Description="<%$Resources:wss,siteactions_sitesettingsdescription%>"
               ImageUrl="/_layouts/images/ActionsSettings.gif"
               MenuGroupId="100"
               Sequence="300"
               UseShortId="true"
               ClientOnClickNavigateUrl="~site/_layouts/settings.aspx"
               PermissionsString="EnumeratePermissions,ManageWeb,ManageSubwebs,AddAndCustomizePages,ApplyThemeAndBorder,ManageAlerts,ManageLists,ViewUsageData"
               PermissionMode="Any" />
             </SharePoint:featuremenutemplate>
             </CustomTemplate>
            </SharePoint:SiteActions>
            </asp:Panel>
<asp:ContentPlaceHolder id="PlaceHolderMain" runat="server" />
 <asp:Panel visible="false" runat="server">
  <asp:ContentPlaceHolder id="PlaceHolderSearchArea" runat="server"/>
  <asp:ContentPlaceHolder id="PlaceHolderTitleBreadcrumb" runat="server"/>
  <asp:ContentPlaceHolder id="PlaceHolderPageTitleInTitleArea"  runat="server"/>
  <asp:ContentPlaceHolder id="PlaceHolderLeftNavBar" runat="server"/>
  <asp:ContentPlaceHolder ID="PlaceHolderPageImage" runat="server"/>
  <asp:ContentPlaceHolder ID="PlaceHolderBodyLeftBorder" runat="server"/>
  <asp:ContentPlaceHolder ID="PlaceHolderNavSpacer" runat="server"/>
  <asp:ContentPlaceHolder ID="PlaceHolderTitleLeftBorder" runat="server"/>
  <asp:ContentPlaceHolder ID="PlaceHolderTitleAreaSeparator" runat="server"/>
  <asp:ContentPlaceHolder ID="PlaceHolderMiniConsole" runat="server"/>
  <asp:ContentPlaceHolder id="PlaceHolderCalendarNavigator" runat ="server" />
  <asp:ContentPlaceHolder id="PlaceHolderLeftActions" runat ="server"/>
  <asp:ContentPlaceHolder id="PlaceHolderPageDescription" runat ="server"/>
  <asp:ContentPlaceHolder id="PlaceHolderBodyAreaClass" runat ="server"/>
 <asp:ContentPlaceHolder id="PlaceHolderTitleAreaClass" runat ="server"/>
 </asp:Panel>
</form>

</body>

</html>

System Account on 06/10/2009 02:04

Syed

Hey ,

Its an excellent article... I am getting an Exception (Cannot Complete this action, Please try again) while calling the Clone method of view while creating a new view.. Please help me
System Account on 28/01/2010 14:24
 

 Statistics

 
Views: 5113
Comments: 5
Tags:
Published:1365 Days Ago