Question

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??

Was it helpful?

Solution

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();

}

OTHER TIPS

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top