Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

view not updating in event receiver

$
0
0

I have created a C# SharePoint 2010 Non-sandboxed project that implements the ListAdded event receiver. What I want to do is to check the new document library and add a number of site columns to it depending on the template. (I have discovered that the ListAdding event receiver is useless as the list reference is null at that point.)

For each field, I run the following code whch successfully adds the columns. However, the code seems to have no effect on the default view.

private void AddColumn(string name, SPListEventProperties properties)
{
    SPWeb web = properties.Web;
    SPField field = null;
    try
    {
        field = web.AvailableFields[name];
        properties.List.Fields.Add(field);
        field.ShowInDisplayForm = true; // Saw these in an post about SP 2007. Not needed?
        field.ShowInEditForm = true;
        field.ShowInListSettings = true;
        field.ShowInNewForm = true;
        field.ShowInViewForms = true;   
        properties.List.Update();
        var xx = field.InternalName;                 
        properties.List.DefaultView.ViewFields.Add(xx);
        properties.List.DefaultView.Update(); 
    }
    catch (Exception)
    {
    }
}
At the bottom of the method, examining properties.List.ViewFields in the Watch window shows that it is untouched by my code. What am I doing wrong? The UI confirms the fact that the the columns have been added and are available for adding to the view.



Viewing all articles
Browse latest Browse all 7589

Trending Articles