Update metadata value using server object model in sharepoint 2013
2013-12-20 20:56
645 查看
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.
The code about logging result:
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.
/// <summary> /// Update sharepoint 2013 metada value via the param of the config xml file. /// </summary> /// <param name="siteUrl2013"></param> /// <param name="siteUrl2010"></param> /// <param name="webUrl2013"></param> /// <param name="webUrl2010"></param> /// <param name="documentName2013"></param> /// <param name="documentName2010"></param> /// <param name="rootFolder2013"></param> /// <param name="rootFolder2010"></param> /// <param name="metadataServiceName"></param> /// <param name="groupName"></param> /// <param name="termSetName"></param> /// <param name="metadataName"></param> 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<string> 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++; } } }
相关文章推荐
- Get the metadata value using client object model from the sharepoint 2010
- Using Client Object Model In SharePoint
- Using the SharePoint 2010 Client Object Model_part_1
- 转:Code-blocks are not allowed in this file: Using Server-Side Code with SharePoint
- Using the SharePoint 2010 Client Object Model_part_1
- Using the SharePoint 2010 Client Object Model_part_1
- Uploading Files in SharePoint 2013 using CSOM and REST
- Using the SharePoint 2010 Client Object Model_part_2
- Using the SharePoint 2010 Client Object Model_part_2
- Change the default search topology in SharePoint Server 2013
- Using the SharePoint 2010 Client Object Model_part_2
- Using the SharePoint 2010 Client Object Model_part_3
- 转:Best Practices: Common Coding Issues When Using the SharePoint Object Model
- SharePoint 2013中的"Error in the Microsoft SharePoint Server People Protocol Handler"报错
- Using the SharePoint 2010 Client Object Model_part_3
- Using the SharePoint 2010 Client Object Model_part_4
- Using the SharePoint 2010 Client Object Model_part_3
- Team Foundation Server 2015(Update 1)集成 SharePoint Server 2013
- Using the SharePoint 2010 Client Object Model_part_4
- Using the SharePoint 2010 Client Object Model_part_5