Introduction
- We have a requirement like to read the site collection properties in Java Script. First of all, there is no property bag for SiteCollection. Those properties are stored in the root web’s property bag.
Following is the sample code for accessing web property in JavaScript. The following code will require SP.Js files to be loaded.
- //Make sure that the SP.js file is loaded
- SP.SOD.executeOrDelayUntilScriptLoaded(getCustomWebProperty, 'SP.js');
- function getCustomWebProperty()
- {
- var customWebProperty;
- //fetch the current context
- var context = SP.ClientContext.get_current();
- //fetch the all properties of current web
- allWebProperties = context.get_web().get_allProperties();
- context.load(allWebProperties);
- context.executeQueryAsync(
- function ()
- {
- //get specific property
- customWebProperty
- = allWebProperties.get_fieldValues().myCustomWebProperty;
- });
- }
- Similarly we have also requirement to read the user profile property in JavaScript. One quick update here is we couldn’t update user profile properties from JavaScript, we can just read those.
Following is the sample code for accessing web property in JavaScript.The following code will require SP.UserProfiles.Js file to be loaded.
- //Make sure that the SP.UserProfiles.js file is loaded
- SP.SOD.executeOrDelayUntilScriptLoaded(getUserProfileProperty, 'SP.UserProfiles.js');
- function getUserProfileProperty()
- {
- var customUserProfileProperty;
- var context = SP.ClientContext.get_current();
- //people manager provides methods for operation related to people
- var mgrPeople = new SP.UserProfiles.PeopleManager(context);
- //fetch the user properties of current user – Return Type: SP.UserProfiles.PersonProperties
- var myProps = mgrPeople.getMyProperties();
- context.load(myProps);
- context.executeQueryAsync(
- function ()
- {
- //fetch the user profile properties for the user.
- var profileProps = myProps.get_userProfileProperties();
- //fetch the specific property
- customUserProfileProperty =
- profileProps.MyCustomUserProfilePropertyName;
- });
- }
Thanks
Feel free to comment / feedback if any or if you have any query

Rajesh GojiyaPosted Mar 15, 2015, 12:28 AM
Love the Olacabs app. Give it a swirl! Sign up with my referral code A7NG1X and earn Rs.100 Download the app from: www.ola.bz/b
Vithal WadjePosted Mar 14, 2015, 1:58 PM
nice start keep it up