C# 如何在Sharepoint itemadded事件中检索查询字符串

C# 如何在Sharepoint itemadded事件中检索查询字符串,c#,sharepoint,events,C#,Sharepoint,Events,请帮忙 我在一个站点中有两个不同的列表 在一个列表中,我在新表单中添加了一个查询字符串,它将表示用户的EMP_ID。在这里,我有一个隐藏字段“Name”来表示用户填写的记录。此“名称”字段从另一个具有EMP_ID值的表中引用 以下是我的代码: try { SPSite ohportal = new SPSite("http://moss2007dev:1234"); SPWeb site = ohportal.Ope

请帮忙

我在一个站点中有两个不同的列表

在一个列表中,我在新表单中添加了一个查询字符串,它将表示用户的EMP_ID。在这里,我有一个隐藏字段“Name”来表示用户填写的记录。此“名称”字段从另一个具有EMP_ID值的表中引用

以下是我的代码:

        try
        {
            SPSite ohportal = new SPSite("http://moss2007dev:1234");
            SPWeb site = ohportal.OpenWeb();
            SPList vitality = site.Lists[properties.ListId];
            SPList employees = site.Lists["Employees List"];
            SPListItemCollection vitalityCollection = vitality.Items;
            SPListItemCollection employeesCollection = employees.Items;

            SPListItem currentItem = properties.ListItem;

            string empId = HttpContext.Current.Request.QueryString["EMP_ID"].ToString();
            foreach(SPListItem item in employeesCollection)
            {
                if(item["EMP_ID"].Equals(empId)){
                    string name = item["Name"].ToString();
                    currentItem["Name"] = name;
                }
            }
        }
        catch (Exception err)
        {
            properties.ErrorMessage = "ERROR: " + err.Message;
        }

我认为这是不可能的,但您应该尝试ItemAdding事件,该事件首先同步,我认为您无法从事件接收器中访问页面的查询字符串参数。作为一种解决方法,我建议使用JavaScript在SPListItem中填充EMP_ID字段

第二,“Created By”系统字段不包含创建记录的用户吗

例如,将此JavaScript放在EditForm.aspx上,以从Emp_ID参数填充名为“Emp ID field”的字段:

<script type="text/javascript" src="/path/to/prototype.js"></script>
<script type="text/javascript" src="/path/to/SPUtility.js"></script>
<script type="text/javascript">

function SetValueFromURL(fieldName,queryParamName) {
    var queryParams = location.href.toQueryParams();
    if (queryParams != null && queryParams[queryParamName] != null) {
        SPUtility.GetSPField(fieldName).SetValue(decodeURI(queryParams[queryParamName]));
    }
}

Event.observe(window,'load',function(){
    try {

        // TODO: Put your code here
        SetValueFromURL('Emp ID Field', 'EMP_ID');

    } catch (ex) {
        alert(ex.toString());
    }
});

</script>
我注意到你的代码中还有其他一些东西

  • 我认为您在尝试更新
    属性.ListItem
    时可能会遇到问题。我认为您可能需要更新
    AfterProperties
    而不是()
  • 通过不迭代列表中的每个项目,可以提高查找效率。使用对象或EMP_ID是自动生成的SharePoint ID
  • 例如:

    try
    {
        SPSite ohportal = new SPSite("http://moss2007dev:1234");
        SPWeb site = ohportal.OpenWeb();
        SPList vitality = site.Lists[properties.ListId];
        SPList employees = site.Lists["Employees List"];
        SPListItemCollection vitalityCollection = vitality.Items;
        SPListItemCollection employeesCollection = employees.Items;
    
        SPListItem currentItem = properties.ListItem;
    
        // lookup the employees name
        string empId = currentItem["EMP_ID"].ToString();
        SPListItem employee = list.GetItemById(Int32.Parse(empId));
        properties.AfterProperties["Name"] = employee["Name"].ToString();
    }
    catch (Exception err)
    {
        properties.ErrorMessage = "ERROR: " + err.Message;
    }
    

    我还调用了ItemAdding事件中的HttpContext,但仍然返回null。由于事件接收器没有在http requestHi Kit的上下文中运行,因此在这种情况下,“Created By”列是无用的,因为只有一个用户创建该项,即医生。所以,我需要的是有一个特定项所属的标识符,在这里可以进行过滤。我会研究上述方案。感谢您的快速回复。:)你好,Kit,在上面的代码中,我如何参考同一站点中的另一个列表来确定我的EMP_ID状况?Jane,我想我的答案中可能没有包含足够的细节(已经更新,请查看)。基本上,您可以使用JavaScript填充字段,以便在事件接收器中引用它(因为您无法访问查询字符串)
    try
    {
        SPSite ohportal = new SPSite("http://moss2007dev:1234");
        SPWeb site = ohportal.OpenWeb();
        SPList vitality = site.Lists[properties.ListId];
        SPList employees = site.Lists["Employees List"];
        SPListItemCollection vitalityCollection = vitality.Items;
        SPListItemCollection employeesCollection = employees.Items;
    
        SPListItem currentItem = properties.ListItem;
    
        // lookup the employees name
        string empId = currentItem["EMP_ID"].ToString();
        SPListItem employee = list.GetItemById(Int32.Parse(empId));
        properties.AfterProperties["Name"] = employee["Name"].ToString();
    }
    catch (Exception err)
    {
        properties.ErrorMessage = "ERROR: " + err.Message;
    }