Preset field value

rss

Posted by TKleinfeld - Sep 12 ’16 at 01:59

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

  • Yuriy Medvedev
    Yuriy Medvedev
    Moderator
    Member for: 3 years 3 months 13 days
    #1 by Yuriy Medvedev Sep 12 ’16 at 10:57

    The message was removed by a forum moderator.

  • Yuriy Medvedev
    Yuriy Medvedev
    Moderator
    Member for: 3 years 3 months 13 days
    #2 by Yuriy Medvedev Sep 12 ’16 at 10:59

    The message was removed by a forum moderator.

  • Yuriy Medvedev
    Yuriy Medvedev
    Moderator
    Member for: 3 years 3 months 13 days
    #3 by Yuriy Medvedev Sep 12 ’16 at 10:59

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

     

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

    };

     

  • TKleinfeld
    Member
    Member for: 3 years 2 months 30 days
    #4 by TKleinfeld Sep 14 ’16 at 02:33

    [#3]:  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
    Member
    Member for: 3 years 2 months 30 days
    #5 by TKleinfeld Sep 14 ’16 at 03:56

    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();

Displaying 1 to 5 of 5 messages