public SPOContentTypePipeBind(ContentType contentType) { if ((contentType == null)) { throw new ArgumentNullException("The Content Type must be specified."); } _spoContentType = new SPOContentType(contentType); _contentType = contentType; _name = contentType.Name; _contentTypeId = contentType.Id.StringValue; }
internal static SPOContentType GetContentType(CmdletContext ctx, ContentTypeCollection contentTypes, string name) { ctx.Load(contentTypes); ctx.ExecuteQuery(); foreach (ContentType ct in contentTypes) { if (ct.Name.ToLower() == name.ToLower()) { SPOContentType.LoadContentType(ctx, ct); return(new SPOContentType(ct)); } } return(null); }
protected override void ProcessRecord() { base.ProcessRecord(); var ctx = base.Context; Web web = ctx.Site.OpenWeb(Web.Read()); SPOContentType newContentType = null; SPOList list = List.Read(web, false); SPOContentType parentContentType = ContentType.Read(web); if (parentContentType == null) { throw new ArgumentException("Unable to locate the specified parent content type."); } SPOContentType existingContentType = SPOContentType.GetContentType(ctx, list.List.ContentTypes, parentContentType.Name); if (existingContentType != null) { WriteWarning("The content type \"" + parentContentType.Name + "\" already exists within the List."); WriteObject(existingContentType); return; } ContentType ct = list.List.ContentTypes.AddExistingContentType(parentContentType.ContentType); ctx.ExecuteQuery(); SPOContentType.LoadContentType(ctx, ct); newContentType = new SPOContentType(ct); WriteObject(newContentType); }
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); }