Preset field value

Discussions about Forms Designer for SharePoint 2013 / 2016 and Office 365.
Locked
TKleinfeld
Posts: 5
Joined: Sun Sep 11, 2016

12 Sep 2016

I have this script to read out the manager oft the current user.

I know there is some stuff missing.

What do i have to change to make it work?



var userid = _spPageContextInfo.userId;
function GetCurrentUser() {
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties/";

var requestHeaders = { "accept" : "application/json;odata=verbose" };
$.ajax({
url : requestUri,
contentType : "application/json;odata=verbose",
headers : requestHeaders,
success : onSuccess,
error : onError
});
}
function onSuccess(data, request){
console.log(data);
var loginName = data.d.DisplayName;
var myManager = data.d.UserProfileProperties.results[15].Value;


SetUserFieldValue("pfer",loginName);
SetUserFieldValue("ri2o",myManager);

}
function onError(error) {
alert(error);
}
function SetUserFieldValue(fieldName, userName) {
var _PeoplePicker = $("div[title='" + fieldName + "']");
var _PeoplePickerTopId = _PeoplePicker.attr('id');
var _PeoplePickerEditer = $("input[title='" + fieldName + "']");
_PeoplePickerEditer.val(userName);
var _PeoplePickerOject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];
}
GetCurrentUser();



Thx for your help and best regards,

Tim

YuriyMedvedev
Moderator
Posts: 33
Joined: Wed Sep 21, 2016

12 Sep 2016

Try to add an attribute {"X-RequestDigest": $("#__REQUESTDIGEST").val()} to headers and everything should start working:


Code: Select all

var requestHeaders = {
 "accept" : "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
};
};

TKleinfeld
Posts: 5
Joined: Sun Sep 11, 2016

14 Sep 2016

should this look like this?:



var requestHeaders = {
"accept" : "application/json;odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
};

};


var userid = _spPageContextInfo.userId;
function GetCurrentUser() {
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties/";

var requestHeaders = { "accept" : "application/json;odata=verbose" };
$.ajax({
url : requestUri,
contentType : "application/json;odata=verbose",
headers : requestHeaders,
success : onSuccess,
error : onError
});
}
function onSuccess(data, request){
console.log(data);
var loginName = data.d.DisplayName;
var myManager = data.d.UserProfileProperties.results[15].Value;


SetUserFieldValue("pfer",loginName);
SetUserFieldValue("ri2o",myManager);

}
function onError(error) {
alert(error);
}
function SetUserFieldValue(fieldName, userName) {
var _PeoplePicker = $("div[title='" + fieldName + "']");
var _PeoplePickerTopId = _PeoplePicker.attr('id');
var _PeoplePickerEditer = $("input[title='" + fieldName + "']");
_PeoplePickerEditer.val(userName);
var _PeoplePickerOject = SPClientPeoplePicker.SPClientPeoplePickerDict[_PeoplePickerTopId];
}
GetCurrentUser();



sry i actually don't know that much about JS.

i tryed some different sequences with the header you mentioned.

But i only got syntax errors from the debugger.

Thx for your efforts!

TKleinfeld
Posts: 5
Joined: Sun Sep 11, 2016

14 Sep 2016

OK already solved the problem.

you can shorten it a bit and use standard commands from SPFD.

In the end it looked like this and worked.

but it requires a well maintained organisation plan in your site collection of sharepoint.



var userid = _spPageContextInfo.userId;
function GetCurrentUser() {
var requestUri = _spPageContextInfo.webAbsoluteUrl + "/_api/SP.UserProfiles.PeopleManager/GetMyProperties/";

var requestHeaders = { "accept" : "application/json;odata=verbose" };
$.ajax({
url : requestUri,
contentType : "application/json;odata=verbose",
headers : requestHeaders,
success : onSuccess,
error : onError
});
}
function onSuccess(data, request){
console.log(data);
var loginName = data.d.DisplayName;
var myManager = data.d.UserProfileProperties.results[15].Value;


SetUserFieldValue("pfer",loginName);
SetUserFieldValue("xwww",myManager);

}
function onError(error) {
alert(error);
}
function SetUserFieldValue(fieldName, userName) {

fd.field(fieldName).value(userName);

};

GetCurrentUser();

Locked
  • Information
  • Who is online

    Users browsing this forum: No registered users and 6 guests