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.

2
  • If I understand your question right, you want to view Display name of current user when you click on button Commented Feb 19, 2018 at 10:33
  • @Melad, yes I want to display its name in one of field of SP list Commented Feb 19, 2018 at 11:26

4 Answers 4

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.

1
  • 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. Commented Feb 20, 2018 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();}
0

If I understand your question correctly, you want to set the value of a field in a list item to be the current user.

You update a Person or Group field the same way you update a lookup field. That is, you set the value of the field to the Id of target item. In the case of a Person or Group field, that's the Id of the user.

You can get the Id of the current user using the userId property legacy page context, which is the same information that's included in the _spPageContextInfo object in classic pages. In an SPFx web part, you can get the Id of the current user like this this.context.pageContext.legacyPageContext.userId.

Once you have the Id, you can then write code like this to add a list item that populates the Person or Group field value. In the sample below, the internal name of the Person or Group field is Person.

private addItemButtonClickHandler(): void {
  let restUrl = this.context.pageContext.web.absoluteUrl +
    "/_api/web/lists/getbytitle('CurrentUserSpfx')/items";
    
  let request: any = {};
  request.body = JSON.stringify({
    Title: "Item " + new Date().toUTCString(),
    PersonId: this.context.pageContext.legacyPageContext.userId
  });

  this.context.spHttpClient.post(restUrl, SPHttpClient.configurations.v1, request)
    .then((response) => {
      response.text().then((data) => {
        let addItemResultDiv = this.domElement.querySelector("#addItemResultDiv");
        addItemResultDiv.innerHTML += data;
      });
    });
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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