This blog will continue The steps of migrating metadata from SP site 2010 to SP site 2013, and the below is about Updating metadata value using server object model in sharepoint 2013
1. Update the metadata value of the item in list about sharepoint 2013.
2. Save the result into log.
///
/// Update sharepoint 2013 metada value via the param of the config xml file.
///
///
///
///
///
///
///
///
///
///
///
///
///
public void updateMetadata(string siteUrl2013, string siteUrl2010, string webUrl2013, string webUrl2010, string documentName2013, string documentName2010, string rootFolder2013, string rootFolder2010, string metadataServiceName, string groupName, string termSetName, string metadataName)
{
using (SPSite site = new SPSite(siteUrl2013 + webUrl2013))
{
MetadataXML xml = new MetadataXML();
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(xmlMetadata);
Log log = new Log();
log.beginMigrate(siteUrl2013 + webUrl2013 + documentName2013 );
try
{
string fileName;
string relativeUrl;
List termName;
TaxonomySession session;
TermStore termStore;
Microsoft.SharePoint.Taxonomy.Group group;
TermSet termSet;
Term term;
Term newterm;
TaxonomyField taxField;
TaxonomyFieldValueCollection taxCollection;
int count;
SPWeb web = site.OpenWeb();
web.AllowUnsafeUpdates = true;
SPList list = web.Lists[documentName2013];
SPListItemCollection listItems = list.Items;
web.AllowUnsafeUpdates = true;
ClientMetadataService service = new ClientMetadataService();
session = new TaxonomySession(site);
termStore = session.TermStores[metadataServiceName];
group = termStore.Groups[groupName];
termSet = group.TermSets[termSetName];
count = termSet.Terms.Count;
foreach (SPListItem item in listItems)
{
fileName = item["LinkFilename"].ToString();
if (webUrl2010 != "/")
{
relativeUrl = webUrl2010 + "/" + item.Url.Replace("/" + fileName, "").Replace(rootFolder2013, rootFolder2010);
}
else
{
relativeUrl = webUrl2010 + item.Url.Replace("/" + fileName, "").Replace(rootFolder2013, rootFolder2010);
}
taxField = item.Fields[metadataName] as TaxonomyField;
taxCollection = new TaxonomyFieldValueCollection(taxField);
termName = xml.getDocument(myXmlDoc, webUrl2010, documentName2010, metadataName, relativeUrl, fileName);
int flag = 0;
int flagTerm = 0;
if (termName.Count > 0)
{
if (termName[0] != "")
{
for (int i = 0; i < termName.Count; i++)
{
int termsCount = termSet.Terms.Count;
for (int num = 0; num < termsCount; num++)
{
TaxonomyFieldValue taxValue = new TaxonomyFieldValue(taxField);
term = termSet.Terms[num];
newterm = service.circleTerm(term, termName[i]);
if (newterm != null)
{
flagTerm = 1;
taxValue.TermGuid = newterm.Labels[0].Term.Id.ToString();
taxValue.Label = newterm.Labels[0].Term.Name;
if (taxField.AllowMultipleValues)
{
taxCollection.Add(taxValue);
}
else
{
taxField.SetFieldValue(item, taxValue);
flag = 1;
}
break;
}
}
}
}
else
{
flagTerm = 1;
}
}
if (flagTerm == 0 && termName.Count > 0)
{
string metadata = "";
for (int i = 0; i < termName.Count; i++)
{
metadata += metadata + termName[i].ToString() + ";";
}
log.errorNoMetadata(siteUrl2013 + webUrl2013 + item.Url, metadata);
}
else if (termName.Count == 0)
{
log.errorNofound(siteUrl2013 + webUrl2013 + item.Url);
}
if (taxField.AllowMultipleValues)
{
taxField.SetFieldValue(item, taxCollection);
}
else if (flag == 0)
{
TaxonomyFieldValue taxValue = new TaxonomyFieldValue(taxField);
taxField.SetFieldValue(item, taxValue);
}
item.SystemUpdate();
}
web.Update();
}
catch
{
log.errorMigrate(siteUrl2013 + webUrl2013 +"/"+ documentName2013);
}
finally {
log.endMigrate(siteUrl2013 + webUrl2013 + "/" + documentName2013);
}
}
}
}
The code about logging result:
public void errorNofound(string url2013)
{
string words = "ERROR : Unable to find the item ";
lock (typeof(Log))
{
FileInfo finfo = new FileInfo(fname);
using (FileStream fs = finfo.OpenWrite())
{
StreamWriter SW = new StreamWriter(fs);
SW.BaseStream.Seek(0, SeekOrigin.End);
SW.WriteLine(words + "\"" + url2013 + "\"" +" in site 2010");
SW.Close();
errorNumber++;
}
}
}