Monday, May 10, 2010

Deleting a Field from Content Type and its list



private bool DeleteAttributeLabel(string AttributeLabelName)


{


try


{


using (SPSite p_site = new SPSite(SharePointURL))


{


using (SPWeb p_web = p_site.OpenWeb())


{


SPContentType ct = p_web.ContentTypes["VideoBankContentType"];


SPField fld = p_web.Fields[AttributeLabelName];


DeleteFieldRefFromContentType(ct, fld);


fld.Delete();


SPList list = p_web.Lists[AttributeLabelName];


list.Delete();


}


}


return true;


}


catch (Exception ex)


{


return false;


}


}





private bool DeleteFieldRefFromContentType(SPContentType contentType, SPField field)


{


try


{


//Check if the Field reference exists


if (contentType.Fields.ContainsField(field.Title))


{


contentType.FieldLinks.Delete(field.Title);


contentType.Update(true);


}


else


{


//Do Nothing


}


return true;


}


catch (Exception ex)


{


return false;


}


}


}

No comments:

Post a Comment