| @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 : - Added custom action to lists and document libraries that makes a copy of a selected View
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;
}
- While making the copy, I open up the .aspx page that is created and replace the referenced masterpage
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();
}
- Copying the empty.master file to the Web where the List or Document Library is at
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());
}
}
- 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! |