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

            if (List != null)
            {
                var list = List.GetList(CurrentWeb);
                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,
                        InternalName     = InternalName,
                        DisplayName      = DisplayName,
                        Group            = Group,
                        AddToDefaultView = AddToDefaultView
                    };

                    if (AddToAllContentTypes)
                    {
                        fieldCI.FieldOptions |= AddFieldOptions.AddToAllContentTypes;
                    }

                    if (ClientSideComponentId != null)
                    {
                        fieldCI.ClientSideComponentId = ClientSideComponentId;
                    }
                    if (!string.IsNullOrEmpty(ClientSideComponentProperties))
                    {
                        fieldCI.ClientSideComponentProperties = ClientSideComponentProperties;
                    }
                    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)
                    {
                        // Either set the ResultType as input parameter or set it to the default Text
                        if (!string.IsNullOrEmpty(calculatedFieldParameters.ResultType))
                        {
                            fieldCI.AdditionalAttributes = new List <KeyValuePair <string, string> >()
                            {
                                new KeyValuePair <string, string>("ResultType", calculatedFieldParameters.ResultType)
                            };
                        }
                        else
                        {
                            fieldCI.AdditionalAttributes = new List <KeyValuePair <string, string> >()
                            {
                                new KeyValuePair <string, string>("ResultType", "Text")
                            };
                        }

                        fieldCI.AdditionalChildNodes = new List <KeyValuePair <string, string> >()
                        {
                            new KeyValuePair <string, string>("Formula", calculatedFieldParameters.Formula)
                        };

                        f = list.CreateField <FieldCalculated>(fieldCI);
                    }
                    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 = CurrentWeb.Fields.GetById(Field.Id);
                            ClientContext.Load(field);
                            ClientContext.ExecuteQueryRetry();
                        }
                        else if (!string.IsNullOrEmpty(Field.Name))
                        {
                            try
                            {
                                field = CurrentWeb.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,
                    InternalName     = InternalName,
                    DisplayName      = DisplayName,
                    Group            = Group,
                    AddToDefaultView = AddToDefaultView
                };

                if (ClientSideComponentId != null)
                {
                    fieldCI.ClientSideComponentId = ClientSideComponentId;
                }
                if (!string.IsNullOrEmpty(ClientSideComponentProperties))
                {
                    fieldCI.ClientSideComponentProperties = ClientSideComponentProperties;
                }

                if (Type == FieldType.Choice || Type == FieldType.MultiChoice)
                {
                    f = CurrentWeb.CreateField <FieldChoice>(fieldCI);
                    ((FieldChoice)f).Choices = choiceFieldParameters.Choices;
                    f.Update();
                    ClientContext.ExecuteQueryRetry();
                }
                else if (Type == FieldType.Calculated)
                {
                    f = CurrentWeb.CreateField <FieldCalculated>(fieldCI);
                    ((FieldCalculated)f).Formula = calculatedFieldParameters.Formula;
                    f.Update();
                    ClientContext.ExecuteQueryRetry();
                }
                else
                {
                    f = CurrentWeb.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()
        {
            Field f;

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

                f = list.CreateField(FieldXml);
            }
            else
            {
                f = CurrentWeb.CreateField(FieldXml);
            }
            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;
            }
            }
        }