Want to become a Vibe Coder? Join Vibe Coding Training here
x
C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Accessing Site Collection / Web properties and user Profile Properties in Javascript
WhatsApp
Prasham Sabadra
5y
5.8
k
0
1
25
Blog
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 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
People also reading
Membership not found