1

I'm having trouble getting the value of a look up field after a list item is submitted.

The XML for the look up field looks like this:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/"> <Field ID="{6C798145-A205-4FC4-8175-837B0F7744CC}" Type="Lookup" SourceID="http://schemas.microsoft.com/sharepoint/v3" DisplayName="Work Project" Required="True" List="Lists/USNITWorkManagementProjectList" ShowField="ProjectName" UnlimitedLengthInDocumentLibrary="TRUE" Group="USNITWorkManagement" Name="WorkProject" /> </Elements>

Both of these return null:

properties.AfterProperties["WorkProject"].ToString()

properties.AfterProperties["ProjectName"].ToString()

I'm new to this, so I'm sure I'm doing something simple wrong. Any help is appreciated!

1 Answer 1

-1

Since the fields are lookup types, use SPFieldLookupValue to get the value in the fields:

SPFieldLookupValue workProject = properties.AfterProperties["WorkProject"] as SPFieldLookupValue;
var workProjectValue = workProject.LookupValue
3
  • Thanks for the response, but I'm still getting a null error on workProject.LookupValue. I tried "WorkProject" and the look up name "ProjectName", same result for both. Any ideas? Thanks again!
    – Lin M
    Commented Mar 6, 2015 at 14:47
  • Check if the field contains any value using convert.tostring. Moreover, you need to use the internal name of the lookup field which is WorkProject Commented Mar 6, 2015 at 15:21
  • 1
    A value of a lookupfield in before/afterproperties is just a string. Only when you have the listitem you will get SPField like types. Commented Jun 8, 2017 at 13:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.