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 the following code used to programmatically create content types. The code creates and deletes the content type using a windows form app which I will eventually transfer to an event receiver. The odd thing is that with each time I delete/create the content type random fields are marked as hidden. To compensate I even added false to the Hidden property on the SPField but to no avail. Has anyone seen this oddity before? Suggestions for fixing are welcome. Thanks for reading.

#region Field Defs
var passprtFieldName = web.Fields.Add("Passport Number", SPFieldType.Text, false);
var passprtField = web.Fields.GetFieldByInternalName(passprtFieldName) as SPFieldText;
passprtField.MaxLength = 15;
passprtField.Group = GROUP_NAME;
passprtField.AllowDeletion = false;
passprtField.Hidden = false;
passprtField.Update();

var empIdFieldName = web.Fields.Add("Employee ID", SPFieldType.Text, false);
var empIdField = web.Fields.GetFieldByInternalName(empIdFieldName) as SPFieldText;
empIdField.MaxLength = 10;
empIdField.Group = GROUP_NAME;
empIdField.AllowDeletion = false;
empIdField.Hidden = false;
empIdField.Update();

....

var item = web.ContentTypes["Item"];
var volunteers = new SPContentType(item, web.ContentTypes, VOLUNTEER_CONTENT_TYPE_NAME);
volunteers.Group = GROUP_NAME;

var title = volunteers.FieldLinks["Title"] as SPFieldLink;
title.DisplayName = "Volunteer Name";

volunteers.FieldLinks.Add(new SPFieldLink(passprtField));
volunteers.FieldLinks.Add(new SPFieldLink(empIdField));
volunteers.FieldLinks.Add(new SPFieldLink(driveLicField));
volunteers.FieldLinks.Add(new SPFieldLink(homeStreetAddrsField));
volunteers.FieldLinks.Add(new SPFieldLink(homeCityAddrsField));
volunteers.FieldLinks.Add(new SPFieldLink(homeStateAddrsField));
volunteers.FieldLinks.Add(new SPFieldLink(homeZipAddrsField));

web.ContentTypes.Add(volunteers);
volunteers.Update();

Results for run 1 enter image description here

Results for run 2 enter image description here

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

I was able to reproduce this problem on 2010 and 2013 :) I have run into lots of quirks like this before, and I believe it is because the object in memory does not reflect the correct state of the object after you call Update(), and then you try to use the same in-memory object for another purpose. In the past I have found it necessary to discard a new SharePoint object after Update() and re-create before using it later.

e.g from your code...

// here you create and then update the field object
var passprtFieldName = web.Fields.Add("Passport Number", SPFieldType.Text, false);
var passprtField = web.Fields.GetFieldByInternalName(passprtFieldName) as SPFieldText;
passprtField.MaxLength = 15;
passprtField.Group = GROUP_NAME;
passprtField.AllowDeletion = false;
passprtField.Hidden = false;
passprtField.Update();
...
// later you use the same object to add as a Field Link on the Content Type
volunteers.FieldLinks.Add(new SPFieldLink(passprtField);

I was able to fix the problem two ways. When adding the field link, set the Hidden and ReadOnly properties on the new SPFieldLink object:

SPFieldLink passprtFieldLink = new SPFieldLink(passprtField);
passprtFieldLink.Hidden = false;
passprtFieldLink.Required = false;
volunteers.FieldLinks.Add(passprtFieldLink);

but I believe this is just masking the problem. The better approach is to re-retrieve a new SPField object:

// refresh the object by retrieving an updated reference before adding as field link   
SPField passprtField = web.Fields.GetFieldByInternalName(passprtField.InternalName);
volunteers.FieldLinks.Add(new SPFieldLink(passprtField));

Hope this helps :)

share|improve this answer
1  
That did it! I used your second approach and all came out clean. Thanks Chris! –  ChiliYago Jun 25 '13 at 17:03
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.