英文:
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 = ""; 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); });
}
答案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: 'Edm.String' }
. So it would be:
const firstNameEntry = userProperties.filter(({ Key }) => Key === "FirstName")[0]
const firstName = firstNameEntry ? firstNameEntry[0].Value : ""
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论