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

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Taxonomy;
using System.Xml;
using System.IO;

namespace IdentifyMetadataColumns
{
    class Program
    {
        static void Main(string[] args)
        {
            ClientContext context = new ClientContext("http://sp2dev/en-us");
            ListCollection listsColl = context.Web.Lists;
            context.Load(listsColl);
            context.ExecuteQuery();
            XmlTextWriter writer = new XmlTextWriter("recordValues.xml",null);
            writer.Formatting = Formatting.Indented;
            writer.WriteStartElement("lists");
            foreach(List list in listsColl){
                writer.WriteStartElement("list");
                writer.WriteAttributeString("listName",list.Title);

                Console.WriteLine(list.Title + ":"); 
                FieldCollection collField = list.Fields;
                context.Load(collField,fields => fields.Include(field => field.Title));
                context.ExecuteQuery();
                foreach (Field field in collField)
                {
                    //if (field is TaxonomyField) {
                    //    Console.WriteLine("success---------");
                    //    return;
                    //}
                    TaxonomyField taxFiled = field as TaxonomyField;
                    if (taxFiled != null)
                    {
                        try
                        {                            
                            context.Load(taxFiled);
                            context.ExecuteQuery();                                                   
                            if (taxFiled.AllowMultipleValues)
                            {
                                //Multiple                                    
                                Console.WriteLine("\t" + taxFiled.InternalName + "\t"+"Multiple");
                                writer.WriteStartElement("MetadataColumn");
                                writer.WriteAttributeString("name",taxFiled.InternalName);
                                writer.WriteElementString("values","Multiple");
                                writer.WriteEndElement();
                            }
                            else
                            {
                                //Single
                                Console.WriteLine("\t" + taxFiled.InternalName + "\t" + "Single");
                                writer.WriteStartElement("MetadataColumn");
                                writer.WriteAttributeString("name","Single");
                                writer.WriteEndElement();
                            }                                                                                           
                            continue;
                        }//try end
                        catch (ArgumentException exception)
                        {
                            Console.WriteLine("fail");
                            throw new ArgumentOutOfRangeException(Resources.GetString("AllFieldsNotFetched"),exception);
                        }
                    }//if end
                                                      
                }//foreach field end
                writer.WriteEndElement();               
            }//foreach list end            
            writer.WriteEndElement();
            writer.Close(); 
            Console.ReadLine();
        }
    }
}

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念