none
Event Receiver Created Lookup Field Shows Result in the form of 1_.000

    问题

  • Hi,

    When I create a lookup field via an Event Receiver, using the below code.

                        string lookUp3 = CompaniesList.Fields.AddLookup("Countries", CountriesList.ID, false);
                        SPFieldLookup fieldLookup3 = (SPFieldLookup)CompaniesList.Fields.GetFieldByInternalName(lookUp3);
                        fieldLookup3.LookupField = Status.Fields["Name"].InternalName;
                        fieldLookup3.Indexed = true;
                        fieldLookup3.Update();


    In the "Companies" list when I use the drop down menu of the Lookup it displays the results in the format of 1_.000, where the result should be "Active", "Closed", "Branched. The Id field funny enough are retrieved correctly. The reason I use an event receiver for this is because I create the list in the same solution as well. I Tested it after deploy is successful if I can create a Lookup successfully through the SharePoint UI Using "Companies" and "Status" and there the lookup works correctly.

    2012年6月27日 14:20

答案

  • Hi Pieter,

    every list has built in fields. Four of them have the name “name” and are used for the attached file. The internal names are FileLeafRef, LinkFilenameNoMenu, LinkFilename, LinkFilename2.

    Your code:

    StatusList.Fields["Name"].InternalName

    returns the first of the five “name” fields in the status list, with the internal name FileLeafRef.

    You should select the field by the internal name.

    StatusList.Fields.GetFieldByInternalName("Name").InternalName
    2012年6月29日 10:10

全部回复

  • Hi PieterJVR,

    I tried to reproduce your issue, however, everything works well, the lookup field can be created successfully from the event receiver.

    The code snippet is:

    SPList list = web.Lists["list2"];

               SPList list1 = web.Lists["list1"];

               string lookup = list.Fields.AddLookup("LookUpFieldTest", list1.ID, false);

               SPFieldLookup lookupfield = (SPFieldLookup)list.Fields.GetFieldByInternalName(lookup);

               lookupfield.LookupField = list1.Fields["Title"].InternalName;

               lookupfield.Indexed = true;

               lookupfield.Update();

    the difference seems to be that your lookup list is named CompaniesList, then you use Status variable to define SPFieldLookup.LookupField value, please check it.

    Thanks,


    Qiao Wei

    TechNet Community Support

    2012年6月28日 8:21
  • Hi Qiao,

    My SPList Objects are declared like below.
    SPList CompaniesList = web.Lists.TryGetList(@"Companies");
    SPList Status = web.Lists.TryGetList(@"Status");

    I also seem to have left out a little information. In this feature I also create the "Companies" and "Status" lists prior to the FeatureActivated event, could you try it like this, you may get the same issue. And the reason I do it like this is because I use a Content Type, and when I want to create the lookup on the content type XML I am faced with the error "Exception: System.ArgumentException: <nativehr>0x80070057</nativehr><nativestack></nativestack>" on the "Feature Activating" event.

    Regards
    Pieter

    2012年6月28日 9:18
  • Hi Pieter,

    every list has built in fields. Four of them have the name “name” and are used for the attached file. The internal names are FileLeafRef, LinkFilenameNoMenu, LinkFilename, LinkFilename2.

    Your code:

    StatusList.Fields["Name"].InternalName

    returns the first of the five “name” fields in the status list, with the internal name FileLeafRef.

    You should select the field by the internal name.

    StatusList.Fields.GetFieldByInternalName("Name").InternalName
    2012年6月29日 10:10