0

I have made one webpart using spfx, on submit button its values are pushing in SP list but in SP list I want user who is currently logged in, its value should come in one of the field of Sp list, how to do in spfx.

  • If I understand your question right, you want to view Display name of current user when you click on button – Melad Batta Feb 19 '18 at 10:33
  • @Melad, yes I want to display its name in one of field of SP list – Deeksha Gupta Feb 19 '18 at 11:26
0

As explained in the community here, you could retrieve the current user information before pushing your data into the your list.

0

You can retrieve current user information as below:

console.log(this.context.pageContext.user.displayName);
console.log(this.context.pageContext.user.loginName);

Here this is a web part context.

  • the above commands works when the field which stores current user name in SP list is of single line of text but when I am making it person or group type then value is not coming. – Deeksha Gupta Feb 20 '18 at 11:46
0

this solution may help you to get Display name of current user when you click on button

public _getCurrentUser(): void{
if (Environment.type == EnvironmentType.SharePoint || Environment.type == EnvironmentType.ClassicSharePoint) {
  console.log(this.context.pageContext.user.displayName);
  let User = this.context.pageContext.user.displayName;
  let html: string = '';
  html = `
    <ul class="${styles.list}">
      <li class="${styles.listItem}">
        <span class="ms-font-l">${User}</span>
      </li>
    </ul>`;
  const listContainer: Element = this.domElement.querySelector('#SPList');
  listContainer.innerHTML = html;
} }
 private _setButtonEventHandlers(): void {
const webPart: UserClickWebPart = this;
this.domElement.querySelector('#GetUser').addEventListener('click', () => { this._getCurrentUser(); });}
public render(): void {
this.domElement.innerHTML = `
  <div class="${ styles.userClick }">
    <div class="${ styles.container }">
      <div class="${ styles.row }">
        <div class="${ styles.column }">
          <span class="${ styles.title }">Welcome to SharePoint!</span>
          <p class="${ styles.subTitle }">Customize SharePoint experiences using Web Parts.</p>
          <p class="${ styles.description }">${escape(this.properties.description)}</p>
          <button class="${styles.button}" id="GetUser">
          <span class="${styles.label}">CurrentUser</span>
        </button>
        </div>
        <div id="Global" class="${styles.container}">
        <div id="SPList"></div>
      </div>
      </div>
    </div>
  </div>`;
this._setButtonEventHandlers();}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.