Populate Created By with Current user

Hello,

Can you please help me with a small task. I need to populate on form open the SharePoint field CreatedBy with the name of the user who opened the form.

Thank you.

Hello @Vasilii_Burca,

The CreatedBy field cannot be changed.

You can display the name of the current user in the form and display the name of the user who created the item.

What exactly do you want to design?

Hello @Margo,

I want to to populate a people picker field with the username of the user who currently opened the form.
It does not have to be the CreatedBy field.

Thanks.

I use the following:
fd.field('PeoplePickerField').value = _spPageContextInfo.userLoginName;

Hello @Vasilii_Burca,

The code provided by @stormanh is correct. Just don't forget to use it under spRendered event,

fd.spRendered(function() {
    fd.field('PeoplePickerField').value = _spPageContextInfo.userLoginName; 
});

@stormanh, thank you for help!

Hello @stormanh and @Margo,

I tried this code:

fd.spRendered(function() {
    fd.field('PeoplePickerField').value = _spPageContextInfo.userLoginName; 
});

and it does not work.

Tried from Documentation to populate current date and it works like a charm.

    fd.spRendered(function(vue) {
        fd.field('Created').value = new Date();
});

Can you please tell me what I am dooing wrong. I am a complete beginner.

Thank You !

@Vasilii_Burca Make sure that you are referencing the internal name of the SharePoint field for the 'PeoplePickerField' field as shown in this screencap in Forms Designer.

replace: fd.field('PeoplePickerField').value with fd.field('InternalName').value

From the screencapture above (demonstrative purposes only) it would be:

fd.field('Author').value

Therefore always reference/use the InternalName of your SharePoint Field when coding in the JS editor. Note: the screencapture provided in this example is for your visual reference only to show you where you can find the InternalName of the SharePoint Field in Forms Designer (you cannot change the default "Created By" SharePoint Field that has the InternalName of "Author" because this is a built-in/standard SharePoint field that is not editable).

1 Like

Thank you a lot for your help @stormanh and @Margo,

The problem was that I was testing the form logged in as the System Account, so it didn’t work. After logging in as a plain user everything worked like a charm.

I have one more small question is there a list or perhaps a way to lookup other SharePoint attributes to prepopulate other fields with information?

Example :

_spPageContextInfo.userLoginName; this is for current user;
_spPageContextInfo.department ? attribute to populate user department attribute;

Thank You very much and have a great day and weekend.

Hello @Vasilii_Burca,

Please find the code example in Populate fields with profile information article.

The code in the article is for the People Picker field, to get the current user profile information you need to change the code slightly:

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;

            }
        }
    });

Hello @Margo,

I did exactly like you suggested but it does not work.

Please see screenshot.

Do you need to have my sites enabled in order this to work ?

@Vasilii_Burca,

I'm sorry, I should have been more specific.

This is just a part of the code, the one that should be changed if you want to populate fields with the profile information of the current user, not the one selected in the People Picker field. The complete code is below.

Please don't forget to use the internal names of the fields in the code.

function updateUserInfo() {

        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;

            }
        }
    });
}

fd.spRendered(function() {

    //executes updateUserInfo on form load
    updateUserInfo();
});

Hello @Margo,

Here is the entire code from the form still those two fields are empty.

function updateUserInfo() {

    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 'Office':
                fd.field('Numarul_x0020_de_x0020_tabel').value = props[i].Value;
                break;

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

        }
    }
});

}

fd.spRendered(function() {

//executes updateUserInfo on form load
updateUserInfo();

//set button's text
fd.toolbar.buttons[0].text = "Submit";

//set button`s icon from https://developer.microsoft.com/en-us/fabric#/styles/web/icons
fd.toolbar.buttons[0].icon = "Add";

//Add current user to form field
fd.field('Utilizator').value = _spPageContextInfo.userLoginName;

});

Hello @Margo,

Using Developer tools I came across an error in SharePoint API.
The problem was that _api/SP.UserProfiles.PeopleManager/GetMyProperties

Gave me an Unknown Error.

After Extensive search on google, the solution was this

Now everything works as expected.

Another solution will be My Sites deployment.

Thank you @Margo and @stormanh for your help !!!

1 Like