Identifying multiple/single values of metadata and recording into XML via .NET Client Object Model

前端之家收集整理的这篇文章主要介绍了Identifying multiple/single values of metadata and recording into XML via .NET Client Object Model前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Steps Involved:

1.OpenVisual Studio 2013(Run as administrator).

2.Go to File=> New => Project.

3.SelectConsole Applicationin theVisual C#node from the installed templates.

4.Enter theNameand click onOk.

5.In the solution explorer,right click onReferencesfolder and then click onAdd Reference.

6.Add the following assemblies from 15 hive (C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI).

a.Microsoft.SharePoint.Client.dll

b.Microsoft.SharePoint.Client.Runtime.dll

c.Microsoft.SharePoint.Client.Taxonomy.dll

7.Open Program.cs file and replace the code with the following

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. using Microsoft.SharePoint.Client;
  8. using Microsoft.SharePoint.Client.Taxonomy;
  9. using System.Xml;
  10. using System.IO;
  11.  
  12. namespace IdentifyMetadataColumns
  13. {
  14. class Program
  15. {
  16. static void Main(string[] args)
  17. {
  18. ClientContext context = new ClientContext("http://sp2dev/en-us");
  19. ListCollection listsColl = context.Web.Lists;
  20. context.Load(listsColl);
  21. context.ExecuteQuery();
  22. XmlTextWriter writer = new XmlTextWriter("recordValues.xml",null);
  23. writer.Formatting = Formatting.Indented;
  24. writer.WriteStartElement("lists");
  25. foreach(List list in listsColl){
  26. writer.WriteStartElement("list");
  27. writer.WriteAttributeString("listName",list.Title);
  28.  
  29. Console.WriteLine(list.Title + ":");
  30. FieldCollection collField = list.Fields;
  31. context.Load(collField,fields => fields.Include(field => field.Title));
  32. context.ExecuteQuery();
  33. foreach (Field field in collField)
  34. {
  35. //if (field is TaxonomyField) {
  36. // Console.WriteLine("success---------");
  37. // return;
  38. //}
  39. TaxonomyField taxFiled = field as TaxonomyField;
  40. if (taxFiled != null)
  41. {
  42. try
  43. {
  44. context.Load(taxFiled);
  45. context.ExecuteQuery();
  46. if (taxFiled.AllowMultipleValues)
  47. {
  48. //Multiple
  49. Console.WriteLine("\t" + taxFiled.InternalName + "\t"+"Multiple");
  50. writer.WriteStartElement("MetadataColumn");
  51. writer.WriteAttributeString("name",taxFiled.InternalName);
  52. writer.WriteElementString("values","Multiple");
  53. writer.WriteEndElement();
  54. }
  55. else
  56. {
  57. //Single
  58. Console.WriteLine("\t" + taxFiled.InternalName + "\t" + "Single");
  59. writer.WriteStartElement("MetadataColumn");
  60. writer.WriteAttributeString("name","Single");
  61. writer.WriteEndElement();
  62. }
  63. continue;
  64. }//try end
  65. catch (ArgumentException exception)
  66. {
  67. Console.WriteLine("fail");
  68. throw new ArgumentOutOfRangeException(Resources.GetString("AllFieldsNotFetched"),exception);
  69. }
  70. }//if end
  71. }//foreach field end
  72. writer.WriteEndElement();
  73. }//foreach list end
  74. writer.WriteEndElement();
  75. writer.Close();
  76. Console.ReadLine();
  77. }
  78. }
  79. }

猜你在找的XML相关文章