Sunday, 23 February 2014

JSOM: Accessing Site Collection / Web properties and user profile properties

Hi Again,
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 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.
//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();
        function () {
                   //get specific property
= allWebProperties.get_fieldValues().myCustomWebProperty;
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. 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();
function () {
                //fetch the user profile properties for the user.
                var profileProps = myProps.get_userProfileProperties();
//fetch the specific property
customUserProfileProperty =
Enjoy reading J
Feel free to comment / feedback if any or if you have any query J

No comments: