In my case I have 2 columns in the list, I want to delete one column from the list if the list contains 2 columns.
public static void DeleteColumn(string siteURL, String copyFromFieldInternalName, String copyToFieldInternalName)
{
using (SPSite site = new SPSite(siteURL))
{
foreach (SPWeb web in site.AllWebs)
{
//Get all the List in every list of the web that has the 2 fields
var items = (from l in web.Lists.Cast<SPList>()
where l.Fields.ContainsField(copyFromFieldInternalName)
&& l.Fields.ContainsField(copyToFieldInternalName)
select l).ToList();
foreach (SPList list in items)
{
Console.WriteLine("WebUrl: " + web.Url);
Console.WriteLine("Deleteing the column " + copyFromFieldInternalName + "from the list " + list.Title);
list.Fields.Delete(copyFromFieldInternalName);
list.Update();
}
}
}
}
public static void DeleteColumn(string siteURL, String copyFromFieldInternalName, String copyToFieldInternalName)
{
using (SPSite site = new SPSite(siteURL))
{
foreach (SPWeb web in site.AllWebs)
{
//Get all the List in every list of the web that has the 2 fields
var items = (from l in web.Lists.Cast<SPList>()
where l.Fields.ContainsField(copyFromFieldInternalName)
&& l.Fields.ContainsField(copyToFieldInternalName)
select l).ToList();
foreach (SPList list in items)
{
Console.WriteLine("WebUrl: " + web.Url);
Console.WriteLine("Deleteing the column " + copyFromFieldInternalName + "from the list " + list.Title);
list.Fields.Delete(copyFromFieldInternalName);
list.Update();
}
}
}
}
No comments:
Post a Comment