1

I'm attempting to create a Site-Collection Feature in SharePoint 2013 that will create a new Site Column when activated, add this site column to every document library in the collection, and then include it in the libraries' default view.

However I am having an odd situation on the step of adding it to the default view. When the field has been created and is to be added to the default view, on the first run through, the field is still missing and throws "Key cannot be null"

The relevant class has the following function:

private void AddColumnToLibrary(TaxonomyField column, SPDocumentLibrary library)
{
    library.Fields.Add(column);
    library.Update();
    SPView view = library.DefaultView;
    view.ViewFields.Add(column);
    view.Update();
    library.Update();
}

I've managed to debug the feature activation, error occurs at the line
view.ViewFields.Add(column)

On closer investigation, the library does not still have the new column after library.Update(); hence it failing when attempting to add the column to the default view.

However, when attempting to activate the feature the second time, the field is present already, and thus the function passes. The feature therefore activates with no error.

Is there a way to retrieve this new field immediately after creating it? I've attempted to re-get the list var updatedlibrary = library.ParentWeb.Lists[library.ID] but this is still missing the new field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.