I have created a EventReciever that will be activated when a site gets created. This site have a announcement webpart. When they click on "add announcement", newform.aspx pops up as a dialog and there is a field there that is called Expired I need to make it required.


How can I find that specific field and set it to Required in my EventReciever?

<!-- FieldName="Expires"

This is how I have progressed so far:

public override void FeatureActivated(SPFeatureReceiverProperties properties)

    if (properties.Feature.Parent is SPWeb)
        var web = properties.Feature.Parent as SPWeb;
            var expiredDate = web.Fields.GetFieldByInternalName("Expires");
            expiredDate.Required = true;



Any kind of help or tips is appreciated!

1 Answer 1


I tried to work in some kind of a similar scenario as yours, however, i created an "Announcements" list and then added a web part that shows the content of that list. Then i used the code below to make the "Expires" field as "Required". I hope this will help:

        SPSite site = (SPSite)properties.Feature.Parent;
        SPWeb web = site.OpenWeb();
        SPList list = web.Lists.TryGetList("ListName");
        SPField field = list.Fields.GetField("Expires");
        if (field.Required == false)
            field.Required = true;
  • What does SPList list = web.Lists.TryGetList("Eliya"); do?
    – Rohander
    Mar 21, 2013 at 12:22
  • I have updated the answer where "ListName" is the name of the custom "Announcements" list that i created. Mar 21, 2013 at 12:51
  • hmm I getting a error when I try to create a site, are you sure this code is not for site collection?
    – Rohander
    Mar 21, 2013 at 13:13
  • This eventreciever trigger when a site gets created on a site collection
    – Rohander
    Mar 21, 2013 at 13:18
  • this is the error: Unable to cast object of type 'Microsoft.SharePoint.SPWeb' to type 'Microsoft.SharePoint.SPSite'.
    – Rohander
    Mar 21, 2013 at 13:20

Your Answer

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

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