How to retrieve the querystring in Sharepoint itemadded event
Please help.
I have two different lists from within one site.
In one list, I have appended a querystring to the new form which will represent the EMP_ID of the user. In here, I have a hidden field "Name" to represent the record filled out by the user. This "Name" field is referenced from the other table with the EMP_ID value.
The following is my code:
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 vitalityCollect开发者_JAVA百科ion = 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;
}
First, I don't think you'll be able to access a page's querystring parameters from within an event receiver. As a workaround, I would suggest populating an EMP_ID field in your SPListItem using JavaScript.
Second, wouldn't the "Created By" system field hold the user who created the record?
For example, putting this JavaScript on your EditForm.aspx to populate a field named 'Emp ID Field' from the EMP_ID parameter:
<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>
This is using SPUtility.js (full disclosure, this library is maintained by me).
Then, in your event receiver, you can access the EMP ID to lookup the correct name.
try
{
// same code...
SPListItem currentItem = properties.ListItem;
// **CHANGED** get the current employee's ID from the current SPListItem
string empId = currentItem["Emp ID Field"].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;
}
Some other things I noticed with your code...
- I think you might have problems trying to update
properties.ListItem
. I think you might need to updateAfterProperties
instead (take a look at this very handy reference). - You can make your lookup more efficient by not iterating over every item in the list. Use either an SPQuery object or SPList.GetItemById if EMP_ID is the auto-generated SharePoint ID.
For example:
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;
}
I think it is not possible, but you should try the ItemAdding event, which sync
精彩评论