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.
- 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 root web’s property bag.
Following is the sample code for accessing web property in JavaScript. Following code will require SP.Js file to be loaded.
-
- SP.SOD.executeOrDelayUntilScriptLoaded(getCustomWebProperty, 'SP.js');
-
- function getCustomWebProperty()
- {
- var customWebProperty;
-
- var context = SP.ClientContext.get_current();
-
- allWebProperties = context.get_web().get_allProperties();
- context.load(allWebProperties);
- context.executeQueryAsync(
- function ()
- {
-
- 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.
Following code will require SP.UserProfiles.Js file to be loaded.
-
- SP.SOD.executeOrDelayUntilScriptLoaded(getUserProfileProperty, 'SP.UserProfiles.js');
-
- function getUserProfileProperty()
- {
- var customUserProfileProperty;
- var context = SP.ClientContext.get_current();
-
- var mgrPeople = new SP.UserProfiles.PeopleManager(context);
-
- var myProps = mgrPeople.getMyProperties();
- context.load(myProps);
- context.executeQueryAsync(
- function ()
- {
-
- var profileProps = myProps.get_userProfileProperties();
-
- customUserProfileProperty =
- profileProps.MyCustomUserProfilePropertyName;
- });
- }
Thanks
Feel free to comment / feedback if any or if you have any query