Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a List Column Exists using SharePoint Client Object Model?

Using the Client Object Model (C#) in SharePoint 2010, how can I determine if a specified column (field) name exists in a given List?

Thanks, MagicAndi.

like image 862
Tangiest Avatar asked Dec 09 '10 17:12

Tangiest


2 Answers

Just found this while searching for the same thing, but it looks like Sharepoint 2010 has something built in for this, at least for the Server model: list.Fields.ContainsField("fieldName");

Not sure if it exists for Client side though. Figured it would be a good place to store this information however.

like image 94
EHorodyski Avatar answered Oct 22 '22 18:10

EHorodyski


Server Object Model

string siteUrl = "http://mysite";
using (SPSite site = new SPSite(siteUrl))
{
    using (SPWeb web = site.OpenWeb())
    {
        SPList list = web.Lists["my forum"];
        for (int i = 0; i < list.Fields.Count; i++)
        {
            if (list.Fields[i].Title == "xyz")
            {
                -
                -
            }
        }
    }
}

Client Object Model

string siteUrl = "http://MyServer/sites/MySiteCollection";
ClientContext clientContext = new ClientContext(siteUrl);
SP.List List = clientContext.Web.Lists.GetByTitle("my forum");
for (int i = 0; i < list.Fields.Count; i++)
{
    if (list.Fields[i].Title == "xyz")
    {
        -
        -
    }
}
like image 39
Ashutosh Singh-MVP SharePoint Avatar answered Oct 22 '22 17:10

Ashutosh Singh-MVP SharePoint