SharePoint List Items with Lookup Fields - moving/copying to different site collection
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
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.
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");
}
}
}
精彩评论