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.

  1. <script type="text/javascript" language="javascript">
  2. _spBodyOnLoadFunctionNames.push("setLocationAndDivision");
  3. function setLocationAndDivision() {
  4. var tags = document.getElementsByTagName('input');
  5. for (var i=0; i < tags.length; i++) {
  6. if (tags[i].title=='Feldbezeichnung')
  7. var field =tags[i]
  8. }
  9. //Get user attributes
  10. field.value = GetUserProfileProperty('AccountName');
  11. }
  12.  
  13. function GetUserProfileProperty(propertyName)
  14. {
  15. var a = new ActiveXObject("Microsoft.XMLHTTP");
  16. if(a == null) return null;
  17. a.Open("POST", GetRootUrl() + "_vti_bin/UserProfileService.asmx", false);
  18. a.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
  19. a.setRequestHeader("SOAPAction", "http://microsoft.com/webservices/SharePointPortalServer/UserProfileService/GetUserProfileByName");
  20.  
  21. var d = '<?xml version=\"1.0\" encoding=\"utf-8\"?>'
  22. +"<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/\">"
  23. +" <soap:Body>"
  24. +" <GetUserProfileByName xmlns=\"http://microsoft.com/webservices/SharePointPortalServer/UserProfileService\">"
  25. +" <AccountName></AccountName>"
  26. +" </GetUserProfileByName>"
  27. +" </soap:Body>"
  28. +"</soap:Envelope>";
  29. a.Send(d);
  30.  
  31. if (a.status != 200)
  32. {
  33. return null;
  34. }
  35. else
  36. {
  37. return a.responseXML.selectSingleNode("//PropertyData[Name = \""+propertyName+"\"]/Values/ValueData/Value").text;
  38. //return a.responseXML.selectNodes('//Row');
  39. }
  40. }
  41.  
  42. function GetRootUrl()
  43. {
  44. var pathparts = document.location.pathname.split('/');
  45. var url = 'http://' + document.location.hostname + pathparts[1] + '/'+pathparts[2]+'/';
  46. return url;
  47. }
  48. </script>
  49.  

Kommentare