2

I'm creating a Sharepoint feature, this feature has an event receiver associated to it. In the event receiver, I'm creating a Document Library and Picture Library using server-side object model. I'm also adding new custom columns (around 80) to these newly created document and picture library. Now I want to modify the properties of the Description, Keywords and Title fields that are by default created along with the picture library. I want to make these fields as Required fields. How do I do this? I tried to set SPList.AllowContentTypes = true and try to change the attributes of these fields, but it doesn't work (neither gives an error nor makes these required fields). I also tried to access the content types and try to change the attributes using SPContentType.FieldsLinks["Column_name"].Required and SPContentType.Fields["Column_name"].Required but it gives me an error. Does anyone have any other suggestions?

xgencoder
  • 419
  • 3
  • 10
  • You need to set these fields as required for the **list**. So try to get the SPField from the list and set `SPField.Required` to `true` – Dennis G Nov 03 '11 at 08:20
  • no luck, these are not the fields that I created. they come with the Picture Library definition. I tried different ways to get references to SPField, but nothing worked so far... – xgencoder Nov 03 '11 at 08:30

4 Answers4

2

Here is the answer....

SPContentType ct = mypiclib.ContentTypes["Picture"];
SPFieldLinks titleLink = ct.FieldLinks["Title"];
SPFieldLinks descLink = ct.FieldLinks["comments"]; //internal name of Description
SPFieldLinks keywords = ct.FieldLinks["keywords"];
titlelink.Required = true;
descLink.Required = true;
keywords.Required = true;
ct.Update();
xgencoder
  • 419
  • 3
  • 10
0

can you tell us the error you got when trying to use the fieldlinks? Because this should work... I have done it like this:

SPContentType ct = web.Lists["*ListName*"].ContentTypes["*ContentTypeName*"];
SPFieldLinkCollection flinks = ct.FieldLinks;
flinks["*ColumnName*"].Required = true;
ct.update();
Tjassens
  • 922
  • 6
  • 9
  • tried, it complains that Description field is not available in picture library... It actually exist, not sure how to handle Description field. – xgencoder Nov 03 '11 at 13:34
0

This should do the trick:

SPWeb yourWeb =  ... //assign your web
SPList yourPictureLibrary = ... //assign your picture library

web.AllowUnsafeUpdates = true;
yourPictureLibrary.Fields[SPBuiltInFieldId.Title].Required = true;
yourPictureLibrary.Fields[SPBuiltInFieldId.Description].Required = true;
yourPictureLibrary.Fields[SPBuiltInFieldId.Keywords].Required = true;
yourPictureLibrary.Update();
Pedro Fonseca
  • 198
  • 1
  • 16
0

SPAllowContentTypes isn't settable. You might try setting ContentTypesEnabled instead.

I don't have a 2010 box to test against, but if SPAllowContentTypes returns false I think you're looking at modifying the definition of your picture library in the 14 hive (TEMPLATE\FEATURES\PictureLibrary\PicLib) to get what you're after. Tread lightly there.

vinny
  • 1,810
  • 15
  • 21