Skip to main content

Copy lookup fields in SharePoint 2010

I had a scenario where I had to copy items to different sites in the same site collection. I got problems when I tried to copy an item with a lookup field. It copies ok but the lookup was all wrong since they pointed to the item with the same id as the source witch was not always the same item in the look up list. Then I got the idea of querying the look up list at the target site to see if it had a field with the same value as the source site. Using this I made the lookup field point to that field in the item and now it was able to point to the right item.

SPFieldLookup lookupField = (SPFieldLookup)field;

string lookUpListName = contextWeb.Lists[new Guid(lookupField.LookupList)].Title;
string lookUpFieldName = lookupField.LookupField;


// Get the target list on the copy to web.
SPList copyToListLookUp = copyToList.ParentWeb.Lists[lookUpListName];

SPField lookUpTargetField = copyToListLookUp.Fields.GetFieldByInternalName(lookUpFieldName);
// Lookup fields store there data as "FieldID;#FieldValue" we only need the FieldValue
int fieldValuePart = 1;
string fieldValue = copyFromItem[field.InternalName].ToString().Split('#')[fieldValuePart];

string query = string.Format(@"<Where><Eq><FieldRef Name=""{0}"" /><Value Type=""{1}"">{2}</Value></Eq></Where>"",
lookUpFieldName, lookUpTargetField.TypeAsString , fieldValue);

SPQuery queryForLookup = new SPQuery();
queryForLookup.Query = query;

SPListItemCollection listItems = copyToListLookUp.GetItems(queryForLookup);

if (listItems.Count > 0)
{
copyToItem[field.InternalName] = listItems[0];
}

Comments

Popular posts from this blog

CSOM System Update that does not update minor version number

I was in a scenario where I had to do a add some data to a document list in a web job. My issue was that I could not find any system update method in the CSOM API. After searching it i found a blog post showing that one could just override the system fields, like Modified and Editor(Modified My). That seemed to work but that did not stop it form updating the version number of the document. This example will only work for minor unpublished version. I have not found a way to do this without a published version. There it will get a new minor version. The solution combines the use of adding Editor, and Modified with the use of the ValidateUpdateListItem metod. I found that one can set the item values on the item and just set one value in the form value list, then it will update all the fields. If one then set the "bNewDocumentUpdate" it will not update the version number if it is a minor version. So what i do is just get the values from the item before an update, then sets...

Log4Net MVC.net and what web config to use

I'm new to using log4net ( remember using it a long time ago .net 2 area) so I did a rookie mistake. I trying to set it up using resources i found on the net (mainly http://www.codeproject.com/Articles/140911/log-net-Tutorial). But it did not log anything, and I did not get any errors in the Visual Studio  output. After some time and testing I found that because I had put the configuration part under the Views folder, I have to point to that config file. The reason was that I got an error stating that I can only have one configuration section in the web.config, if I added a configuration section in the root web.config. So then I tried to set the assembly section in the AsseblyInfo file to point to the web.config in the Views folder. Then it worked. [assembly: log4net.Config.XmlConfigurator(ConfigFile = "View\\Web.Config", Watch=true)]

No more features

An aria is over, I'm quit happy to see the new methods to use with provision of SharePoint solutions. https://channel9.msdn.com/blogs/OfficeDevPnP/PnP-Web-Cast-Feature-framework-vs-Remote-Provisioning