Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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"
             FieldInternalName="Expires"
             FieldType="SPFieldDateTime"
          -->

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;
            expiredDate.Update();

    }

}

Any kind of help or tips is appreciated!

share|improve this question
add comment

1 Answer 1

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;
            field.Update();
        }
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
show 8 more comments

Your Answer

 
discard

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.