I have a page that needs to retrieve the department for the logged-in user. Ideally I'd like to be able to do it through a JavaScript SOAP CAML query. I have the user's id (which I'm assuming doubles as the GUID), so it seems like a simple matter of retrieving the row that matches the ID.
I'm looking into using the 'GetUserProfileByIndex' or 'GetUserProfileByGuid' function in SOAP, but I can't seem to find any solid documentation or decent examples using them. I'm hoping to do something like this in JavaScript - (which isn't working):
var userId = 194;
"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
<soapenv:Body> \
<GetUserProfileByIndex xmlns='http://microsoft.com/webservices/SharePointPortalServer/UserProfileService'> \
<index>userId</index> \
</GetUserProfileByIndex > \
</soapenv:Body> \
</soapenv:Envelope>";
I recommend you check out the jQuery Library for SharePoint 2007 and 2010 called SPServices. $().SPServices.SPGetCurrentUser can retrieve the Department in an effecient 4 lines of code:
var thisDepartment = $().SPServices.SPGetCurrentUser({
fieldName: "Department",
debug: false
});
I've never answered my own question, but I managed to come up with a simple solution. Here is how I went about it
Overall, here is the SOAP call and CAML query
$(document).ready(function() {
var soapEnv =
"<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'> \
<soapenv:Body> \
<GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'> \
<listName>User Information List</listName> \
<topLevelSite>_catalogs</topLevelSite> \
<query> \
<Query> \
<Where> \
<Contains> \
<FieldRef Name='ID' /> \
<Value Type='Text'>"+_spUserId+"</Value> \
</Contains> \
</Where> \
</Query> \
</query> \
<viewFields> \
<ViewFields> \
<FieldRef Name='Name' /> \
<FieldRef Name='Department' /> \
<FieldRef Name='ID' /> \
</ViewFields> \
</viewFields> \
<query> \
<Query /> \
</query> \
</GetListItems> \
</soapenv:Body> \
</soapenv:Envelope>";
$.ajax({
url: "/_vti_bin/Lists.asmx",
type: "POST",
dataType: "xml",
data: soapEnv,
complete: processResult,
contentType: "text/xml; charset=\"utf-8\""
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With