Introduction

This time some findings related to JavaScript Object Model which I learn so I thought I should share this with all may be already shared by another techie. I’ll keep updating this blog as I’ll find new stuff related to JSOM.
  1. 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.
    1. //Make sure that the SP.js file is loaded
    2. SP.SOD.executeOrDelayUntilScriptLoaded(getCustomWebProperty, 'SP.js');
    3. function getCustomWebProperty()
    4. {
    5. var customWebProperty;
    6. //fetch the current context
    7. var context = SP.ClientContext.get_current();
    8. //fetch the all properties of current web
    9. allWebProperties = context.get_web().get_allProperties();
    10. context.load(allWebProperties);
    11. context.executeQueryAsync(
    12. function ()
    13. {
    14. //get specific property
    15. customWebProperty
    16. = allWebProperties.get_fieldValues().myCustomWebProperty;
    17. });
    18. }
  2. 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.
    1. //Make sure that the SP.UserProfiles.js file is loaded
    2. SP.SOD.executeOrDelayUntilScriptLoaded(getUserProfileProperty, 'SP.UserProfiles.js');
    3. function getUserProfileProperty()
    4. {
    5. var customUserProfileProperty;
    6. var context = SP.ClientContext.get_current();
    7. //people manager provides methods for operation related to people
    8. var mgrPeople = new SP.UserProfiles.PeopleManager(context);
    9. //fetch the user properties of current user – Return Type: SP.UserProfiles.PersonProperties
    10. var myProps = mgrPeople.getMyProperties();
    11. context.load(myProps);
    12. context.executeQueryAsync(
    13. function ()
    14. {
    15. //fetch the user profile properties for the user.
    16. var profileProps = myProps.get_userProfileProperties();
    17. //fetch the specific property
    18. customUserProfileProperty =
    19. profileProps.MyCustomUserProfilePropertyName;
    20. });
    21. }
Thanks
Feel free to comment / feedback if any or if you have any query