Solving the “User cannot be found” error with SPFile.Author

本文介绍了一个在自定义ASP.NET页面中显示SharePoint 2007列表项时遇到的问题,当尝试查看某个特定列表项的附件详情时,用户会收到“找不到用户”的错误。该文详细解释了错误的原因——即当附件的作者从Active Directory中移除后,SharePoint无法解析此用户。文章还提供了一个解决方案,通过直接从SPFile属性获取登录名来显示作者信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I ran into a bug today that was a bit unusual.  We have a custom ASP.NET page that shows information from a custom list in SharePoint 2007.  That page shows details, including the Author, for attachments.  On one particular list item, users started getting a “User cannot be found” error whey they tried to view it with our custom page.  The stack trace showed that it was getting the error in Microsoft.SharePoint.SPFile.get_Author().

It turns out that SPFile stores the user as a login name and tries to resolve the name using SPWeb.SiteUsers, which throws an error if the login name is not found.  The issue was, in this case, the attachment author had been removed from Active Directory and was no longer in SharePoint’s user list, hence the error.

The fix is to go directly to the SPFile properties and get the login name.  Since we can no longer resolve the login name to a user display name, we just display the login name.

string GetAuthorName(SPFile file)
{
    SPUser author = null;
    try { author = file.Author; } catch { }
    return (author == null) ? (string)file.Properties["vti_author"] : author.Name;
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值