Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>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.</p> <p><strike>Second, wouldn't the "Created By" system field hold the user who created the record?</strike></p> <p>For example, putting this JavaScript on your EditForm.aspx to populate a field named 'Emp ID Field' from the EMP_ID parameter:</p> <pre><code>&lt;script type="text/javascript" src="/path/to/prototype.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="/path/to/SPUtility.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; function SetValueFromURL(fieldName,queryParamName) { var queryParams = location.href.toQueryParams(); if (queryParams != null &amp;&amp; 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()); } }); &lt;/script&gt; </code></pre> <p>This is using <a href="http://sputility.codeplex.com/" rel="nofollow">SPUtility.js</a> (full disclosure, this library is maintained by me).</p> <p>Then, in your event receiver, you can access the EMP ID to lookup the correct name.</p> <pre><code>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; } </code></pre> <p>Some other things I noticed with your code...</p> <ol> <li>I think you might have problems trying to update <code>properties.ListItem</code>. I think you might need to update <code>AfterProperties</code> instead (<a href="http://www.synergyonline.com/blog/blog-moss/Lists/Posts/Post.aspx?List=fcb287b5-7a1a-4206-a04f-2a97805e4a25&amp;ID=25" rel="nofollow">take a look at this very handy reference</a>).</li> <li>You can make your lookup more efficient by not iterating over every item in the list. Use either an <a href="http://msdn.microsoft.com/en-us/library/ms457534.aspx" rel="nofollow">SPQuery</a> object or <a href="http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splist.getitembyid%28office.12%29.aspx" rel="nofollow">SPList.GetItemById</a> if EMP_ID is the auto-generated SharePoint ID.</li> </ol> <p>For example:</p> <pre><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 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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload