Question

I have an issue with setting the field in item updating event in document library with SharePoint 2007.

I have following situation, I have a document library, and when i add a document based on one LookUpField i want to add values to another lookup field with picker, but it fails.

But if i have a document already in the document library and in properties edit i modify the item and i update it, then it does update the field.

I've notice some inconsistency in this, namely when i update i pass to a field this kind of data: 58;#SomeValue;#59;#SomeValue2;#60;#SomeValue3 ...

But when i add a document in in the template i fill in the lookup field i get the following stuff to pass to another lookup field with picker: 58;#;59;#;#60;#

So it means it passes ID's of it but the values are empty, but it seems i need to have values too, to make it work.

I h ave the following code:

public override void ItemUpdating(SPItemEventProperties properties)
        {
            base.DisableEventFiring();
            base.ItemUpdating(properties);

            string nameLookUp = "LookUpField";
            string namePickUp = "LookUpFieldWithPicker";


            string opp = properties.BeforeProperties[namePickUp]==null ? string.Empty : properties.BeforeProperties[namePickUp].ToString();
            string npp = properties.AfterProperties[namePickUp] == null ? string.Empty : properties.AfterProperties[namePickUp].ToString();

            string op = properties.BeforeProperties[nameLookUp] == null ? string.Empty : properties.BeforeProperties[nameLookUp].ToString();
            string np = properties.AfterProperties[nameLookUp] == null ? string.Empty : properties.AfterProperties[nameLookUp].ToString();

            if (!string.IsNullOrEmpty(npp) && opp != npp)
            {
                SPFieldLookupValueCollection value = new SPFieldLookupValueCollection(properties.AfterProperties[namePickUp].ToString());
                properties.AfterProperties[nameLookUp] = properties.AfterProperties[namePickUp];
            }
            else if (!string.IsNullOrEmpty(np) && op != np)
            {
                 properties.AfterProperties[namePickUp] = properties.AfterProperties[nameLookUp];
            }

            base.EnableEventFiring();
        }

I hope anyone can advise me something ... thank you

Was it helpful?

Solution

Take reference to lookup list and use List.GetItemById(Id) to get the item and take the column value and construct the lookup collection. Example: SPList lookupList=properties.web.lists["lookupListName"]; SPListItem lookuplistitem=lookupList.GetItemById(4); //Here Id is 4 string filedvalue=lookuplistitem["lookupfieldname"];//Which field you are taking as lookup now construct the fieldlookup and do the same for all id's and construct fieldlookupvaluecollection.

List.GetItemById(Id) query is faster so you will not get much performance issues.

OTHER TIPS

As per my understanding there are two lookups from same list/library. SPFieldLookupValueCollection for each field is different. The format is ID;#Value here ID is listitem ID and Value is what you stored for respective fields in lookup list.

For "LookUpField" "ID;#LookUpFieldValue"

For "LookUpFieldWithPicker" "ID;#LookUpFieldWithPickerValue"

Before updating the listitem you have to construct the lookup collection like this

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top