Saturday, 22 March 2014

Enable Target Audience Field Programmatically

The following code snippet can be used to enable target audience field programmatically.

 using (SPSite site = new SPSite("SiteUrl"))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists["Documents"];

                    XmlElement fldElement = new XmlDocument().CreateElement("Field");
                    fldElement.SetAttribute("ID", "61cbb965-1e04-4273-b658-eedaa662f48d");
                    fldElement.SetAttribute("Type", "TargetTo");
                    fldElement.SetAttribute("Name", "TargetTo");
                    fldElement.SetAttribute("DisplayName", "Target Audiences");
                    fldElement.SetAttribute("Required", "FALSE");

                    list.Fields.AddFieldAsXml(fldElement.OuterXml);
                    list.Update();
                }
            }

No comments:

Post a comment