protected override void ProcessRecord() { base.ProcessRecord(); var ctx = base.Context; Web web = ctx.Site.OpenWeb(Web.Read()); SPOField fieldToAdd = Identity.Read(web); if (fieldToAdd == null) { WriteError(new ErrorRecord(new ArgumentOutOfRangeException("The specified field could not be found! The field was not added to the list."), null, ErrorCategory.InvalidData, Identity)); return; } SPOList list = List.Read(web, false); SPOField existingField = SPOField.GetField(ctx, list.List.Fields, fieldToAdd.InternalName); if (existingField != null) { WriteWarning("The specified field already exists within the list."); WriteObject(existingField); return; } Field field = list.List.Fields.Add(fieldToAdd.Field); ctx.Load(field); ctx.ExecuteQuery(); WriteObject(new SPOField(field)); }
protected override void ProcessRecord() { base.ProcessRecord(); var ctx = base.Context; Web web = ctx.Site.OpenWeb(Web.Read()); XmlDocument schemaXmlDoc = SchemaXml.Read(); string xml = schemaXmlDoc.OuterXml; if (ParameterSetName == "Web") { SPOField existingField = SPOField.GetField(ctx, web.AvailableFields, schemaXmlDoc); if (existingField != null) { WriteWarning("The field " + schemaXmlDoc.DocumentElement.GetAttribute("Name") + " already exists!"); WriteObject(existingField); return; } Field field = web.Fields.AddFieldAsXml(xml, false, Options); ctx.Load(field); ctx.ExecuteQuery(); WriteObject(new SPOField(field)); } else if (ParameterSetName == "List") { SPOList list = List.Read(web, false); SPOField existingField = SPOField.GetField(ctx, list.List.Fields, schemaXmlDoc); if (existingField != null) { WriteWarning("The field " + schemaXmlDoc.DocumentElement.GetAttribute("Name") + " already exists!"); WriteObject(existingField); return; } Field field = list.List.Fields.AddFieldAsXml(xml, AddToDefaultView, Options); ctx.Load(field); ctx.ExecuteQuery(); WriteObject(new SPOField(field)); } }
private void AddFieldLinks(FieldCollection fieldCollection, bool isList, params string[] fieldsToAdd) { List <SPOField> fields = new List <SPOField>(); if (fieldsToAdd == null) { return; } var ctx = SPOSiteContext.CurrentSiteContext.Context; foreach (string fieldName in fieldsToAdd) { SPOField existingField = SPOField.GetField(ctx, fieldCollection, fieldName); if (existingField == null) { throw new ArgumentOutOfRangeException("Unable to locate field " + fieldName + "."); } else { fields.Add(existingField); } } if (fields.Count == 0) { return; } foreach (SPOField field in fields) { FieldLinkCreationInformation flci = new FieldLinkCreationInformation(); flci.Field = field.Field; this.ContentType.FieldLinks.Add(flci); } if (fields.Count > 0) { this.ContentType.Update(!isList); ctx.ExecuteQuery(); } }
protected override void ProcessRecord() { base.ProcessRecord(); var ctx = base.Context; Web web = ctx.Site.OpenWeb(Web.Read()); SPOContentType newContentType = null; SPOContentType parentContentType = ParentContentType.Read(web); if (parentContentType == null) { throw new ArgumentException("Unable to locate the specified parent content type."); } SPOContentType existingContentType = SPOContentType.GetContentType(ctx, web.AvailableContentTypes, Name); if (existingContentType != null) { WriteWarning("The content type \"" + Name + "\" already exists within the Site."); WriteObject(existingContentType); return; } List <SPOField> fields = new List <SPOField>(); if (FieldsToAdd != null) { foreach (string fieldName in FieldsToAdd) { SPOField existingField = SPOField.GetField(ctx, web.AvailableFields, fieldName); if (existingField == null) { WriteError(new ErrorRecord(new ArgumentOutOfRangeException("Unable to locate field " + fieldName + ". Content Type will not be created."), null, ErrorCategory.InvalidData, web.AvailableFields)); } else { fields.Add(existingField); } } if (fields.Count != FieldsToAdd.Length) { return; } } var ctli = new ContentTypeCreationInformation(); ctli.Description = Description; ctli.Group = Group; ctli.Name = Name; ctli.ParentContentType = parentContentType.ContentType; ContentType ct = web.ContentTypes.Add(ctli); ctx.ExecuteQuery(); SPOContentType.LoadContentType(ctx, ct); newContentType = new SPOContentType(ct); foreach (SPOField field in fields) { FieldLinkCreationInformation flci = new FieldLinkCreationInformation(); flci.Field = field.Field; newContentType.ContentType.FieldLinks.Add(flci); } if (fields.Count > 0) { newContentType.ContentType.Update(true); ctx.ExecuteQuery(); } WriteObject(newContentType); }