1

In my sharepoint 2010 environment, I am using the client object model (ecma script/javascript) to display people information.

In my user profile service in sharepoint, I have created some custom properties (using Business Connectivity Services) for people to have.

One of them is called "MyOffice".

How can I get the value of this property using the client object model script for the current user logged in?

I know that I can do things like get items created by current user using the <UserID/> code in caml query, or if I get an items Author field, I can call the getlookupid() function on it, and it will give me its username.

Is there a way like this I can get the value of a custom property, if I pass in a people object or lookup name or something?

Thanks.

2 Answers 2

1

Unfortunately, there is no way to directly query the User Profile Service through JavaScript in SharePoint 2010 (It is possible in SP2013).

However, you can do couple of workarounds for this:

1) As @Falak mentioned, you can query the Site User Information List to get the property. But for the custom property to show up in the Site User Info list, you have to go to the Central Administration -> Manage User Profile Properties and edit your custom property. In the "Policy Settings" check the "Replicable" property. After setting the custom property as Replicable, it will start showing up in the User Information List which you can query with JavaScript as shown in @Falak's answer.

2) User Server Side Code (C#/.NET) to access the User Profile Service and get the value of the custom property. Then write the value to the page in a JavaScript variable which you can access with your JavaScript code. Example: (Not Tested/Demo Purposes Only)

Controls.Add(new LiteralControl("<script>var myOfficeJSProp = "+ cSharpVariableWithValueOfCustomProperty +";</script>"));
0

You can load the current user using _spUserId variable and get it's custom property:

<script type="text/ecmascript" language="ecmascript">
    ExecuteOrDelayUntilScriptLoaded(getProfile, "sp.js");

    var context = null;
    var web = null;         
    function getProfile() {
        context = SP.ClientContext.get_current();
        web = context.get_web();
        userInfoList = web.get_siteUserInfoList();
        camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\'ID\'/><Value Type=\'Number\'>' + _spUserId + '</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>');
        this.listItems = userInfoList.getItems(camlQuery);
        context.load(listItems);
        context.executeQueryAsync(Function.createDelegate(this, this.onProfileSuccessMethod), Function.createDelegate(this, this.onFailureMethod));
    }

    function onProfileSuccessMethod(sender, args) {
        var item = listItems.itemAt(0);
        var picture = item.get_item('Picture');
        if (picture) {
           alert(picture.get_url());
        } 
    }

    function onFailureMethod(sender, args) {
        alert('Error: ' + args.get_message() + '\n' + args.get_stackTrace());
    }
</script>
1
  • When I tried it on Picture I get the url of the picture image, but when I try a custom property, I get the javascript error [11:06:34.561] Error: The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested..
    – omega
    Commented Jun 5, 2013 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.