如何在 SharePoint 中使用 SPFx 显示当前已登录用户?

huangapple go评论94阅读模式
英文:

How to display current logged In User in SharePoint Using SPFx?

问题

我正在使用SPF制作SharePoint Web部件,用SPF来向当前登录的用户显示欢迎消息。我对SPF不熟悉,对JavaScript了解有限。

我已经尝试了这段代码,它显示属性列表。但我不确定如何只显示用户的名字。

private GetUserProperties(): void {
  pnp.sp.profiles.myProperties.get().then(function(result) {
    var userProperties = result.UserProfileProperties;
    var userName = ""; userProperties.forEach(function(property: { Key: 
      string; Value: string; }) {
      userName += property.Key + " - " + property.Value + "<br/>";
    });

    document.getElementById("FirstName").innerHTML = userName;
  })
  .catch(function(error) { console.log("Error: " + error); });
}
英文:

I am working on a SharePoint Web part using SPF to display welcome a message to current logged in user. I am new to SPF and with little knowledge in JavaScript.

I Have tried this code which displays the list of properties. But i am not sure how to only display the user first name.

private GetUserProperties(): void {
  pnp.sp.profiles.myProperties.get().then(function(result) {
    var userProperties = result.UserProfileProperties;
    var userName = &quot;&quot;; userProperties.forEach(function(property: { Key: 
      string; Value: string; }) {
      userName += property.Key + &quot; - &quot; + property.Value + &quot;&lt;br/&gt;&quot;;
    });

    document.getElementById(&quot;FirstName&quot;).innerHTML = userName;
  })
  .catch(function(error) { console.log(&quot;Error: &quot; + error); });
}

答案1

得分: 1

从SharePoint Framework应用程序的任何加载点(WebPart,ApplicationCustomizer等)中,您会得到一个基于BaseComponentContext的上下文对象,作为该类的私有成员。这个对象提供了许多信息,其中包括context.pageContext.user和以下属性:

{
  displayName: string
  email: string
  firstDayOfWeek: undefined
  isAnonymousGuestUser: boolean
  isExternalGuestUser: boolean
  loginName: string
  preferUserTimeZone: boolean
  timeZoneInfo: undefined
}

对于不同的用户,某些值可能不是未定义的。

这将是检索用户的完整显示名称的最简单方法,然后您可以将其转换为仅包含名字的方式。

然而,要继续您的示例,这只是简单地检索UserProfileProperties中的正确条目,这是一个包含 { Key: string, Value: string ValueType: 'Edm.String' } 的数组。所以,可以这样做:

const firstNameEntry = userProperties.filter(({ Key }) => Key === "FirstName")[0];
const firstName = firstNameEntry ? firstNameEntry[0].Value : "";
英文:

From any loading point in a SharePoint Framework app (WebPart, ApplicationCustomizer, ...) you get a context object derived from BaseComponentContext as a private member of the class. This object gives you many things, among which you have context.pageContext.user. and the following properties:

{
  displayName: string
  email: string
  firstDayOfWeek: undefined
  isAnonymousGuestUser: boolean
  isExternalGuestUser: boolean
  loginName: string
  preferUserTimeZone: boolean
  timeZoneInfo: undefined
}

Some values may not be undefined for different users.

This would be the simplest method to retrieve the user full display name, then you may transform for the first name only.

To continue on your example however, this is a simple matter of retrieving the correct entry in UserProfileProperties which is an array of { Key: string, Value: string ValueType: &#39;Edm.String&#39; }. So it would be:

const firstNameEntry = userProperties.filter(({ Key }) =&gt; Key === &quot;FirstName&quot;)[0]
const firstName = firstNameEntry ? firstNameEntry[0].Value : &quot;&quot;

huangapple
  • 本文由 发表于 2023年3月15日 18:57:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75743773.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定