1

my aim is to use a Sharepoint 2013 Foundation this way:

  1. define a template Document Library, ProjectTemplate

  2. define a list of projects, with a "category" column

  3. an itemAdded event receiver on the projects list creates a Doc Lib based on the ProjectTemplate each time a new project gets added; the Doc Lib is named with the project name

  4. for the category column of the Document Library, sets a default value equal to the added project' category. Everything is working, but the default value part. This is the code:

public override void ItemAdded(SPItemEventProperties properties)
{
    SPWeb myWeb = properties.Web;
    string projectName = properties.ListItem["Project Name"].ToString();
    var customTemplate = myWeb.ListTemplates["ProjectTemplate"];
    Guid myItemGuid = myWeb.Lists.Add(projectName , projectName , customTemplate);

    SPList myItem = myWeb.Lists[myItemGuid];

    myItem.Fields["Project Category"].DefaultValue = "Test default value";
    myItem.Update();

    properties.ListItem.Update();
    base.ItemAdded(properties);
}

I thought there was something dealing with maybe asynchronous execution of myWeb.Lists.Add, so I tried setting the defaultvalue for another existing Document Library - without luck anyway.

I even tried browsing with Sharepoint Manager, to see if the defaultvalue gets filled, or filling it by hand. No results.

Is field.defaultvalue managed in Sharepoint Foundation? It is not available from the site settings administration interface

Are custom fields accessible within ItemAdded event?

3
  • What do you mean ìt's not available on site settings administration interface? I would expect it to be under library settings -> columns -> your column OR library settings -> Column default value settings
    – MdMazzotti
    Commented Apr 19, 2015 at 9:32
  • In Sharepoint Foundation that feature is missing
    – RaffaeleT
    Commented Apr 19, 2015 at 15:37
  • 1
    call the .Update() on the field not on the item
    – Nk SP
    Commented Apr 19, 2015 at 16:07

1 Answer 1

3

I think you should call Update on the SPField object, whereas you're calling it on the SPList item object.

var projectCategoryField = myItem.Fields["Project Category"];
projectCategoryField.DefaultValue = "Test default value";
projectCategoryField.Update();
1
  • Hallo thank you, this solves in part: the field remains empty in the visualization; digging into Sharepoint with Sharepoint Manager, I see that the field gets the defaultValue.
    – RaffaeleT
    Commented Apr 20, 2015 at 7:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.