Populating user profile properties (Sharepoint 2019)

Hi,
I'm testing Forms for Sharepoint 2019 before we buy it. We do love the potential it has, but also meet with some troubles.
One of the most important functions for us is possibility to populate current user profile properties to fields in the form and also get some information from associated lookup list.
I just do not know how to get these fields and make them visible on form just when it is rendered (onload) - like it works in OnLoad in InfoPath.
I have found nice example Retrieve user profile information — SharePoint forms, but it doesn't work. I'm not sure where to place it in code and how to launch.
I'm a bit advanced in Sharepoint Server, but absolute begginer in javascript.
In this script:
function updateCurrentUserInfo() { .... code from website...}

fd.rendered(function() {
updateCurrentUserInfo();
fd.field('Text1').value = _spPageContextInfo.userDisplayName;
});
Text1 field assignment works great but defined function updateCurrentUserInfo() does not show any effect.
I have checked against the error in Console (in dev. tools in browser) and found a lot of red errors like below:
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
spform.js:30 Uncaught (in promise) Error: Error making HttpClient request in queryable [500] Internal Server Error ::>

Can you somehow show me how to achieve the goal in Sharepoint 2019 and Plumsail Form?

Regards
Marcin

Dear @Marcin,
Which code exactly are you using from the article? There are different samples there.

I tried to use updateCurrentUserInfo() function (as example above) from part "Accessing current user profile properties".
I have implemented this withe code below in JS Section of Form Designer:

function updateCurrentUserInfo() { .... here long code from website...}

fd.rendered(function() {
updateCurrentUserInfo(); //here I call the function and later another something
fd.field('Text1').value = _spPageContextInfo.userDisplayName;
});

Dear @Marcin,
How can we help, if we don't know what code you're running here?

function updateCurrentUserInfo() { .... here long code from website...}

I beg your pardon...
I just didn't want to copy paste full code you can find on your own website.
But of course it is not a problem:
function updateCurrentUserInfo() {
pnp.sp.profiles.myProperties.get().then(function(result) {

        var props = result.UserProfileProperties;

        for (var i = 0; i < props.length; i++) {

            switch (props[i].Key) {
                case 'Manager':
                    fd.field('Manager').value = props[i].Value;
                    break;

                case 'Department':
                    fd.field('Department').value = props[i].Value;
                    break;

                case 'Title':
                    fd.field('JobTitle').value = props[i].Value;
                    break;

                case 'CellPhone':
                    fd.field('Mobile').value = props[i].Value;
            }
        }
    });
}

fd.spRendered(function() {
    updateCurrentUserInfo();
});

Lets assume we have those fields in list and form.

Dear @Marcin,
This particular code should work, though, it might depend on the site's settings. Please, add the following line to the top of JS editor on the form:
window.pnp = pnp;

Then, go to the form, open browser's console and enter the following code + press Enter:

pnp.sp.profiles.myProperties.get().then(function(result) {
  console.log(result);
});

Send us a screenshot of the result. It will likely result in the same error, but we need to check.

Please find result below:

Promise {<pending>}
spform.js:16          GET http://plwaws73/sites/wnioskizakupowe/_api/sp.userprofiles.peoplemanager/getmyproperties 500 (Internal Server Error)
e.fetch @ spform.js:16
i @ spform.js:60
(anonymous) @ spform.js:60
e.fetchRaw @ spform.js:60
(anonymous) @ spform.js:60
(anonymous) @ spform.js:16
(anonymous) @ spform.js:16
(anonymous) @ spform.js:16
a @ spform.js:16
e.fetch @ spform.js:60
(anonymous) @ spform.js:30
e.send @ spform.js:30
o.value @ spform.js:30
I @ spform.js:30
(anonymous) @ spform.js:30
Promise.then (async)
o.value @ spform.js:30
I @ spform.js:30
(anonymous) @ spform.js:30
Promise.then (async)
o.value @ spform.js:30
I @ spform.js:30
M @ spform.js:30
(anonymous) @ spform.js:30
(anonymous) @ spform.js:30
(anonymous) @ spform.js:16
(anonymous) @ spform.js:16
a @ spform.js:16
Promise.then (async)
c @ spform.js:16
(anonymous) @ spform.js:16
a @ spform.js:16
(anonymous) @ spform.js:30
l @ spform.js:30
t.get @ spform.js:30
(anonymous) @ VM614:1
spform.js:30 Uncaught (in promise) Error: Error making HttpClient request in queryable [500] Internal Server Error ::> {"odata.error":{"code":"-1, Microsoft.SharePoint.Client.UnknownError","message":{"lang":"pl-PL","value":"Nieznany b\u0142\u0105d"},"innererror":{"message":"Object reference not set to an instance of an object.","type":"System.NullReferenceException","stacktrace":"   at Microsoft.Office.Server.UserProfiles.PersonProperties.get_PersonalUrl()\r\n   at Microsoft.Office.Server.UserProfiles.PersonPropertiesServerStub.GetProperty(Object target, String propName, ProxyContext proxyContext)\r\n   at Microsoft.SharePoint.Client.ServerStub.<GetClientServiceODataScalarProperties>d__66.MoveNext()\r\n   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()\r\n   at Microsoft.Data.OData.JsonLight.ODataJsonLightPropertySerializer.WriteProperties(IEdmStructuredType owningType, IEnumerable`1 properties, Boolean isComplexValue, DuplicatePropertyNamesChecker duplicatePropertyNamesChecker, ProjectedPropertiesAnnotation projectedProperties)\r\n   at Microsoft.Data.OData.JsonLight.ODataJsonLightWriter.EndEntry(ODataEntry entry)\r\n   at Microsoft.Data.OData.ODataWriterCore.<WriteEndImplementation>b__16()\r\n   at Microsoft.Data.OData.ODataWriterCore.InterceptException(Action action)\r\n   at Microsoft.Data.OData.ODataWriterCore.WriteEnd()\r\n   at Microsoft.SharePoint.Client.ServerStub.WriteAsEntity(Object value, Uri path, ODataWriter writer, RESTfulQuery query, ProxyContext proxyContext)\r\n   at Microsoft.SharePoint.Client.ServerStub.WriteAsEntityWithMonitoredScope(Object value, Uri path, ODataWriter writer, RESTfulQuery query, ProxyContext proxyContext, MonitoredScopeType scopeType)\r\n   at Microsoft.SharePoint.Client.ServerStub.Write(Object value, Uri path, ODataWriter writer, RESTfulQuery query, ProxyContext proxyContext)\r\n   at Microsoft.SharePoint.Client.Rest.RestRequestProcessor.Process()\r\n   at Microsoft.SharePoint.Client.Rest.RestRequestProcessor.ProcessRequest()\r\n   at Microsoft.SharePoint.Client.Rest.RestService.ProcessQuery(ClientServiceHost serviceHost, Stream inputStream, IList`1 pendingDisposableContainer)"}}}
    at new t (spform.js:30:5435)
    at Function.<anonymous> (spform.js:30:5706)
    at spform.js:30:1662
    at Object.next (spform.js:30:1767)
    at a (spform.js:30:5881)

Dear @Marcin,
Tested it in SharePoint 2019, it should work.

You might need to configure User Profile Service for this to work properly - Configuring the User Profile Service in SharePoint 2010 | SharePoint George, A GKM2 Solutions Blog

More about the same issue here - Calling User Profile rest api in https site returns Internal Server Error.

OK.
I have switched to another Sharepoint where this API works great.
So the result from comsole is now:
image

Having this code in JSEditor:
window.pnp = pnp;

function updateCurrentUserInfo() {
    pnp.sp.profiles.myProperties.get().then(function(result) {
        
        var props = result.UserProfileProperties;

        for (var i = 0; i < props.length; i++) {

            switch (props[i].Key) {
                case 'Manager':
                    fd.field('Przelozony').value = props[i].Value;
                    break;

                case 'Department':
                    fd.field('Ksiegowe').value = props[i].Value;
                    break;

                case 'SAPNumber':
                    fd.field('SAPNumber').value = props[i].Value;
                    break;
            }
        }
    });
}

fd.rendered(function() {
        updateCurrentUserInfo(); //here I call the function and later another something
});

my fields are still blank:

I'm sure there is probably some basic mistake I make.

Dear @Marcin,
I think the issue is the event, it should be spRendered() here:

fd.spRendered(function() {
        updateCurrentUserInfo(); //here I call the function and later another something
});

O yeah! It is much better.
But when script reads another property I receve new issue "Uncaught (in promise) TypeError: Cannot set properties of undefined (setting 'value')":

Both custom property and field are strings. They are of the same type. Like Job Title or Department from example on your website.

Dear @Marcin,
Here, the issue is the field, it's not defined. Maybe the Internal Name is different, not SAPNumber?

Thank You Nikita.
You were right. I have completely forgotten about Internal Name of the field. I think this is common or often for all of us leaving InfoPath.
I'm sure I will have another questions, but first I take my chances to migrate first Form to test env.
Regards
Marcin

1 Like