1

I have the following ItemUpdated event receiver inside my sharepoint farm 2013. now inside my itemupdated event receiver i am creating a new sub-site, then inside a list titled "Risk & Issue", i am accessing its list content type, and set the title field as hidden inside the list content type and leave the settings as is inside the site content type. so i tried the following code:-

 public override void ItemUpdated(SPItemEventProperties properties)
        {

            base.ItemUpdated(properties);
            //create the subsite
            SPWeb newSite = spCurrentSite.Webs.Add(curItemID, curItemSiteName, "created automatically after adding a new project item", Convert.ToUInt16(1033), webTemplate, false, false);
            //code goes here
            newSite.Update();
            // loop through the new site lists
            for (int i = 0; i < newSite.Lists.Count; i++)
            {
            if (newSite.Lists[i].Title.ToLower() == "risk & issue")
                {

                    SPContentType riskCT =  newSite.Lists[i].ContentTypes[0];
                    if (riskCT.Fields.ContainsFieldWithStaticName("Title") == true)
                    {
                    SPField titlefield=   riskCT.Fields.GetFieldByInternalName("Title");
                    titlefield.Hidden = true; 
                    riskCT.Update();

                    }
                }
            }
            newSite.Dispose();
        }

now when the event receiver runs, it will create a new subsite, and i will not get any exception, but the title field inside the content type will still be set to Required instead of being hidden. so can anyone adivce what is wrong with my code, that is preventing the Title field from being set as hidden inside the list content type??

2 Answers 2

1

Try the below code:

public override void ItemUpdated(SPItemEventProperties properties)
{

    base.ItemUpdated(properties);
    //create the subsite
    SPWeb newSite = spCurrentSite.Webs.Add(curItemID, curItemSiteName, "created automatically after adding a new project item", Convert.ToUInt16(1033), webTemplate, false, false);
    //code goes here
    newSite.Update();

    //change the list title as its in the site
    //no need to iterate the lists in the subsite
    SPList list = newSite.Lists.TryGetList("Risk & Issue");

    SPContentType listCT = list.ContentTypes[0];

    SPField field = listCT.Fields["Title"]; 
    listCT.FieldLinks[field.Id].Required = false; 
    listCT.FieldLinks[field.Id].Hidden = true; 
    listCT.Update();

    newSite.Dispose();

}
7
  • thanks for your reply.. now i do not want to hide the title field from the list view, i want to hide it from the list content type... simialr to going to the list settings>>list content type>click on the title field>> set it as hidden..
    – John John
    Commented Jul 9, 2017 at 21:36
  • 1
    Can you check the edited code ? Commented Jul 10, 2017 at 17:34
  • now i tried your updated code and it is working well,, but i am not sure why you are setting this ` listCT.FieldLinks[field.Id].Required = false; ` i remove this line of code and my title field was set to hidden.. so is there a specific reason you are using this line of code listCT.FieldLinks[field.Id].Required = false; ??? as seems listCT.FieldLinks[field.Id].Hidden = true; will be sufficient to set the field as hidden..
    – John John
    Commented Jul 10, 2017 at 23:04
  • The primary reason i wrote it was because by default Title column is set to required in the content types. But since we want to hide it, i set the required to false. But if its working in your case even without that line of code, you can remove it. I just thought of it as a kinda good practice to make the field non-required first and then hide it. Commented Jul 11, 2017 at 3:50
  • 1
    Yes it will be overridden. If you make a change at site CT level, and choose to propagate change then at the list level it will be changed as well. Commented Jul 11, 2017 at 12:30
1

Try this:

SPList list = newSite.Lists.TryGetList("Risk & Issue");
SPField titlefield= list.Fields["Title"];
if (!titlefield.CanToggleHidden)
{
  Type type = titlefield.GetType();
  MethodInfo mi = type.GetMethod("SetFieldBoolValue", BindingFlags.NonPublic | BindingFlags.Instance); 
  mi.Invoke(titlefield, new object[] { "CanToggleHidden", true });
}
titlefield.Required = false;
titlefield.Hidden = true;
titlefield.Update();
4
  • now i find this link which propsed 2 appraochies, and it spandps.com/2011/02/09/… but it mentioned that using toggling CanToggleHidden internal property is not a supported appraoch..
    – John John
    Commented Jul 10, 2017 at 23:08
  • 1
    It is true. It is not supported. But it is the only approach that has worked for me when the site has been created through a template. Commented Jul 11, 2017 at 12:04
  • but i tried the below appraoch which mainly uses listCT.FieldLinks[field.Id].Hidden = true; and it worked for me.. so is there any reason you did not try it ? second question, i am not sure what does not supported mean? does it mean that Microsoft can not guarantee that it will work !
    – John John
    Commented Jul 11, 2017 at 12:17
  • 1
    If the supported option works, use it. Yes, it means what you wrote. Commented Jul 11, 2017 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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