Check if user was created an item in a list

Good day,
Can i on load form to verify if current user created an item in current list? if yes alert him, and close the form.

Hello @ixxxl,

You can check it on form load using the PnPjs library like this:

fd.spRendered(function(vue) {
  pnp.sp.web.lists.getByTitle('ListName').items.filter("AuthorId eq " + _spPageContextInfo.userId).get().then(function(items){
    if(items.length>0){
      alert('Alert Message');
      fd.close();
    }
  });
});
1 Like

Thank you ! i tried like this but tryin with displayName

@ixxxl,

It is better to check by user ID, as Display Name is not a unique property.

@mnikitina
Thank you.
In peoplepicker, when i use _spPageContextInfo.userEmail or _spPageContextInfo.userLoginName, sometimes it return another people with similar accounts.
For ex. Dima.rusu(display name Dima Rusu and dima.rusucovschi (display name Dima A. Rusucovschi) - when in form enter dima.rusu it show in people picker Dima A.Rusucovschi. it seems because of A in name it put first. Can i use here id?
For now i put_spPageContextInfo.DisplayName and it works

@ixxxl,

Do you mean updating the list item using PnPjs or populating the field on the form dynamically?

@mnikitina
It is populating on load form

@ixxxl,

As both users' account name starts with dima.rusu, there is a chance to select the incorrect user.

If you populate Person or Group field programmatically, it is better to use user email or login name.

@mnikitina
Yes i populate current user on form load
With this 2 users worked only -_spPageContextInfo.userDisplayName;
Email, and LoginName displays another user that entered in the form

@mnikitina
Good day!
How i can verify if item is in the list by user Modified?
Not by author.
EditorId i tried but it seems not work.
I need to restrict current user and current Title to modify more than one item.

@ixxxl,

This code should work to search for items by the user who changed the item:

  pnp.sp.web.lists.getByTitle('ListName').items.filter("AuthorId eq " + _spPageContextInfo.userId).get().then(function(items){
    console.log(items);
  });

@mnikitina

 pnp.sp.web.lists
    .getById("ListID")
    .items.filter(
      "EditorId eq " +
        _spPageContextInfo.userId +
        " and Title eq " +
        fd.field("Title").value
    )
    .get()
    .then(function (items) {
      console.log(items);
});

i 'm trying this one. is it correct filter? a need to check title and user modified.

@ixxxl,

Yes, the filter condition is correct, but you must replace ListID with the SharePoint List unique ID, or get list by its title getByTitle('Departments').

If the code doesn't work for you, please check teh browser console (F12) for the errors.

@mnikitina
There was some mistakes in filter. for now it works. Thank you.

fd.spRendered(function (vue) {
var editor = _spPageContextInfo.userId
var title = fd.field("Title").value
var filtering = "Statut eq 'Ocupat' and EditorId eq '"  + editor + "' and Title eq '" + title+ "'"
pnp.sp.web.lists
.getById("4129E849-5E22-439C-A575-0FEA45DEDB63")
.items.filter(filtering)
.get()
.then(function (items) {
  console.log(items);
  if (items.length > 0) {
        alert("Deja aveti programare: " + fd.field("Title").value);
        fd.close();
      }
});
});
1 Like