SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question
up vote 0 down vote accepted

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;
share|improve this answer
What does SPList list = web.Lists.TryGetList("Eliya"); do? – Rohander Mar 21 '13 at 12:22
I have updated the answer where "ListName" is the name of the custom "Announcements" list that i created. – Eliya Amanoeel Mar 21 '13 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 '13 at 13:13
This eventreciever trigger when a site gets created on a site collection – Rohander Mar 21 '13 at 13:18
this is the error: Unable to cast object of type 'Microsoft.SharePoint.SPWeb' to type 'Microsoft.SharePoint.SPSite'. – Rohander Mar 21 '13 at 13:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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