Using the SharePoint 2010 Managed Client Object Model – Discovering the Schema for Fields
This is a clipboard friendly version of example #14, Discovering the Schema for Fields, from Using the SharePoint 2010 Managed Client Object Model.
This blog is inactive.
New blog: EricWhite.com/blog
using System;
using System.Linq;
using System.Xml.Linq;
using Microsoft.SharePoint.Client;
class Program
{
static void Main(string[] args)
{
ClientContext clientContext =
new ClientContext("https://intranet.contoso.com");
List list = clientContext.Web.Lists
.GetByTitle("Client API Test List");
clientContext.Load(list);
FieldCollection fields = list.Fields;
clientContext.Load(fields);
clientContext.ExecuteQuery();
foreach (var f in fields)
{
XElement e = XElement.Parse(f.SchemaXml);
string name = (string)e.Attribute("Name");
if (name == "Category" || name == "Estimate")
{
e.Attributes("ID").Remove();
e.Attributes("SourceID").Remove();
e.Attributes("ColName").Remove();
e.Attributes("RowOrdinal").Remove();
e.Attributes("StaticName").Remove();
Console.WriteLine(e);
Console.WriteLine("===============");
}
}
}
}
Following is the same example using fully qualified names for better discoverability.
using System;
using System.Linq;
using System.Xml.Linq;
class Program
{
static void Main(string[] args)
{
Microsoft.SharePoint.Client.ClientContext clientContext =
new Microsoft.SharePoint.Client.ClientContext("https://intranet.contoso.com");
Microsoft.SharePoint.Client.List list = clientContext.Web.Lists
.GetByTitle("Client API Test List");
clientContext.Load(list);
Microsoft.SharePoint.Client.FieldCollection fields = list.Fields;
clientContext.Load(fields);
clientContext.ExecuteQuery();
foreach (var f in fields)
{
XElement e = XElement.Parse(f.SchemaXml);
string name = (string)e.Attribute("Name");
if (name == "Category" || name == "Estimate")
{
e.Attributes("ID").Remove();
e.Attributes("SourceID").Remove();
e.Attributes("ColName").Remove();
e.Attributes("RowOrdinal").Remove();
e.Attributes("StaticName").Remove();
Console.WriteLine(e);
Console.WriteLine("===============");
}
}
}
}