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?
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;
});
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:
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)
OK.
I have switched to another Sharepoint where this API works great.
So the result from comsole is now:
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
});
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')":
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