terça-feira, 19 de maio de 2009

Visualizar PDF no site SharePoint

Este conceito é muito bacana e pra quem quer facilitar para olhar seu arquivo pdf no prórpio site pdf segue ai tutorial muito bom que encontrei.

O conceito é similar ao Page Viewer(PVWP) Esta WebPart cria um IFRAME object e seta atributo para a pagina.



Através de um DropDown você seleciona o nome do documento.



Crie uma biblioteca de documentos para inserir seus documentos em pdf
Aqui está sendo apontada para a biblioteca de documentos




Segue o código para criar está WebPart
Esta WebPart foi desenvolvida no Visual Studio 2008

[Personalizable(PersonalizationScope.Shared),
WebBrowsable(true),
WebDisplayName("Document Library"),
WebDescription("Enter in the Url to the document library")]
public string LibraryUrl
{
get { return _docLib; }
set { _docLib = value; }
}

Aqui o codigo método CreateChildControl para esta webpart

protected override void CreateChildControls()
{
base.CreateChildControls();

iframeObjectID = "iframe_" + guid;

string siteUrl;
string webUrl = string.Empty;
string fileContents = string.Empty;

//Define our DropDown List
dl = new DropDownList();
dl.Attributes.Add("onchange", "LoadFile_" + guid + "();");
dl.Items.Add("Select a document");

SortedDictionary <string,string> files;

try
{
//Is our document library path set?
if (this._docLib != null)
{
//decode any %20 spaces in URL
siteUrl = this._docLib.Replace("%20", " ");

//Open site collection
using (SPSite site = new SPSite(siteUrl))
{
//this block of code allows us to extract the relative Url of a subweb
if (site.Url.Length + 1 <= siteUrl.Length) { webUrl = siteUrl.Remove(0, site.Url.Length + 1); } //open web
using (SPWeb web = site.OpenWeb(webUrl))
{
//get handle to our document library
SPDocumentLibrary library = (SPDocumentLibrary)web.GetList(_docLib);

//create a generic sorted dictionary to store the files
files = new SortedDictionary<string, string>();

//loop through each file in root folder and add to sorted list
foreach (SPFile file in library.RootFolder.Files)
{
files.Add(file.Name, web.Url + "/" + file.Url);
}

//add each sorted file name to our drop down list
foreach (KeyValuePair<string, string> key in files)
{
ListItem item = new ListItem(key.Key, key.Value);
dl.Items.Add(item);
}
}
}
//Add drop down list and break tag to web part
this.Controls.Add(dl);
this.Controls.Add(new LiteralControl("
"
));

//Define a literal control to hold generated HTML (This will become our iframe)
iframe = new LiteralControl();
//Uniquely stamp this IFRAME
string frameGuid = new Guid().ToString();
iframe.Text = "";
this.Controls.Add(iframe);
}
else
{
//Set literal text to configure this web part
LiteralControl html = new LiteralControl("Click + this.ID + "')\">open tool pane to set path to Document Library");
this.Controls.Add(html);
}
}
catch (Exception ex)
{
//Display any error that comes up
this.Controls.Clear();
this.Controls.Add (new LiteralControl ("An error occurred: " + ex.Message.ToString()));
}


}

Agora o JScript para DropDown

crie antes
maiores detalhes veja aqui

protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);

if (!Page.ClientScript.IsClientScriptBlockRegistered ("script_" + guid))
{
//dynamically generate our JavaScript
string script = @"function LoadFile_" + guid + @" () {
var iframeobj = document.getElementById ('"
+ iframeObjectID + @"');
var selectobj = document.getElementById ('"
+ dl.ClientID + @"');
if (selectobj.value.toLowerCase().indexOf ('http') != -1)
{
iframeobj.src = selectobj.value;
iframeobj.style.visibility = 'visible';
}
else
{
iframeobj.src = '';
iframeobj.style.visibility = 'hidden';
}
}
"
;
//register our client side script that is an embedded resource
Page.ClientScript.RegisterClientScriptBlock (typeof (Page),"script_" + guid, script,true);
}
}


Agora é só fazer o deploy da solução

stsadm –o addsolution –filename <full path to WSP file>
stsadm –o deploysolution –filename –url <path to Web application>
stsadm –o activatefeature –name FileViewer –url <path to site collection where you want to use this>


Até Mais,

2 comentários: