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

The SetUp

I have created 3 site columns. All set to not required fields. I have added these site columns to a content type. This content type I have added to a document library. The document library has had a couple of extra columns added to it locally, (causing a child content type), and two of the original site columns I have made required fields. This means on the library these fields are required by in the original content type they are still not required.

The problem

When I read the field in from the list and see if it is required it still states that it isn't required. Which means it appears to be reading it from the content type not from the list. Please see my code below. Am I coding it wrong?

 for (int i = 0; i < spView.ViewFields.Count; i++)
 {
   string fieldName = spView.ViewFields[i];
   SPField field = spList.Fields.GetField(fieldName);
   var isRequired = field.Required;
  //Other code once found out if field is required.
 }
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

Right, let's correct a few things:

First, adding a content type to the list will create a child content type. No list item ever has a Site Content Type as it's type. They're all list content types.

Second, you're iterating over the fields of the list, and checking the columns definition for if they're required. However, the columns are only required for your content type. The list could (theoretically) contain other content types for whom these columns are not required.

Thus, the question of whether or not that field is required is specific to that content type, rather than the list, and you'll find this in the SPContentType.FieldLinks collection. Each FieldLink lets you read/select if a given column is required for that content type.

So it should be something like:

SPContentType ct = ...
foreach(SPFieldLink fl in ct.FieldLinks) {
   bool isRequired = fl.Required;
   //Other code
}
share|improve this answer
    
Hi. Thank you for your reply. Sorry I've taken time to get back to you but I've been away. I will be looking at your answer shortly and hopefully update my code accordingly. I will let you know if it solves my problem. –  Cann0nF0dder Jun 7 '12 at 9:47
    
Hi Andy, I have tested the above and got it working. I now read the correct required fields. Although this causes a problem further into my code, which I hadn't included. After finding if the control is required or not, it renders a title on the page, I then render the control using field.FieldRenderingControl. This I'm unable to do with a spfieldlink. Because of this the SPField renders with the site columns required status not the list column required status. Do you have any idea what I should do? –  Cann0nF0dder Jun 7 '12 at 13:23
    
Not off the top of my head. I gues you could render your own 'required' marker. Or have you considered using the ListFieldIterator control? –  Andy Burns Jun 12 '12 at 12:32
add comment

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.