Hi guys,
I thought, I try to write in English instead of German language now to reach more people. Sorry, for my bad english in advance. :-)
This code gives you a nice way to create new content types with new site columns programmatically in a SharePoint 2010 Feature (C# / Visual Studio 2010). The difficulty is to create a new site column from a managed metadata service. This code took me 1 day, but it works fine now. Have fun with it.
If you have any hints, to improve the code please write a comment below. Thanks. :-)
If you have any hints, to improve the code please write a comment below. Thanks. :-)
const string TermStore = "Managed Metadata Service";
const string TermStoreGroup = "TermStoreGroup";
object[][] Data = new object[][]
{
//new object[] { 0="Content Type Name", 1="Field Name", 2="Field Type", 3="Required", 4="AllowMultipleValues" }
new object[] { "Content Type Name", "Property 1", "TaxonomyFieldType", true, false },
new object[] { "Content Type Name", "Property 2", "TaxonomyFieldType", true, false },
new object[] { "Content Type Name", "Property 3", "TaxonomyFieldType", false, false },
new object[] { "Content Type Name", "Property 4", "TaxonomyFieldType", false, false },
new object[] { "Content Type Name", "Property 5", SPFieldType.Text, false, null },
new object[] { "Content Type Name", "Property 6", SPFieldType.DateTime, true, null }
};
static SPField AddField(string title, SPFieldType type, Boolean required, SPFieldCollection fields)
{
// If the field is not in the collection,
if (!fields.ContainsField(title))
{
// Add it.
fields.Add(title, type, required);
}
return fields.GetField(title);
}
static void AddFieldLink(SPField field, SPContentType contentType)
{
// Is the FieldLink in the collection?
SPFieldLink fieldLink = contentType.FieldLinks[field.Id];
if (fieldLink == null) // No, so add it.
{
fieldLink = new SPFieldLink(field);
contentType.FieldLinks.Add(fieldLink);
}
}
static void AddFieldLink(TaxonomyField field, SPContentType contentType)
{
SPFieldLink fieldLink = new SPFieldLink(field);
if (contentType.FieldLinks[fieldLink.Id] == null)
{
contentType.FieldLinks.Add(fieldLink);
}
}
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
using (SPWeb spWeb = properties.Feature.Parent as SPWeb)
{
foreach (object[] CurrentData in Data)
{
SPContentType contentType;
if (spWeb.ContentTypes[CurrentData[0].ToString()] == null)
{
contentType = new SPContentType(spWeb.ContentTypes["Document"], spWeb.ContentTypes, CurrentData[0].ToString());
contentType.Group = "Content Type Group";
spWeb.ContentTypes.Add(contentType);
spWeb.Update();
}
else
{
contentType = spWeb.ContentTypes[CurrentData[0].ToString()];
}
if (CurrentData[2].ToString() == "TaxonomyFieldType")
{
using (SPSite site = new SPSite(spWeb.Site.ID))
{
TaxonomySession taxonomySession = new TaxonomySession(site);
TermStore termStore = taxonomySession.TermStores[TermStore];
Group termStoreGroup = termStore.Groups[TermStoreGroup];
TaxonomyField NewField;
if (spWeb.Fields.ContainsField(CurrentData[1].ToString()) == false || (spWeb.Fields.ContainsField(CurrentData[1].ToString()) == true && spWeb.Fields[CurrentData[1].ToString()].TypeAsString != CurrentData[2].ToString()))
{
TermSet termSet = termStoreGroup.TermSets[CurrentData[1].ToString()];
NewField = (TaxonomyField)spWeb.Fields.CreateNewField("TaxonomyFieldType", CurrentData[1].ToString());
NewField.SspId = termSet.TermStore.Id;
NewField.Group = termStoreGroup.Name;
NewField.TermSetId = termSet.Id;
NewField.AllowMultipleValues = (Boolean)CurrentData[4];
NewField.TargetTemplate = string.Empty;
NewField.CreateValuesInEditForm = false;
NewField.Open = false;
NewField.AnchorId = Guid.Empty;
NewField.Title = CurrentData[1].ToString();
NewField.Required = (Boolean)CurrentData[3];
spWeb.Fields.Add(NewField);
spWeb.Update();
NewField = (TaxonomyField)spWeb.Fields[CurrentData[1].ToString()];
}
else
{
NewField = (TaxonomyField)spWeb.Fields[CurrentData[1].ToString()];
}
AddFieldLink(NewField, contentType);
}
}
else
{
SPField NewField = AddField(CurrentData[1].ToString(), (SPFieldType)CurrentData[2], (Boolean)CurrentData[3], spWeb.Fields);
AddFieldLink(NewField, contentType);
}
contentType.Update();
}
}
}
catch (Exception exp)
{
throw;
}
}
const string TermStoreGroup = "TermStoreGroup";
object[][] Data = new object[][]
{
//new object[] { 0="Content Type Name", 1="Field Name", 2="Field Type", 3="Required", 4="AllowMultipleValues" }
new object[] { "Content Type Name", "Property 1", "TaxonomyFieldType", true, false },
new object[] { "Content Type Name", "Property 2", "TaxonomyFieldType", true, false },
new object[] { "Content Type Name", "Property 3", "TaxonomyFieldType", false, false },
new object[] { "Content Type Name", "Property 4", "TaxonomyFieldType", false, false },
new object[] { "Content Type Name", "Property 5", SPFieldType.Text, false, null },
new object[] { "Content Type Name", "Property 6", SPFieldType.DateTime, true, null }
};
static SPField AddField(string title, SPFieldType type, Boolean required, SPFieldCollection fields)
{
// If the field is not in the collection,
if (!fields.ContainsField(title))
{
// Add it.
fields.Add(title, type, required);
}
return fields.GetField(title);
}
static void AddFieldLink(SPField field, SPContentType contentType)
{
// Is the FieldLink in the collection?
SPFieldLink fieldLink = contentType.FieldLinks[field.Id];
if (fieldLink == null) // No, so add it.
{
fieldLink = new SPFieldLink(field);
contentType.FieldLinks.Add(fieldLink);
}
}
static void AddFieldLink(TaxonomyField field, SPContentType contentType)
{
SPFieldLink fieldLink = new SPFieldLink(field);
if (contentType.FieldLinks[fieldLink.Id] == null)
{
contentType.FieldLinks.Add(fieldLink);
}
}
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
try
{
using (SPWeb spWeb = properties.Feature.Parent as SPWeb)
{
foreach (object[] CurrentData in Data)
{
SPContentType contentType;
if (spWeb.ContentTypes[CurrentData[0].ToString()] == null)
{
contentType = new SPContentType(spWeb.ContentTypes["Document"], spWeb.ContentTypes, CurrentData[0].ToString());
contentType.Group = "Content Type Group";
spWeb.ContentTypes.Add(contentType);
spWeb.Update();
}
else
{
contentType = spWeb.ContentTypes[CurrentData[0].ToString()];
}
if (CurrentData[2].ToString() == "TaxonomyFieldType")
{
using (SPSite site = new SPSite(spWeb.Site.ID))
{
TaxonomySession taxonomySession = new TaxonomySession(site);
TermStore termStore = taxonomySession.TermStores[TermStore];
Group termStoreGroup = termStore.Groups[TermStoreGroup];
TaxonomyField NewField;
if (spWeb.Fields.ContainsField(CurrentData[1].ToString()) == false || (spWeb.Fields.ContainsField(CurrentData[1].ToString()) == true && spWeb.Fields[CurrentData[1].ToString()].TypeAsString != CurrentData[2].ToString()))
{
TermSet termSet = termStoreGroup.TermSets[CurrentData[1].ToString()];
NewField = (TaxonomyField)spWeb.Fields.CreateNewField("TaxonomyFieldType", CurrentData[1].ToString());
NewField.SspId = termSet.TermStore.Id;
NewField.Group = termStoreGroup.Name;
NewField.TermSetId = termSet.Id;
NewField.AllowMultipleValues = (Boolean)CurrentData[4];
NewField.TargetTemplate = string.Empty;
NewField.CreateValuesInEditForm = false;
NewField.Open = false;
NewField.AnchorId = Guid.Empty;
NewField.Title = CurrentData[1].ToString();
NewField.Required = (Boolean)CurrentData[3];
spWeb.Fields.Add(NewField);
spWeb.Update();
NewField = (TaxonomyField)spWeb.Fields[CurrentData[1].ToString()];
}
else
{
NewField = (TaxonomyField)spWeb.Fields[CurrentData[1].ToString()];
}
AddFieldLink(NewField, contentType);
}
}
else
{
SPField NewField = AddField(CurrentData[1].ToString(), (SPFieldType)CurrentData[2], (Boolean)CurrentData[3], spWeb.Fields);
AddFieldLink(NewField, contentType);
}
contentType.Update();
}
}
}
catch (Exception exp)
{
throw;
}
}
No comments:
Post a Comment