Create New Field with Specific Internal Name in SharePoint 2010 Client Object Model
When using Fields.AddFieldsAsXml as next, you won't have control over the Internal Name of the created field. "Name='HelloWorld'" part of the CAML will be ignored by SharePoint 2010 and the created field will have display name "Hello World" and internal name "Hello_x0020_World". The "DisplayName='Hello World'" is used for both display name and internal name (which encoded it as Hello_x0020_World).
using (ClientContext context = new ClientContext(tbUrl.Text))
{
try
{
Web web = context.Site.OpenWebById(currListInfo.WebId);List list = web.Lists.GetById(currListInfo.ListId);Field field = list.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='Hello World' Name='HelloWorld' />", true, AddFieldOptions.AddFieldCheckDisplayName | AddFieldOptions.AddToAllContentTypes);
context.Load(field, f => f.Title, f => f.InternalName);context.ExecuteQuery();LogInfo("Field Title = " + field.Title + " Internal Name = " + field.InternalName);}catch (Exception expt)
{
LogInfo(expt.Message + expt.StackTrace);
}
}
To have a control over the internal name of the created field, you can use your intended internal name as both "DisplayName" and "Internal Name" and then updated the "Display Name" with the intended display name.
using (ClientContext context = new ClientContext(tbUrl.Text))
{
try
A similiar work-around for SharePoint server side object model is documented at{Web web = context.Site.OpenWebById(currListInfo.WebId);List list = web.Lists.GetById(currListInfo.ListId);Field field = list.Fields.AddFieldAsXml("<Field Type='Text' DisplayName='HelloWorld' Name='HelloWorld' />", true, AddFieldOptions.AddFieldCheckDisplayName | AddFieldOptions.AddToAllContentTypes);field.Title = "Hello World";field.Update();
context.Load(field, f => f.Title, f => f.InternalName);context.ExecuteQuery();LogInfo("Field Title = " + field.Title + " Internal Name = " + field.InternalName);}catch (Exception expt)
{LogInfo(expt.Message + expt.StackTrace);}}
http://weblogs.asp.net/bsimser/archive/2005/07/21/420147.aspx
No comments:
Post a Comment