1

I want to create some site columns and content types programmatically, since I need to create some site columns with the same display name as existing columns, and this can not be done using the CA UI. so can anyone advice what are the approaches available, I found that I can use:-

  • farm or sandboxed solution
  • app
  • or using power shell

so when/why to use any of these approaches? second question let say I use any of the above three approaches to create a new site column, will this affect the final column, or at the end all of these approaches will result in creating exactly the same column ?

2
  • do you just want the same display name? – Gwny Jul 20 '15 at 12:07
  • 1
    because in this case you can change the displayname after creating a wrong one and the internal name will be different – Gwny Jul 20 '15 at 12:20
0

You can create site columns using c# with same display name and distinct internal name. The important thing here is internal name. See this code:

string internalName = site.RootWeb.Fields.Add("XXX", SPFieldType.DateTime, true);
SPFieldDateTime newField = (SPFieldDateTime)site.RootWeb.Fields.GetFieldByInternalName(internalName);
newField.Title = "existing display name";
newField.DisplayFormat = SPDateTimeFieldFormatType.DateOnly;
newField.Description = "my description";
newField.Group = "My group";
newField.Update();

This code is for create Content Type:

SPContentType newCT = new SPContentType(site.RootWeb.ContentTypes["Element"], site.RootWeb.ContentTypes, "CT Name");
site.RootWeb.ContentTypes.Add(newCT);
newCT.Group = "My group";
newCT.Update();

And this code to add the site columns to the content type:

SPContentType newCT= sitie.RootWeb.ContentTypes["CT Name"];
// Note you use internal name
SPField newCol = site.RootWeb.Fields.GetFieldByInternalName("XXX");
SPFieldLink newColLink = new SPFieldLink(newCol);
newCT.FieldLinks.Add(newColLink);
newCT.Update(true);
0

You can also create a SiteColumn with a different name first and then change the displayname with powershell:

$web = get-spweb http://yourweb
$web.Fields["InitialName"].Title = "NewName"
$web.Fields["InitialName"].Update()

In this case you don't need to deploy a solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy