Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
How to queue 'Personal Site'
*When we work with Social features in SharePoint one main pre-requisite is 'User should have a personal site'. *
Personal Site is self-service site collection, so once user clicks on "About me" links from the upper-right corner menu it will enable user’s personal site. But multiple time we need to provision user’s personal site via code on-demand.
In this article, we will enable Personal site using JavaScript.
Step by Step
The first step is to check if the user already has a “personal site”.
var userHavePersonalSite = false;
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=PersonalUrl",
method: "GET",
async: false,
headers: {
"ACCEPT": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose"
},
success: function (data) {
//If user has personal site, set the variable to true
if (data && data.d && data.d.PersonalUrl && data.d.PersonalUrl != "" && data.d.PersonalUrl.indexOf("/personal/") != -1)
userHavePersonalSite = true;
},
error: function (data) { }
})
For checking Personal Site, we will access 'PersonalUrl' from user properties and check if it has '/personal/' (Managed properties used in SharePoint online setup) token.
If '/personal/' token index is greater then -1 it means users already have My Site.
More detail on Managed property can be found here Second step is to get the ‘FormDigest’, there are 2 ways to get the values
- Using _spPageContextInfo
_spPageContextInfo.formDigestValue
- Using 'contextinfo'
var digest = "";
$.ajax({
url: "_api/contextinfo",
method: "POST",
async: false,
headers: {
"ACCEPT": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose"
},
success: function (data) {
digest = data.d.GetContextWebInformation.FormDigestValue;
},
error: function (e) { oPleaseWaitDialog.close(); }
})
Below code will use the second method to get "FormDigest". This is because we are creating personal site on-demand and there might be a chance 'FormDigest' is not valid so it's always preferred to get current 'FormDigest'.
Final step is to queue 'personal site' creation.
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/sp.userprofiles.profileloader.getprofileloader/getuserprofile/createpersonalsiteenque(false)",
type: "POST",
async: false,
headers: { "ACCEPT": "application/json;odata=verbose", "X-RequestDigest": digest },
success: function (data) {
//Showing wait dialog for 8 sec, rough time given to create personal site.
setTimeout(function () { oPleaseWaitDialog.close(); }, 8000)
},
error: function (e) { oPleaseWaitDialog.close(); }
});
Overall function will look as follows:
var userHavePersonalSite = false;
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties?$select=PersonalUrl",
method: "GET",
async: false,
headers: {
"ACCEPT": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose"
},
success: function (data) {
//If user has personal site, set the variale to true
if (data && data.d && data.d.PersonalUrl && data.d.PersonalUrl != "" && data.d.PersonalUrl.indexOf("/personal/") != -1)
userHavePersonalSite = true;
},
error: function (data) { }
})
.done(function () {
//If user does not have personal site, adding request for creating 'Personal Site'. VMActivate.activateFollows
if (!userHavePersonalSite) {
var oPleaseWaitDialog = SP.UI.ModalDialog.showWaitScreenWithNoClose('Setting up things..', SP.Res.dialogLoading15);
var digest = "";
$.ajax({
url: "_api/contextinfo",
method: "POST",
async: false,
headers: {
"ACCEPT": "application/json;odata=verbose",
"content-type": "application/json;odata=verbose"
},
success: function (data) {
digest = data.d.GetContextWebInformation.FormDigestValue;
},
error: function (e) { oPleaseWaitDialog.close(); }
})
.done(function () {
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/sp.userprofiles.profileloader.getprofileloader/getuserprofile/createpersonalsiteenque(false)",
type: "POST",
async: false,
headers: { "ACCEPT": "application/json;odata=verbose", "X-RequestDigest": digest },
success: function (data) {
//Showing wait dialog for 8 sec, rough time given to create personal site.
setTimeout(function () { oPleaseWaitDialog.close(); }, 8000)
},
error: function (e) { oPleaseWaitDialog.close(); }
});
});
}
});
References
- Provision "Personal Site" on demand using JavaScript : AutoProvisionPersonalSite