Wednesday, May 14, 2014

SharePoint 2010: How to activate Target Audience

I had a bit of trouble, like most days for the last two weeks, to get my coding done. Again, it's because I usually don't code as much as I do at the moment. I need to create a field "Target Audience" and fill it with the correct value. Funny thing so: I shouldn't just create this field as a new Site column or something like that. I need to activate audience targeting first. But: How do I code an activation script of some kind in C#?
You will find two links below with an explanation, but I wanted to post the code anyway so it might be easier to find.

// Activate audience targeting
try
{

// Get the list where you want to activate audience targeting
SPList myList = myWeb.Lists["LIST"];
// leave this as it is, only change the "Required" attribute
XmlElement fieldElement = new XmlDocument().CreateElement("Field");
fieldElement.SetAttribute("ID", "61cbb965-1e04-4273-b658-eedaa662f48d");
fieldElement.SetAttribute("Type", "TargetTo");
fieldElement.SetAttribute("Name", "TargetTo");
fieldElement.SetAttribute("DisplayName", "Target Audiences");
fieldElement.SetAttribute("Required", "FALSE");
myList.Fields.AddFieldAsXml(fieldElement.OuterXml);
myList.Update();

}

catch
{
}
And here are some more links with information on this topic:
http://www.c-sharpcorner.com/uploadfile/anavijai/enable-audience-targeting-for-sharepoint-list-using-powershell/
http://blogs.msdn.com/b/syedi/archive/2008/09/12/enabling-the-audience-targeting-through-object-model-moss-sharepoint-2007.aspx

No comments:

Post a Comment