Exemplo n.º 1
0
        protected override void ExecuteCmdlet()
        {
            if (Id.Id == Guid.Empty)
            {
                Id = new GuidPipeBind(Guid.NewGuid());
            }

            if (List != null)
            {
                var list = List.GetList(SelectedWeb);
                if (list == null)
                {
                    throw new PSArgumentException($"No list found with id, title or url '{List}'", "List");
                }
                Field f;
                if (ParameterSetName != ParameterSet_ADDFIELDREFERENCETOLIST)
                {
                    var fieldCI = new FieldCreationInformation(Type)
                    {
                        Id               = Id.Id,
                        InternalName     = InternalName,
                        DisplayName      = DisplayName,
                        Group            = Group,
                        AddToDefaultView = AddToDefaultView
                    };
#if !ONPREMISES
                    if (ClientSideComponentId != null)
                    {
                        fieldCI.ClientSideComponentId = ClientSideComponentId.Id;
                    }
                    if (!string.IsNullOrEmpty(ClientSideComponentProperties))
                    {
                        fieldCI.ClientSideComponentProperties = ClientSideComponentProperties;
                    }
#endif
                    if (Type == FieldType.Choice || Type == FieldType.MultiChoice)
                    {
                        f = list.CreateField <FieldChoice>(fieldCI);
                        ((FieldChoice)f).Choices = choiceFieldParameters.Choices;
                        f.Update();
                        ClientContext.ExecuteQueryRetry();
                    }
                    else if (Type == FieldType.Calculated)
                    {
                        f = list.CreateField <FieldCalculated>(fieldCI);
                        ((FieldCalculated)f).Formula = calculatedFieldParameters.Formula;
                        f.Update();
                        ClientContext.ExecuteQueryRetry();
                    }
                    else
                    {
                        f = list.CreateField(fieldCI);
                    }
                    if (Required)
                    {
                        f.Required = true;
                        f.Update();
                        ClientContext.Load(f);
                        ClientContext.ExecuteQueryRetry();
                    }
                    WriteObject(f);
                }
                else
                {
                    Field field = Field.Field;
                    if (field == null)
                    {
                        if (Field.Id != Guid.Empty)
                        {
                            field = SelectedWeb.Fields.GetById(Field.Id);
                            ClientContext.Load(field);
                            ClientContext.ExecuteQueryRetry();
                        }
                        else if (!string.IsNullOrEmpty(Field.Name))
                        {
                            try
                            {
                                field = SelectedWeb.Fields.GetByInternalNameOrTitle(Field.Name);
                                ClientContext.Load(field);
                                ClientContext.ExecuteQueryRetry();
                            }
                            catch
                            {
                                // Field might be sitecolumn, swallow exception
                            }
                            if (field != null)
                            {
                                var rootWeb = ClientContext.Site.RootWeb;
                                field = rootWeb.Fields.GetByInternalNameOrTitle(Field.Name);
                                ClientContext.Load(field);
                                ClientContext.ExecuteQueryRetry();
                            }
                        }
                    }
                    if (field != null)
                    {
                        list.Fields.Add(field);
                        list.Update();
                        ClientContext.ExecuteQueryRetry();
                    }
                }
            }
            else
            {
                Field f;

                var fieldCI = new FieldCreationInformation(Type)
                {
                    Id               = Id.Id,
                    InternalName     = InternalName,
                    DisplayName      = DisplayName,
                    Group            = Group,
                    AddToDefaultView = AddToDefaultView
                };

#if !ONPREMISES
                if (ClientSideComponentId != null)
                {
                    fieldCI.ClientSideComponentId = ClientSideComponentId.Id;
                }
                if (!string.IsNullOrEmpty(ClientSideComponentProperties))
                {
                    fieldCI.ClientSideComponentProperties = ClientSideComponentProperties;
                }
#endif

                if (Type == FieldType.Choice || Type == FieldType.MultiChoice)
                {
                    f = SelectedWeb.CreateField <FieldChoice>(fieldCI);
                    ((FieldChoice)f).Choices = choiceFieldParameters.Choices;
                    f.Update();
                    ClientContext.ExecuteQueryRetry();
                }
                else if (Type == FieldType.Calculated)
                {
                    f = SelectedWeb.CreateField <FieldCalculated>(fieldCI);
                    ((FieldCalculated)f).Formula = calculatedFieldParameters.Formula;
                    f.Update();
                    ClientContext.ExecuteQueryRetry();
                }
                else
                {
                    f = SelectedWeb.CreateField(fieldCI);
                }

                if (Required)
                {
                    f.Required = true;
                    f.Update();
                    ClientContext.Load(f);
                    ClientContext.ExecuteQueryRetry();
                }
                switch (f.FieldTypeKind)
                {
                case FieldType.DateTime:
                {
                    WriteObject(ClientContext.CastTo <FieldDateTime>(f));
                    break;
                }

                case FieldType.Choice:
                {
                    WriteObject(ClientContext.CastTo <FieldChoice>(f));
                    break;
                }

                case FieldType.Calculated:
                {
                    var calculatedField = ClientContext.CastTo <FieldCalculated>(f);
                    calculatedField.EnsureProperty(fc => fc.Formula);
                    WriteObject(calculatedField);
                    break;
                }

                case FieldType.Computed:
                {
                    WriteObject(ClientContext.CastTo <FieldComputed>(f));
                    break;
                }

                case FieldType.Geolocation:
                {
                    WriteObject(ClientContext.CastTo <FieldGeolocation>(f));
                    break;
                }

                case FieldType.User:
                {
                    WriteObject(ClientContext.CastTo <FieldUser>(f));
                    break;
                }

                case FieldType.Currency:
                {
                    WriteObject(ClientContext.CastTo <FieldCurrency>(f));
                    break;
                }

                case FieldType.Guid:
                {
                    WriteObject(ClientContext.CastTo <FieldGuid>(f));
                    break;
                }

                case FieldType.URL:
                {
                    WriteObject(ClientContext.CastTo <FieldUrl>(f));
                    break;
                }

                case FieldType.Lookup:
                {
                    WriteObject(ClientContext.CastTo <FieldLookup>(f));
                    break;
                }

                case FieldType.MultiChoice:
                {
                    WriteObject(ClientContext.CastTo <FieldMultiChoice>(f));
                    break;
                }

                case FieldType.Number:
                {
                    WriteObject(ClientContext.CastTo <FieldNumber>(f));
                    break;
                }

                default:
                {
                    WriteObject(f);
                    break;
                }
                }
            }
        }
Exemplo n.º 2
0
        protected override void ExecuteCmdlet()
        {
            if (Id.Id == Guid.Empty)
            {
                Id = new GuidPipeBind(Guid.NewGuid());
            }

            if (List != null)
            {
                var   list = List.GetList(SelectedWeb);
                Field f;
                if (ParameterSetName != "FieldRef")
                {
                    var fieldCI = new FieldCreationInformation(Type)
                    {
                        Id               = Id.Id,
                        InternalName     = InternalName,
                        DisplayName      = DisplayName,
                        Group            = Group,
                        AddToDefaultView = AddToDefaultView
                    };

                    if (Type == FieldType.Choice || Type == FieldType.MultiChoice)
                    {
                        f = list.CreateField <FieldChoice>(fieldCI);
                        ((FieldChoice)f).Choices = context.Choices;
                        f.Update();
                        ClientContext.ExecuteQueryRetry();
                    }
                    else
                    {
                        f = list.CreateField(fieldCI);
                    }
                    if (Required)
                    {
                        f.Required = true;
                        f.Update();
                        ClientContext.Load(f);
                        ClientContext.ExecuteQueryRetry();
                    }
                    WriteObject(f);
                }
                else
                {
                    Field field = Field.Field;
                    if (field == null)
                    {
                        if (Field.Id != Guid.Empty)
                        {
                            field = SelectedWeb.Fields.GetById(Field.Id);
                        }
                        else if (!string.IsNullOrEmpty(Field.Name))
                        {
                            field = SelectedWeb.Fields.GetByInternalNameOrTitle(Field.Name);
                        }
                        ClientContext.Load(field);
                        ClientContext.ExecuteQueryRetry();
                    }
                    if (field != null)
                    {
                        list.Fields.Add(field);
                        list.Update();
                        ClientContext.ExecuteQueryRetry();
                    }
                }
            }
            else
            {
                Field f;

                var fieldCI = new FieldCreationInformation(Type)
                {
                    Id               = Id.Id,
                    InternalName     = InternalName,
                    DisplayName      = DisplayName,
                    Group            = Group,
                    AddToDefaultView = AddToDefaultView
                };

                if (Type == FieldType.Choice || Type == FieldType.MultiChoice)
                {
                    f = SelectedWeb.CreateField <FieldChoice>(fieldCI);
                    ((FieldChoice)f).Choices = context.Choices;
                    f.Update();
                    ClientContext.ExecuteQueryRetry();
                }
                else
                {
                    f = SelectedWeb.CreateField(fieldCI);
                }

                if (Required)
                {
                    f.Required = true;
                    f.Update();
                    ClientContext.Load(f);
                    ClientContext.ExecuteQueryRetry();
                }

                WriteObject(f);
            }
        }
Exemplo n.º 3
0
        protected override void ExecuteCmdlet()
        {
            if (Id.Id == Guid.Empty)
            {
                Id = new GuidPipeBind(Guid.NewGuid());
            }

            if (List != null)
            {
                var   list = List.GetList(SelectedWeb);
                Field f;
                if (ParameterSetName != "FieldRef")
                {
                    var fieldCI = new FieldCreationInformation(Type)
                    {
                        Id               = Id.Id,
                        InternalName     = InternalName,
                        DisplayName      = DisplayName,
                        Group            = Group,
                        AddToDefaultView = AddToDefaultView
                    };

                    if (Type == FieldType.Choice || Type == FieldType.MultiChoice)
                    {
                        f = list.CreateField <FieldChoice>(fieldCI);
                        ((FieldChoice)f).Choices = _context.Choices;
                        f.Update();
                        ClientContext.ExecuteQueryRetry();
                    }
                    else
                    {
                        f = list.CreateField(fieldCI);
                    }
                    if (Required)
                    {
                        f.Required = true;
                        f.Update();
                        ClientContext.Load(f);
                        ClientContext.ExecuteQueryRetry();
                    }
                    WriteObject(f);
                }
                else
                {
                    Field field = Field.Field;
                    if (field == null)
                    {
                        if (Field.Id != Guid.Empty)
                        {
                            field = SelectedWeb.Fields.GetById(Field.Id);
                            ClientContext.Load(field);
                            ClientContext.ExecuteQueryRetry();
                        }
                        else if (!string.IsNullOrEmpty(Field.Name))
                        {
                            try
                            {
                                field = SelectedWeb.Fields.GetByInternalNameOrTitle(Field.Name);
                                ClientContext.Load(field);
                                ClientContext.ExecuteQueryRetry();
                            }
                            catch
                            {
                                // Field might be sitecolumn, swallow exception
                            }
                            if (field != null)
                            {
                                var rootWeb = ClientContext.Site.RootWeb;
                                field = rootWeb.Fields.GetByInternalNameOrTitle(Field.Name);
                                ClientContext.Load(field);
                                ClientContext.ExecuteQueryRetry();
                            }
                        }
                    }
                    if (field != null)
                    {
                        list.Fields.Add(field);
                        list.Update();
                        ClientContext.ExecuteQueryRetry();
                    }
                }
            }
            else
            {
                Field f;

                var fieldCI = new FieldCreationInformation(Type)
                {
                    Id               = Id.Id,
                    InternalName     = InternalName,
                    DisplayName      = DisplayName,
                    Group            = Group,
                    AddToDefaultView = AddToDefaultView
                };

                if (Type == FieldType.Choice || Type == FieldType.MultiChoice)
                {
                    f = SelectedWeb.CreateField <FieldChoice>(fieldCI);
                    ((FieldChoice)f).Choices = _context.Choices;
                    f.Update();
                    ClientContext.ExecuteQueryRetry();
                }
                else
                {
                    f = SelectedWeb.CreateField(fieldCI);
                }

                if (Required)
                {
                    f.Required = true;
                    f.Update();
                    ClientContext.Load(f);
                    ClientContext.ExecuteQueryRetry();
                }
                switch (f.FieldTypeKind)
                {
                case FieldType.DateTime:
                {
                    WriteObject(ClientContext.CastTo <FieldDateTime>(f));
                    break;
                }

                case FieldType.Choice:
                {
                    WriteObject(ClientContext.CastTo <FieldChoice>(f));
                    break;
                }

                case FieldType.Calculated:
                {
                    WriteObject(ClientContext.CastTo <FieldCalculated>(f));
                    break;
                }

                case FieldType.Computed:
                {
                    WriteObject(ClientContext.CastTo <FieldComputed>(f));
                    break;
                }

                case FieldType.Geolocation:
                {
                    WriteObject(ClientContext.CastTo <FieldGeolocation>(f));
                    break;
                }

                case FieldType.User:
                {
                    WriteObject(ClientContext.CastTo <FieldUser>(f));
                    break;
                }

                case FieldType.Currency:
                {
                    WriteObject(ClientContext.CastTo <FieldCurrency>(f));
                    break;
                }

                case FieldType.Guid:
                {
                    WriteObject(ClientContext.CastTo <FieldGuid>(f));
                    break;
                }

                case FieldType.URL:
                {
                    WriteObject(ClientContext.CastTo <FieldUrl>(f));
                    break;
                }

                case FieldType.Lookup:
                {
                    WriteObject(ClientContext.CastTo <FieldLookup>(f));
                    break;
                }

                case FieldType.MultiChoice:
                {
                    WriteObject(ClientContext.CastTo <FieldMultiChoice>(f));
                    break;
                }

                case FieldType.Number:
                {
                    WriteObject(ClientContext.CastTo <FieldNumber>(f));
                    break;
                }

                default:
                {
                    WriteObject(f);
                    break;
                }
                }
            }
        }
Exemplo n.º 4
0
        protected override void ExecuteCmdlet()
        {
            if (Id.Id == Guid.Empty)
            {
                Id = new GuidPipeBind(Guid.NewGuid());
            }

            if (List != null)
            {
                List list = this.SelectedWeb.GetList(List);

                Field f = null;
                if (Type == FieldType.Choice || Type == FieldType.MultiChoice)
                {
                    string choicesCAML = GetChoicesCAML(context.Choices);

                    //var fieldXml = GetFieldCAML(DisplayName, InternalName, StaticName, Type, Id.Id, Required, context.Choices, Group);
                    f = list.CreateField(Id.Id, InternalName, Type, DisplayName, Group, choicesCAML);

                    //f = list.CreateField(fieldXml);
                }
                else
                {
                    f = list.CreateField(Id.Id, InternalName, Type, DisplayName, Group);

                    //var fieldXml = GetFieldCAML(DisplayName, InternalName, StaticName, Type, Id.Id, Required, group: Group);
                    //f = list.CreateField(fieldXml);
                }
                if (Required)
                {
                    f.Required = true;
                    f.Update();
                    ClientContext.Load(f);
                    ClientContext.ExecuteQuery();
                }
                WriteObject(f);
            }
            else
            {
                Field f = null;
                if (Type == FieldType.Choice || Type == FieldType.MultiChoice)
                {
                    var choicesCAML = GetChoicesCAML(context.Choices);

                    f = this.SelectedWeb.CreateField(Id.Id, InternalName, Type, DisplayName, Group, choicesCAML);

                    //var fieldXml = GetFieldCAML(DisplayName, InternalName, StaticName, Type, Id.Id, Required, context.Choices, Group);
                    //f = this.SelectedWeb.CreateField(fieldXml);
                }
                else
                {
                    f = this.SelectedWeb.CreateField(Id.Id, InternalName, Type, DisplayName, Group);
                    //  var fieldXml = GetFieldCAML(DisplayName, InternalName, StaticName, Type, Id.Id, Required, group: Group);
                    //  f = this.SelectedWeb.CreateField(fieldXml);
                }

                if (Required)
                {
                    f.Required = true;
                    f.Update();
                    ClientContext.Load(f);
                    ClientContext.ExecuteQuery();
                }

                WriteObject(f);
            }
        }
Exemplo n.º 5
0
        protected override void ExecuteCmdlet()
        {
            if (Id.Id == Guid.Empty)
            {
                Id = new GuidPipeBind(Guid.NewGuid());
            }

            if (List != null)
            {
                var   list = SelectedWeb.GetList(List);
                Field f;
                var   fieldCI = new FieldCreationInformation(Type)
                {
                    Id               = Id.Id,
                    InternalName     = InternalName,
                    DisplayName      = DisplayName,
                    Group            = Group,
                    AddToDefaultView = AddToDefaultView
                };

                if (Type == FieldType.Choice || Type == FieldType.MultiChoice)
                {
                    f = list.CreateField <FieldChoice>(fieldCI);
                    ((FieldChoice)f).Choices = context.Choices;
                    f.Update();
                    ClientContext.ExecuteQueryRetry();
                }
                else
                {
                    f = list.CreateField(fieldCI);
                }
                if (Required)
                {
                    f.Required = true;
                    f.Update();
                    ClientContext.Load(f);
                    ClientContext.ExecuteQueryRetry();
                }
                WriteObject(f);
            }
            else
            {
                Field f;

                var fieldCI = new FieldCreationInformation(Type)
                {
                    Id               = Id.Id,
                    InternalName     = InternalName,
                    DisplayName      = DisplayName,
                    Group            = Group,
                    AddToDefaultView = AddToDefaultView
                };

                if (Type == FieldType.Choice || Type == FieldType.MultiChoice)
                {
                    f = SelectedWeb.CreateField <FieldChoice>(fieldCI);
                    ((FieldChoice)f).Choices = context.Choices;
                    f.Update();
                    ClientContext.ExecuteQueryRetry();
                }
                else
                {
                    f = SelectedWeb.CreateField(fieldCI);
                }

                if (Required)
                {
                    f.Required = true;
                    f.Update();
                    ClientContext.Load(f);
                    ClientContext.ExecuteQueryRetry();
                }

                WriteObject(f);
            }
        }