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

2 篇文章 0 订阅
2 篇文章 0 订阅

Steps Involved:

1.       Open Visual Studio 2013 (Run as administrator).

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

3.       Select Console Application in the Visual C# node from the installed templates.

4.       Enter the Name and click on Ok.

5.       In the solution explorer, right click on References folder and then click on Add 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", taxFiled.InternalName);
                                writer.WriteElementString("values", "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();
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值