May times we require to add columns defined at root web level to any site at child level.
Here is a simple way to accomplish this :
Make one function like this :
private void AddSiteColumnToList(string sitePath, string listName, string columnName)
{
using (SPSite site = new SPSite(sitePath))
{
using (SPWeb web = site.OpenWeb())
{
using (SPWeb root = site.RootWeb)
{
//Take the site column from the root web
SPField fld = root.Fields[columnName];
//get the list from the site
SPList list = web.Lists[listName];
//add the column
list.Fields.Add(fld);
//update the list
list.Update();
}
}
}
That's it. Your job is done.
Thank you
No comments:
Post a Comment