SPFx 如何在列表中设置人员字段,例如将 AccountName“转换”为查找 ID?
Posted
技术标签:
【中文标题】SPFx 如何在列表中设置人员字段,例如将 AccountName“转换”为查找 ID?【英文标题】:SPFx how to set a people field in a list, e.g. "convert" AccountName to Lookup ID? 【发布时间】:2017-12-20 07:26:03 【问题描述】:我正在尝试使用 React Office UI Fabric 构建一个 SPFX WebPart。我有一个 SharePoint 列表,其中包含一个名为 AssignedTo
的人员字段。 ID 值如 12、20 等。当我使用 PeoplePicker 选择一个人时,我会得到 AccountName
(如 i:0#.f|membership|bob@somewhere.org.nz
)和一个 GUID(如 ff53b41e-d11a-435c-9cd1-d708e71ee7c7
)。
如何将 PeoplePicker 中的信息“转换”为 ID,列表中的 People 字段正在使用?
PeoplePicker 检索人员如下:
private searchPeople(terms: string, results: IPersonaProps[]): IPersonaProps[] | Promise<IPersonaProps[]>
return new Promise<IPersonaProps[]>((resolve, reject) =>
this.props.spHttpClient.get(`$this.props.siteUrl/_api/search/query?querytext='*$terms*'&rowlimit=20&sourceid='b09a7990-05ea-4af9-81ef-edfab16c4e31'`,
SPHttpClient.configurations.v1,
headers:
'Accept': 'application/json;odata=nometadata',
'odata-version': ''
)
.then((response: SPHttpClientResponse): Promise< PrimaryQueryResult: IPeopleDataResult > =>
return response.json();
)
.then((response: PrimaryQueryResult: IPeopleDataResult ): void =>
let relevantResults: any = response.PrimaryQueryResult.RelevantResults;
let resultCount: number = relevantResults.TotalRows;
let people = [];
if (resultCount > 0)
relevantResults.Table.Rows.forEach( (row) =>
let persona: IPersonaProps = ;
row.Cells.forEach( (cell) =>
if (cell.Key === 'PictureURL')
persona.imageUrl = cell.Value;
else if (cell.Key === 'PreferredName')
persona.primaryText = cell.Value;
persona.imageInitials = cell.Value.charAt(0) + (cell.Value.lastIndexOf(" ") > 0 ? cell.Value.charAt(cell.Value.lastIndexOf(" ") + 1) : "");
else if (cell.Key === 'AccountName')
persona.itemID = cell.Value; // e.g.: "i:0#.f|membership|bob@somewhere.org.nz"
else if (cell.Key === 'UserProfile_GUID')
persona.itemID = cell.Value; // e.g.: "ff53b41e-d11a-435c-9cd1-d708e71ee7c7"
);
people.push(persona);
);
resolve(people);
, (error: any): void =>
reject(this._peopleList = []);
));
我得到的列表项如下:
private _getItems(requester: SPHttpClient): Promise<IInteractionLogItem[]>
const queryString: string = `?$select=Id,Title,Client/Id,Client/Title,Client/EMail,Client/MobilePhone,` +
`$expand=Client`;
const queryUrl: string = this._listItemsUrl + queryString;
return requester.get(queryUrl, SPHttpClient.configurations.v1)
.then((response: SPHttpClientResponse) =>
return response.json();
)
.then((json: value: IInteractionLogItem[] ) =>
return json.value.map((interactionLog: IInteractionLogItem) =>
const returnItem: IInteractionLogItem =
Id: interactionLog.Id,
Title: interactionLog.Title,
Client: interactionLog.Client,
;
returnItem.CreatedBy.Picture = this._getPictureUrl(returnItem.CreatedBy.EMail);
if( returnItem.Client )
returnItem.Client.Picture = this._getPictureUrl(returnItem.Client.EMail);
return returnItem;
);
);
非常感谢任何帮助:-)
【问题讨论】:
【参考方案1】:需要使用 SPHttpClient 调用 api/web/ensureuser
。它确保将请求的用户加载到 SharePoint 站点用户信息中。使用给定的AccountName
(如搜索中的i:0#.f|membership|bob@somewhere.org.nz
)作为参数。返回的 SPUser 包含 Lookup 列的 Id
字段。我实现了以下方法:
public EnsureUser(userName: string): Promise<ISPUser>
console.log("SharePointDataProvider.EnsureUser( \"" + userName + "\" )");
var data = logonName: userName;
return this._webPartContext.spHttpClient.post(
this._ensureUserUrl,
SPHttpClient.configurations.v1,
body: JSON.stringify(data) ).then(
(value: SPHttpClientResponse) =>
console.log("SharePointDataProvider.EnsureUser FullFill: statusText:\"" + value.statusText + "\"" );
return value.json();
,
(error: any) => console.log("SharePointDataProvider.EnsureUser Rejected: " + error )
).then((json: ISPUser) =>
console.log("SharePointDataProvider.EnsureUser FullFill: Id:" + json.Id +" LoginName:\"" + json.LoginName + "\"" );
return json;
);
我的迷你 ISP 用户界面:
interface ISPUser
Email: string;
Id: number;
LoginName: string;
Title: string;
【讨论】:
【参考方案2】:如果你看到类似的东西
id:i:0#.f|membership|bob@somewhere.org.nz
但想要身份证号码
id:18
添加
ensureUser=true
到你的 peoplePicker 组件
【讨论】:
太棒了!很高兴我能帮上忙以上是关于SPFx 如何在列表中设置人员字段,例如将 AccountName“转换”为查找 ID?的主要内容,如果未能解决你的问题,请参考以下文章
如何在SharePoint 2010中设置SPField的内部名称?