在事件接收器中通过多种方式获取SPListItem的任何用途?

时间:2013-09-13 14:03:10

标签: c# sharepoint sharepoint-2007 wss-3.0

我正致力于在大约6年前开发的自定义事件接收器中支持代码......没有源代码,所以我们必须反映生产程序集,并且正在努力清理它,然后再开始工作。

在代码中,我遇到了这个,我不知道为什么会这样做,而且我不太了解SharePoint能够知道是否有必要以这种方式完成的原因,或者如果原始编码员是白痴(两者都有可能......)

public override void ItemAdded(SPItemEventProperties properties)
{
    Trace.WriteLine("ItemAdded() invoked.");
    base.DisableEventFiring();
    base.ItemAdded(properties);

    try
    {
        SPContext context = SPContext.GetContext(properties.OpenWeb());
        SPUserToken userToken = context.Site.SystemAccount.UserToken;

        using (SPSite site = new SPSite(context.Site.ID, userToken))
        using (SPWeb web = site.OpenWeb(context.Web.ID))
        {
            SPListItem listItem = null;
            try
            {
                listItem = web.GetListItem(properties.ListItem.Url);
                web.AllowUnsafeUpdates = true;
            }
            catch
            {
                Trace.WriteLine("No Url properties, we must be running in CLI mode. Exiting...");
            }
            if (listItem != null)
            {
                SPList parentList = listItem.ParentList;
                listItem = parentList.GetItemById(properties.ListItemId);
            }
            ...
        }
    }
    catch (Exception ex)
    {
        Trace.WriteLine(ex.ToString());
    }
    finally
    {
        base.EnableEventFiring();
    }
}

这似乎是通过多种方式获取SPListItem,当它在properties对象中传入时...同样的网站..所以,我想我的问题是因为它们最终只是通过列表中的属性获取List项目,是否真的有任何理由通过所有其他检查/方法来最终抛弃值?

1 个答案:

答案 0 :(得分:1)

不同之处在于它没有使用当前用户的凭据获取列表项;它正在使用系统帐户的凭据来抓取它。

系统帐户可能拥有当前用户没有的列表项权限。

也就是说,获取列表项目肯定是更简单的方法,并且没有理由这样做几次,即使在可能适当的情况下获得它也是如此。