Xslt, JavaScript y WebService en Sharepoint – Demo 2

No solo de WebParts vive el hombre – Parte 1 – Demostración 2
En el día de ayer, 06-12-2007 tuvimos la oportunidad de realizar un WebCast a la comunidad de desarrolladores de Sharepoint y le damos las gracias a Brandon Aday de Microsoft por la oportunidad que nos brindo.
En este WebCast presentamos una forma alternativa de realizar componentes para Sharepoint sin utilizar código de servidor.
Acá les dejamos parte del código fuente de la primera presentación realizada, los archivos JavaScript y el formulario ASPX utilizado

Código Fuente – Formulario aspx
<%@ Page Language=”C#” masterpagefile=”~masterurl/default.master” title=”Demo WebService” inherits=”Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” meta:progid=”SharePoint.WebPartPage.Document” %>

<asp:Content id=”Content1″ runat=”Server” contentplaceholderid=”PlaceHolderMain”>

<script type=”text/javascript”>

_spBodyOnLoadFunctionNames.push(“InvocarWebService”);

</script>

<div id=”divResultado”>

</div>

</asp:Content>

<asp:Content id=”Content2″ runat=”server” contentplaceholderid=”PlaceHolderAdditionalPageHead”>

<script type=”text/javascript” src=”soap.js” id=”SoapJs”></script>

<script type=”text/javascript” src=”demo2.js” id=”Demo2Js”></script>

</asp:Content>

Código Fuente – JavaScript demo2.js

function OnLoadListsStart(){

alert(“Comienza invocación al WebService.”);

}

function OnLoadListsComplete(pStrXml){

LoadResultado(pStrXml);

}

function OnLoadListsError(){

alert(‘Se produjo un error en la invocación.’);

}

function LoadResultado(successResponse)

{

var strHtml;

var XmlRsp = successResponse;

successResponse = null;

var XMLDoc;

boolLoadSltList = false;

if (window.ActiveXObject)

{

XMLDoc=new ActiveXObject(“Microsoft.XMLDOM”);

XMLDoc.async=false;

XMLDoc.loadXML(XmlRsp);

}

// code for Mozilla, Firefox, Opera, etc.

else if (document.implementation && document.implementation.createDocument)

{

XMLDoc=document.implementation.createDocument(“”,””,null);

XMLDoc.load(XmlRsp);

}

strHtml = “<table cellSpacing=’0′ class=’ms-menutoolbar’ cellPadding=’0′ border=’0′ width=’100%’>”;

strHtml += “<tr>”;

strHtml += “<td class=’ms-toolbar’ width=’80%’>Documentos de la lista</td>”;

strHtml += “</tr>”;

strHtml += “</table>”;

strHtml += “<table>”;

var nodeList = XMLDoc.getElementsByTagName(“rs:data”);

var node = nodeList.item(0);

if(node.childNodes.length > 0)

{

for(i=0; i < node.childNodes.length; i++)

{

strHtml += “<tr>”;

var childNode = node.childNodes[i];

strHtml += “<td>” + DevolverImg(childNode.getAttribute(“ows_DocIcon”));

strHtml += “</td>”;

strHtml += “<td class=’ms-vb’>” + “<a href=’/” + childNode.getAttribute(“ows_FileRef”).split(‘#’)[1] + “‘>” + childNode.getAttribute(“ows_LinkFilename”);

strHtml += “</a>”;

strHtml += “</td>”;

strHtml += “</tr>”;

}

}

else

{

strHtml += “<tr>”;

strHtml += “<td> No hay elementos que mostrar en esta vista.”;

strHtml += “</td>”;

strHtml += “</tr>”;

}

strHtml += “</table>”;

var divRes = document.getElementById(“divResultado”);

divRes.innerHTML = strHtml;

}

function DevolverImg(tipoDoc)

{

var strImg = “<img border=’0′ src='”;

var src;

if(tipoDoc==”docx”)

{

src=”_layouts/images/icdocx.gif”;

}

else if (tipoDoc==”doc”)

{

src=”_layouts/images/icdocx.gif”;

}

else if (tipoDoc==”xlsx”)

{

src=”_layouts/images/icxlsx.gif”;

}

else if (tipoDoc==”xls”)

{

src=”_layouts/images/icxlsx.gif”;

}

else

{

src=”_layouts/images/edit.gif”;

}

strImg += src + “‘ width=’16’ height=’16’ />”;

return strImg;

}

function InvocarWebService()

{

var lUrlWebService = ‘/webcast/_vti_bin/Lists.asmx’;

var lSoapActionNamespace = ‘http://schemas.microsoft.com/sharepoint/soap/’;

var lSoapAction = ‘GetListItems’;

var lParameters = new Array();

lParameters[0] = ‘listName’;

lParameters[1] = ‘Documentos’;

CallWebService(lUrlWebService,lSoapActionNamespace,lSoapAction,

lParameters,OnLoadListsStart,OnLoadListsComplete,OnLoadListsError);

}

Código Fuente – Soap.js

/// Libreria para consumir webservices

// Objeto para crear el envelop

var Soap = {

createEnvelope: function(action, ns, parameters){

var soap = ‘<?xml version=”1.0″ encoding=”utf-8″?><soap:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:soap=”http://www.w3.org/2003/05/soap-envelope”><soap:Body>’;

//var soap = ‘<?xml version=”1.0″ encoding=”utf-8″?><soap:Envelope xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance” xmlns:xsd=”http://www.w3.org/2001/XMLSchema” xmlns:soap=”http://schemas.xmlsoap.org/soap/envelope/ “><soap:Body>’;

soap += ‘<‘ + action + ‘ xmlns=”‘ + ns + ‘”>’;

soap += Soap.__parseParameters(parameters);

soap += ‘</’ + action + ‘></soap:Body></soap:Envelope>’;

return soap;

},

__parseParameters: function(parameters){

var params = “”;

if (typeof parameters == ‘object’){

// check if we were provided an array or an object

if (typeof parameters.push == ‘function’){

for (var i = 0, length = parameters.length; i < length; i += 2){

params += “<” + parameters[i] + “>” + parameters[i+1] + “</” + parameters[i] + “>”;

}

}

else{

$H(parameters).each(

function(pair){

params += “<” + pair.key + “>” + pair.value + “</” + pair.key + “>”;

});

}

}

return params;

}

}

// CallWebServices

// pUrlWebService

// pSoapActionNamespace

// pSoapAction

// pParameters

// pStartEvent

// pCompleteEvent

// pErrorEvent

function CallWebService(pUrlWebService, pSoapActionNamespace, pSoapAction, pParameters, pStartEvent, pCompleteEvent, pErrorEvent){

var lXmlSoap = Soap.createEnvelope(pSoapAction,pSoapActionNamespace,pParameters);

__getServiceResults(pUrlWebService,pSoapActionNamespace + pSoapAction,lXmlSoap,pStartEvent,pCompleteEvent,pErrorEvent);

}

function __getServiceResults(pUrlWebService, pSoapAction, pXmlSoap, pStartEvent, pCompleteEvent, pErrorEvent){

var lXmlHttp;

function onReadyStateChange(e){

switch(lXmlHttp.readyState){

case 1:

//pStartEvent();

break;

case 4:

if(lXmlHttp.status == 200){

pCompleteEvent(lXmlHttp.responseText);

}

else{

pErrorEvent();

}

break;

default:

break;

}

}

try{

if (pXmlSoap.length > 0){

pStartEvent();

// debug:

//alert(pXmlSoap);

if (window.XMLHttpRequest){

lXmlHttp = new XMLHttpRequest()

}

else if (window.ActiveXObject){

lXmlHttp = new ActiveXObject(“Microsoft.XMLHTTP”)

}

lXmlHttp.open(“POST”, pUrlWebService, true);

lXmlHttp.onreadystatechange = onReadyStateChange;

lXmlHttp.setRequestHeader(“SOAPAction”, pSoapAction);

lXmlHttp.setRequestHeader(“Content-Type”, “text/xml”);

lXmlHttp.send(pXmlSoap);

}

}

catch(e){

alert(e.message);

pErrorEvent();

}

}

Muchas gracias a los que asistieron y nos hicieron llegar sus comentarios………

Fabián Imaz
Siderys Elite Software

Compartir