Mit JavaScript Daten von einem WebService abfragen
Vor Kurzem hatte ich wieder eine Problematik, die ich mir aus vielen Teilen zusammen suchen musste. Und zwar war die Anforderungen Daten des aktuellen Benutzers aus dem Benutzerprofil auszulesen und in eines der Felder des Eingabeformulars eines NewItem-Forms zu schreiben.
Der Code ist nicht ganz optimal, funktioniert aber. Vor allem die Methode GetCurrentUrl sollte beachtet werden, da diese hier noch angepasst werden müsste.
<script type="text/javascript" language="javascript"> _spBodyOnLoadFunctionNames.push("setLocationAndDivision"); function setLocationAndDivision() { var tags = document.getElementsByTagName('input'); for (var i=0; i < tags.length; i++) { if (tags[i].title=='Feldbezeichnung') var field =tags[i] } //Get user attributes field.value = GetUserProfileProperty('AccountName'); } function GetUserProfileProperty(propertyName) { var a = new ActiveXObject("Microsoft.XMLHTTP"); if(a == null) return null; a.Open("POST", GetRootUrl() + "_vti_bin/UserProfileService.asmx", false); a.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); a.setRequestHeader("SOAPAction", "http://microsoft.com/webservices/SharePointPortalServer/UserProfileService/GetUserProfileByName"); var d = '<?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>" +" <GetUserProfileByName xmlns=\"http://microsoft.com/webservices/SharePointPortalServer/UserProfileService\">" +" <AccountName></AccountName>" +" </GetUserProfileByName>" +" </soap:Body>" +"</soap:Envelope>"; a.Send(d); if (a.status != 200) { return null; } else { return a.responseXML.selectSingleNode("//PropertyData[Name = \""+propertyName+"\"]/Values/ValueData/Value").text; //return a.responseXML.selectNodes('//Row'); } } function GetRootUrl() { var pathparts = document.location.pathname.split('/'); var url = 'http://' + document.location.hostname + pathparts[1] + '/'+pathparts[2]+'/'; return url; } </script>