Udostępnij za pośrednictwem


Resolution: Issue with upgrading the schema.xml for a library

Issue

The issue was about upgrading the schema.xml for a library. If we do not do any change in document library settings then it uses schema.xml file but when we create a column in document library it stops using schema.xml file thereafter.

 

Details

We found that this behavior is by design. When you have not done any changes to document library it will use schema.xml and when you have done some changes in document library then SharePoint will make the document library unghosted to preserve the personalization settings of document library and it will not use schema.xml thereafter.

 

Following are the couple of possible workarounds…

1.    Maintain all document libraries through feature only (Schema.xml) and avoid using UI to create any new columns if you anticipate any future changes in document libraries.

2.    In code, you get the node for the column from the schema.xml file and add it through SPField class in your document library. You can take a help of SharePoint’s object model to prepare a complete code for your workaround using classes like SPSite, SPWeb, SPList etc...

The following code example iterates through all the lists in all the subsites under a site and, if it finds a list with a specified name, updates the title, default value, and description for a field.

SPWebCollection sites = siteCollection.AllWebs["Site_Name"].Webs;
foreach (SPWeb site in sites)
{
    SPListCollection lists = site.Lists;
    for (int i=0; i<lists.Count; i++)
    {
        if (lists[i].Title == "List_Name")
        {
            SPField fieldChange = lists[i].Fields["Field_Name"];   
            fieldChange.DefaultValue = "Default_Value";
            fieldChange.Description = "Description";
            fieldChange.Title = "New_Field_Name";
            fieldChange.Update();
        }
    }
}