Los webparts son elementos dentro de cualquier página de SharePoint, cada webpart tiene su función en particular, sea este mostrar el listado de noticias, encuesta, contenido, etc.
En esta ocasión hemos elaborado un WebPart que nos permite listar todos los WebPart que se encuentran en la página actual para seleccionarlos e imprimirlos por separado.
A continuación el código C#:
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
namespace PrintWebPart
{
[Guid("4f05379d-c566-4c6a-9785-b7243069e87f")]
public class PrintWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
public PrintWebPart()
{
}
protected override void CreateChildControls()
{
base.CreateChildControls();
try
{
WebPartZoneCollection zones = this.WebPartManager.Zones;
Literal ddl = new Literal();
int count=-1;
ddl.Text = "<select id='WebPartListIT01'>";
foreach (WebPartZone zone in zones)
{
WebPartCollection webparts = zone.WebParts;
foreach (WebPart webpart in webparts)
{
count++;
ddl.Text += string.Format("<option value='WebPartWPQ{0}'>{1}</option>", count,webpart.Title);
}
}
ddl.Text += "</select>";
Literal btnImprimir = new Literal();
btnImprimir.Text = "<INPUT onclick=javascript:void(Imprime()) type=button value='Imprimir'>";
this.Controls.Add(ddl);
this.Controls.Add(btnImprimir);
}
catch (Exception ex)
{
Label labelError = new Label();
labelError.Text = ex.ToString() + ex.Source.ToString() + ex.StackTrace.ToString();
this.Controls.Add(labelError);
}
}
}
}
A continuación código Javascript:
function PrintWebPart(WebPartElementID)
{
var bolWebPartFound = false;
if (document.getElementById != null)
{
//Create html to print in new window
var PrintingHTML = '<HTML>\n<HEAD>\n';
//Take data from Head Tag
if (document.getElementsByTagName != null)
{
var HeadData= document.getElementsByTagName("HEAD");
if (HeadData.length > 0)
PrintingHTML += HeadData[0].innerHTML;
}
PrintingHTML += '\n</HEAD>\n<BODY>\n';
var WebPartData = document.getElementById(WebPartElementID);
if (WebPartData != null)
{
PrintingHTML += WebPartData.innerHTML;
bolWebPartFound = true;
}
else
{
bolWebPartFound = false;
alert ('Cannot Find Web Part');
}
}
PrintingHTML += '\n</BODY>\n</HTML>';
//Open new window to print
if (bolWebPartFound)
{
var PrintingWindow = window.open("","PrintWebPart", "toolbar,width=800,height=600,scrollbars,resizable,menubar");
PrintingWindow.document.open();
PrintingWindow.document.write(PrintingHTML);
// Open Print Window
PrintingWindow.print();
}
}
function Imprime()
{
//if (ob.selectedIndex != 0) arSelected.push(ob.options[ob.selectedIndex].value);
var ob= document.getElementById('WebPartListIT01');
var WebPart_Id= ob.options[ob.selectedIndex].value;
PrintWebPart(WebPart_Id);
}
El resultado sería lo siguiente: Un listado de los webparts dentro de la página y el botón imprimir
Actualmente calificado con 5.0 por 1 personas
- Currently 5/5 Stars.
- 1
- 2
- 3
- 4
- 5