views:

769

answers:

3

Here is the scenario. Our SharePoint custom job archives list items (based on ceratin crteria) and copies/moves the items to a different site collection. In this scenario, if the list item has some lookup fields, how do preserve these when I copy/mpve to different site collection?.

Thanks, Durga

A: 

The lookup field's internal name should stay the same, and as long as the list the lookup field is referencing is using the same values (ID's can be different, just match the value and create a new SPFIeldLookupValue), there is no problem. copy metadata based on field internal names and everything should go fine, we do the same for archiving news items and documents using timerjobs.

Colin
A: 

Thanks Colin. I would appreacite if you could elaborate on this. I am doing the following:

  1. I have lookup field in list in site collection A
  2. I copy the above item to a list in site collection B. The problem here is that this list does not have lookup field. So copy the lookup field first to the destination list and then copy the item as follows (partial code):
  3. if (f.InternalName == "lookup") { destinationList.Fields.Add(f); destinationList.Update(); targetItem[f.InternalName] = sourceItem[f.InternalName]; } }
  4. Once copied, when I try to access the lookup field value in the destination list using code, I get argument exception (value does not fall within the range). I think it is because the lookup field had hard coded list guid??. Also, I did not have any list an asociated list that lookup field refers to in site collection B
  5. I am missing something here. What is the right approach in this scenario?.
Durga
A: 

you can't "copy" a field from sitecoll A to sitecoll B, you have to recreate it. The usual way to go about this is to actually use a feature that creates the lookup field, but that's not the case here I guess (should have been done at the start, in the future i suggest that's how you create fields, seeing how that method is reusable).

What you would need to do is in the SPSite object of the b sitecoll create a new field, using the original Field's field.SchemaXml, this way you have all relevant info to recreate the field from scratch in the sitecoll b. you need to use the AddFieldAsXml of the SPSite.Fields collection if you want to set the internalname of the new field since the InternalName property is read only.

Read how here

CHeck if field exist:

 using(SPSite targetSite = new SPSite("urloftargetsite"))
 {
   using(SPWeb targetWeb = sourceSite.OpenWeb())
   {
     if(!targetWeb.Fields.ContainsField(originalField.InternalName))
     {
        targetWeb.Fields.AddFieldAsXml("caml string here");
     }
   }
 }
Colin
Thanks. When I looked at this field schema, it has old original list guid. I guess I have to replace this with the new list guid in xml before AddFieldAsXML. By the way, is specifying list guid is mandatory for the lookup fields?. I just want to store the lookup field value in the lookup field without reference list (sinnce this is an archive item).
Durga
If that is all just create a text field with the value in it. Make the internal name the same though for referencing purposes. Added some code to my answer, that can be used to check if a field exists)
Colin