Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SharePoint get current user attribute

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>";
like image 660
Eric Di Bari Avatar asked Jan 29 '26 16:01

Eric Di Bari


2 Answers

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
});
like image 169
Tom Resing Avatar answered Jan 31 '26 05:01

Tom Resing


I've never answered my own question, but I managed to come up with a simple solution. Here is how I went about it

  1. When a user is logged in, there is a global JavaScript variable that includes that user's ID, - __spUserId
  2. The user profile list is accessible via a CAML query through SOAP, it's titled 'User Information List'.
  3. When accessing this list, you have to set the top-level site to point to _catalogs instead of Lists (or whatever it is exactly). You do this by adding the element _catalogs \
  4. Since it is accessing the user profile list as a list and not through a userprofile function, set the query type (?) to 'GetListItems'

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\""
    });
});
like image 44
Eric Di Bari Avatar answered Jan 31 '26 05:01

Eric Di Bari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!