本文整理汇总了C#中SPFieldCollection.Add方法的典型用法代码示例。如果您正苦于以下问题:C# SPFieldCollection.Add方法的具体用法?C# SPFieldCollection.Add怎么用?C# SPFieldCollection.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SPFieldCollection
的用法示例。
在下文中一共展示了SPFieldCollection.Add方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AddFieldTo
protected virtual void AddFieldTo(SPFieldCollection fieldCollection, Action<SPField> fieldAction)
{
SPField newField = null;
if (ID != System.Guid.Empty && fieldCollection.Web.AvailableFields.Contains(ID))
{
SPField field = fieldCollection.Web.AvailableFields[ID];
fieldCollection.Add(field);
newField = fieldCollection[ID];
}
else
newField = CreateField(fieldCollection);
if (fieldAction != null)
fieldAction(newField);
if (!string.IsNullOrEmpty(Title))
newField.Title = Title;
if (!string.IsNullOrEmpty(Description))
newField.Description = Description;
newField.Update();
}
示例2: InnerEnsureField
private SPField InnerEnsureField(SPFieldCollection fieldCollection, BaseFieldInfo fieldInfo)
{
SPField field = null;
if (fieldInfo.GetType().Name.StartsWith("MinimalFieldInfo", StringComparison.OrdinalIgnoreCase))
{
// Ensuring a MinimalFieldInfo from its SchemaXML is impossible since the MinimalFieldInfo object
// doesn't hold enough information to completely describe the field metadata.
// Instead, we have to re-use the site column and apply it to the list.
var existingSiteColumn = fieldCollection.Web.Site.RootWeb.Fields.TryGetFieldByStaticName(fieldInfo.InternalName);
if (existingSiteColumn == null)
{
throw new NotSupportedException(
string.Format(
CultureInfo.InvariantCulture,
"Failed to ensure MinimalFieldInfo for field {0} because the pre-requisite Site Column doesn't exist.",
fieldInfo.InternalName));
}
SPList parentList = null;
if (!TryGetListFromFieldCollection(fieldCollection, out parentList))
{
throw new NotSupportedException(
string.Format(
CultureInfo.InvariantCulture,
"Failed to ensure MinimalFieldInfo for field {0}. A MinimalFieldInfo can only be used to ensure a Field on a List's SPFieldCollection, not to re-define an OOTB site column definition.",
fieldInfo.InternalName));
}
fieldCollection.Add(existingSiteColumn);
field = fieldCollection[existingSiteColumn.Id];
}
else
{
// We have a fully-functional/fully-detailed IFieldInfo which should support a conversion to SchemaXML: go ahead and try to add the field.
XElement xmlSchemaForField = this.fieldSchemaHelper.SchemaForField(fieldInfo);
field = this.fieldSchemaHelper.EnsureFieldFromSchema(fieldCollection, xmlSchemaForField);
}
// In some cases, the returned field will not match the one we meant to create or ensure. For example,
// we may have defined a fieldInfo with an InternalName that clashes with an already existing field.
// In such a case, EnsureFieldFromSchema will return us the conflicting/already existing field (not
// the one we mean to ensure).
if (field.Id == fieldInfo.Id && field.InternalName == fieldInfo.InternalName)
{
try
{
var refetchedField = fieldCollection[field.Id];
}
catch (ArgumentException)
{
// in a sneaky edge case, we're dealing with a sub-web's fieldCollection,
// and we actually ensured the column on the root web (instead of on the sub-web).
fieldCollection = fieldCollection.Web.Site.RootWeb.Fields;
}
// Set the field visibility
this.UpdateFieldVisibility(field, fieldInfo);
// Set miscellaneous properties
this.UpdateFieldTypeSpecificProperties(fieldCollection, field, fieldInfo);
// Tiny bit of ugly reflection here: we assume that all implementations of IFieldInfo will
// derive from FieldInfo<T>, which in turn lets us assume a DefaultValue property will always
// be there for us to create our FieldValueInfo (which simply needs an untyped object as value).
FieldValueInfo defaultValueFieldInfo = new FieldValueInfo(fieldInfo, fieldInfo.GetType().GetProperty("DefaultValue").GetValue(fieldInfo));
this.fieldValueWriter.WriteValueToFieldDefault(fieldCollection, defaultValueFieldInfo);
if (!string.IsNullOrEmpty(fieldInfo.DefaultFormula))
{
if (!string.IsNullOrEmpty(field.DefaultValue))
{
// A default value was already specified, so setting a Formula makes no sense.
throw new InvalidOperationException("Failed to ensure field " + fieldInfo.InternalName + " in its entirety because both DefaultFormula and DefaultValue properties were specified. Please only set Formula OR DefaultValue, not both. Also don't forget to clean up the partially created field " + fieldInfo.InternalName + ".");
}
// Setting the DefaultFormula through the SchemaXML doesn't work,
// so let's force it here.
field.DefaultFormula = fieldInfo.DefaultFormula;
}
field.Update();
}
return field;
}
示例3: EnsureFieldFromSchema
/// <summary>
/// Adds a field defined in xml to a collection of fields.
/// </summary>
/// <param name="fieldCollection">The SPField collection.</param>
/// <param name="fieldXml">The field XML schema.</param>
/// <returns>
/// A string that contains the internal name of the new field.
/// </returns>
/// <exception cref="System.ArgumentNullException">
/// fieldCollection
/// or
/// fieldXml
/// </exception>
/// <exception cref="System.FormatException">Invalid xml.</exception>
public SPField EnsureFieldFromSchema(SPFieldCollection fieldCollection, XElement fieldXml)
{
if (fieldCollection == null)
{
throw new ArgumentNullException("fieldCollection");
}
if (fieldXml == null)
{
throw new ArgumentNullException("fieldXml");
}
// Gotta use reflection to figure out if the field collection comes from
// a content type (and thus should be read-only)
bool isContentTypeFieldCollection = (bool)typeof(SPFieldCollection)
.GetField("m_FromContentType", BindingFlags.NonPublic | BindingFlags.Instance)
.GetValue(fieldCollection);
if (isContentTypeFieldCollection)
{
throw new ArgumentException("EnsureField - Impossible to ensure field on Content Type's field collection - it should be considered as read-only. Use IContentTypeHelper.EnsureContentType instead (which will update the content type's FieldRefs collection).");
}
this.logger.Info("Start method 'EnsureField'");
Guid id = Guid.Empty;
string displayName = string.Empty;
string internalName = string.Empty;
string typeName = string.Empty;
// Validate the xml of the field and get its properties
if (this.IsFieldXmlValid(fieldXml, out id, out displayName, out internalName, out typeName))
{
// If its a lookup we need to fix up the xml.
if (this.IsLookup(fieldXml))
{
fieldXml = this.FixLookupFieldXml(fieldCollection.Web, fieldXml);
}
// Check if the field already exists. Skip the creation if so.
if (!this.FieldExists(fieldCollection, internalName, id))
{
// We want to create the field: if we're trying to add field on a list field collection,
// then chances are the field is already defined on the parent root web (we actually enforce
// this in the calling FieldHelper). In such a case, we need to re-use the existing field definition,
// because using .AddFieldAsXml directly on the list field collection would cause a field
// with an InternalName==ParentRootWebFieldDisplayName (weird bug, really - using AddFieldAsXml
// on a list's SPFieldCollection is just a bad idea: use the already provisioned site column
// whenever possible).
string addedInternalName = string.Empty;
SPField parentRootWebExistingField = null;
SPWeb rootWeb = fieldCollection.Web.Site.RootWeb;
SPFieldCollection rootWebFields = rootWeb.Fields;
if (!this.FieldExists(rootWebFields, internalName, id))
{
if (fieldCollection.List == null && fieldCollection.Web.ID == rootWeb.ID)
{
rootWebFields = fieldCollection;
}
// Add on Root Web if it wasn't done before
addedInternalName = rootWebFields.AddFieldAsXml(fieldXml.ToString(), false, SPAddFieldOptions.Default);
}
// Re-use the parent field definition
parentRootWebExistingField = rootWebFields[id];
// Make sure we're adding the field on a List or on a different web than the root web:
// we don't want to ensure the field twice on the root web
if (fieldCollection.List != null)
{
if (parentRootWebExistingField.EnforceUniqueValues)
{
// Before we ensure the site column on a list, gotta make sure
// that, on a list, EnforceUniqueValues=TRUE IF-AND-ONLY-IF Indexed=TRUE
parentRootWebExistingField.Indexed = true;
}
addedInternalName = fieldCollection.Add(parentRootWebExistingField);
// Then update the list column with the new list-specific or web-specfic definition
var createdField = this.fieldLocator.GetFieldById(fieldCollection, id);
createdField.SchemaXml = fieldXml.ToString(); // TODO: we should probably do a more granular update (property by property,
// only when needed) instead of brutally overwriting the schema XML like this.
createdField.Update();
}
else if (fieldCollection.Web.ID != rootWeb.ID)
//.........这里部分代码省略.........
示例4: CreateField
protected virtual SPField CreateField(SPFieldCollection fieldCollection)
{
fieldCollection.Add(Name, Type, Required);
return fieldCollection[Name];
}
示例5: AddField
public string AddField(SPFieldCollection fieldCollection, SPField field)
{
return fieldCollection.Add(field);
}