Пример #1
0
        public override void Provision()
        {
            if (_list != null && Model != null && Model.Context != null && Model.Context.Context != null)
            {
                var  context = Model.Context.Context;
                Web  web     = context.Web;
                List list    = _list.Url != null
                        ? context.Web.GetList($"{ Model.Context.SiteUrl.TrimEnd('/')}/{_list.Url.TrimStart('/')}")
                        : (_list.Title != null ? context.Web.Lists.GetByTitle(_list.Title) : null);

                context.Load(list);
                try
                {
                    context.ExecuteQuery();
                    OnProvisioned?.Invoke(this, list);
                    return;
                }
                catch (Exception)
                {
                    list = null;
                }

                var newList = new ListCreationInformation()
                {
                    Title        = _list.Title,
                    Url          = _list.Url,
                    TemplateType = (int)_list.Type
                };

                list = web.Lists.Add(newList);
                OnProvisioning?.Invoke(this, list);
                list.Update();
                context.Load(list);
                context.ExecuteQuery();
                OnProvisioned?.Invoke(this, list);
            }
        }
        public override void Provision()
        {
            if (_contentType != null && Model != null && Model.Context != null && Model.Context.Context != null)
            {
                var         context           = Model.Context.Context;
                Web         web               = context.Web;
                List        list              = null;
                ContentType parentContentType = null;
                ContentType contentType       = null;

                if (!string.IsNullOrEmpty(_contentType.ParentId))
                {
                    parentContentType = web.AvailableContentTypes.GetById(_contentType.ParentId);
                }

                if (_list != null)
                {
                    list = _list.Url != null
                                         ? context.Web.GetList($"{ Model.Context.SiteUrl.TrimEnd('/')}/{_list.Url.TrimStart('/')}")
                                         : (_list.Title != null ? context.Web.Lists.GetByTitle(_list.Title) : null);
                }

                var newContentType = new ContentTypeCreationInformation()
                {
                    Id                = _contentType.Id,
                    Name              = _contentType.Name,
                    Group             = _contentType.Group,
                    ParentContentType = parentContentType
                };

                ContentType webContentType  = null;
                ContentType listContentType = null;
                string      ctName          = _contentType.Name;
                if (string.IsNullOrEmpty(ctName))
                {
                    string ctId = _contentType.Id;
                    if (!string.IsNullOrEmpty(ctId))
                    {
                        IEnumerable <ContentType> webContentTypes  = context.LoadQuery(web.AvailableContentTypes.Where(ct => ct.Id.StringValue == ctId));
                        IEnumerable <ContentType> listContentTypes = null;
                        if (list != null)
                        {
                            listContentTypes = context.LoadQuery(list.ContentTypes.Where(ct => ct.Id.StringValue == ctId || ct.Parent.Id.StringValue == ctId));
                        }

                        context.ExecuteQuery();

                        webContentType = webContentTypes.FirstOrDefault();
                        if (listContentTypes != null)
                        {
                            listContentType = listContentTypes.FirstOrDefault();
                        }
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    IEnumerable <ContentType> webContentTypes  = context.LoadQuery(web.AvailableContentTypes.Where(ct => ct.Name == ctName));
                    IEnumerable <ContentType> listContentTypes = null;
                    if (list != null)
                    {
                        listContentTypes = context.LoadQuery(list.ContentTypes.Where(ct => ct.Name == ctName));
                    }

                    context.ExecuteQuery();

                    webContentType = webContentTypes.FirstOrDefault();
                    if (listContentTypes != null)
                    {
                        listContentType = listContentTypes.FirstOrDefault();
                    }
                }

                if (listContentType != null)
                {
                    OnProvisioned?.Invoke(this, listContentType);
                    return;
                }

                if (list != null)
                {
                    if (webContentType != null)
                    {
                        contentType = list.ContentTypes.AddExistingContentType(webContentType);
                    }
                    else
                    {
                        contentType = list.ContentTypes.Add(newContentType);
                        OnProvisioning?.Invoke(this, contentType);
                        contentType.Update(false);
                    }
                }
                else
                {
                    if (webContentType != null)
                    {
                        OnProvisioned?.Invoke(this, webContentType);
                        return;
                    }
                    else
                    {
                        contentType = web.ContentTypes.Add(newContentType);
                        OnProvisioning?.Invoke(this, contentType);
                        contentType.Update(false);
                    }
                }
                if (contentType != null)
                {
                    context.Load(contentType);
                    context.ExecuteQuery();
                    OnProvisioned?.Invoke(this, contentType);
                }
            }
        }
Пример #3
0
        public override void Provision(bool forceOverwrite, ProvisionLevel level)
        {
            if (List != null && Model != null && Model.Context != null && Model.Context.Context != null)
            {
                //if (level == ProvisionLevel.Web) return;
                if (List.Behavior == ProvisionBehavior.None)
                {
                    return;
                }
                var  context = Model.Context.Context;
                Web  web     = context.Web;
                List list    = null;
                if (List.Id != default)
                {
                    list = web.Lists.GetById(List.Id);
                }
                else if (List.Url != null)
                {
                    list = web.GetList($"{ new Uri(Model.Context.SiteUrl).LocalPath.TrimEnd('/')}/{List.Url.TrimStart('/')}");
                }
                else if (!string.IsNullOrEmpty(List.Title))
                {
                    list = web.Lists.GetByTitle(List.Title);
                }
                if (list != null)
                {
                    context.Load(list);
                    try
                    {
                        context.ExecuteQuery();
                    }
                    catch (Exception)
                    {
                        list = null;
                    }
                }
                if (list != null)
                {
                    if (forceOverwrite || List.Behavior == ProvisionBehavior.Overwrite)
                    {
                        if (list.Title != List.Title)
                        {
                            list.Title = List.Title;
                            OnProvisioning?.Invoke(this, list);
                            list.Update();
                        }
                        //context.Load(list);
                        context.ExecuteQuery();
                        OnProvisioned?.Invoke(this, list);
                        return;
                    }
                }
                else
                {
                    var newList = new ListCreationInformation()
                    {
                        Title             = List.Title,
                        Url               = List.Url,
                        TemplateType      = (int)List.Type,
                        TemplateFeatureId = List.TemplateFeatureId
                    };

                    list = web.Lists.Add(newList);
                    OnProvisioning?.Invoke(this, list);
                    list.Update();
                    context.Load(list);
                    context.ExecuteQuery();
                }
                List.Id = list.Id;
                OnProvisioned?.Invoke(this, list);
            }
        }
Пример #4
0
        public override void Provision(bool forceOverwrite, ProvisionLevel level)
        {
            if (ContentType != null && Model != null && Model.Context != null && Model.Context.Context != null)
            {
                if (ContentType.Behavior == ProvisionBehavior.None)
                {
                    return;
                }

                if (level != ProvisionLevel.Default)
                {
                    //ContentType.Level = level;
                }
                else
                {
                    level = ContentType.Level;
                    if (level == ProvisionLevel.Default)
                    {
                        level = List != null ? ProvisionLevel.List : ProvisionLevel.Web;
                    }
                }

                var         context           = Model.Context.Context;
                Web         web               = context.Web;
                List        list              = null;
                ContentType parentContentType = null;
                ContentType contentType       = null;
                ContentType webContentType    = null;
                ContentType listContentType   = null;

                if (string.IsNullOrEmpty(ContentType.Id) && !string.IsNullOrEmpty(ContentType.ParentId))
                {
                    parentContentType = web.AvailableContentTypes.GetById(ContentType.ParentId);
                    //context.Load(parentContentType);
                    //context.ExecuteQuery();
                }
                if (List != null && List.Behavior != ProvisionBehavior.None)
                {
                    if (List.Id != default)
                    {
                        list = context.Web.Lists.GetById(List.Id);
                    }
                    else if (List.Url != null)
                    {
                        list = context.Web.GetList/*ByUrl*/ ($"{ new Uri(Model.Context.SiteUrl).LocalPath.TrimEnd('/')}/{List.Url.TrimStart('/')}");
                    }
                    else if (!string.IsNullOrEmpty(List.Title))
                    {
                        list = context.Web.Lists.GetByTitle(List.Title);
                    }
                }
                //if (list != null)
                //{
                //  context.Load(list);
                //  try
                //  {
                //    context.ExecuteQuery();
                //  }
                //  catch (Exception ex)
                //  {
                //    list = null;
                //  }
                //}

                var newContentType = !string.IsNullOrEmpty(ContentType.Name)
                    ? new ContentTypeCreationInformation()
                {
                    Id                = ContentType.Id,
                    Name              = ContentType.Name,
                    Group             = ContentType.Group,
                    ParentContentType = parentContentType
                } : null;

                string ctName = ContentType.Name;
                string ctId   = ContentType.Id;

                if (!string.IsNullOrEmpty(ctId))
                {
                    if (level == ProvisionLevel.List && list != null)
                    {
                        var listContentTypes = context.LoadQuery(list.ContentTypes.Where(ct => ct.Id.StringValue == ctId || ct.Parent.Id.StringValue == ctId));
                        try
                        {
                            context.ExecuteQuery();
                            listContentType = listContentTypes.FirstOrDefault();
                            if (listContentType != null)
                            {
                                ctName = listContentType.Name;
                            }
                        }
                        catch
                        {
                            listContentType = null;
                        }

                        if ((forceOverwrite || ContentType.Behavior == ProvisionBehavior.Overwrite) && listContentType != null)
                        {
                            if (!listContentType.ReadOnly && !listContentType.Sealed)
                            {
                                if (listContentType.Name != ContentType.Name || (!string.IsNullOrEmpty(ContentType.Group) && listContentType.Group != ContentType.Group))
                                {
                                    listContentType.Name  = ContentType.Name;
                                    listContentType.Group = ContentType.Group;
                                    OnProvisioning?.Invoke(this, listContentType);
                                    listContentType.Update(false);
                                }
                            }

                            if (!listContentType.Sealed)
                            {
                                OnProvisioned?.Invoke(this, listContentType);
                            }
                            return;
                        }
                    }
                    if (listContentType == null)
                    {
                        webContentType = web.AvailableContentTypes.GetById(ctId);
                        context.Load(webContentType);
                        try
                        {
                            context.ExecuteQuery();
                            ctName = webContentType.Name;
                        }
                        catch
                        {
                            webContentType = null;
                        }
                    }
                }
                else if (!string.IsNullOrEmpty(ctName))
                {
                    if (level == ProvisionLevel.List && list != null)
                    {
                        var listContentTypes = context.LoadQuery(list.ContentTypes.Where(ct => ct.Name == ctName));
                        try
                        {
                            context.ExecuteQuery();
                            listContentType = listContentTypes.FirstOrDefault();
                            if (listContentType != null)
                            {
                                ctName = listContentType.Name;
                            }
                        }
                        catch
                        {
                            listContentType = null;
                        }

                        if ((forceOverwrite || ContentType.Behavior == ProvisionBehavior.Overwrite) && listContentType != null)
                        {
                            if (!listContentType.ReadOnly && !listContentType.Sealed)
                            {
                                if (listContentType.Name != ContentType.Name || (!string.IsNullOrEmpty(ContentType.Group) && listContentType.Group != ContentType.Group))
                                {
                                    listContentType.Name  = ContentType.Name;
                                    listContentType.Group = ContentType.Group;
                                    OnProvisioning?.Invoke(this, listContentType);
                                    listContentType.Update(false);
                                }
                            }
                            if (!listContentType.Sealed)
                            {
                                OnProvisioned?.Invoke(this, listContentType);
                            }
                            return;
                        }
                    }
                    if (listContentType == null)
                    {
                        var webContentTypes = context.LoadQuery(web.AvailableContentTypes.Where(ct => ct.Name == ctName));
                        try
                        {
                            context.ExecuteQuery();
                            webContentType = webContentTypes.FirstOrDefault();
                        }
                        catch
                        {
                            webContentType = null;
                        }
                    }
                }

                if (level == ProvisionLevel.Web)
                {
                    if ((forceOverwrite || ContentType.Behavior == ProvisionBehavior.Overwrite) && webContentType != null /*&& webContentType.Group == ContentType.Group*/)
                    {
                        if (!webContentType.ReadOnly && !webContentType.Sealed)
                        {
                            if (webContentType.Name != ContentType.Name || (!string.IsNullOrEmpty(ContentType.Group) && webContentType.Group != ContentType.Group))
                            {
                                webContentType.Name  = ContentType.Name;
                                webContentType.Group = ContentType.Group;
                                OnProvisioning?.Invoke(this, webContentType);
                                webContentType.Update(false);
                            }
                        }
                        if (!webContentType.Sealed)
                        {
                            OnProvisioned?.Invoke(this, webContentType);
                        }
                        return;
                    }
                    else if (webContentType == null && newContentType != null)
                    {
                        contentType = web.ContentTypes.Add(newContentType);
                        OnProvisioning?.Invoke(this, contentType);
                        contentType.Update(false);
                        webContentType = contentType;
                    }
                }
                else if (level == ProvisionLevel.List)
                {
                    if (list != null && List.Behavior != ProvisionBehavior.None)
                    {
                        if (webContentType != null)
                        {
                            contentType = list.ContentTypes.AddExistingContentType(webContentType);
                        }
                        else if (listContentType == null && newContentType != null)
                        {
                            contentType = list.ContentTypes.Add(newContentType);
                            OnProvisioning?.Invoke(this, contentType);
                            contentType.Update(false);
                        }
                    }
                }

                if (contentType != null)
                {
                    context.Load(contentType);
                    context.ExecuteQuery();
                    //if (newContentType != null)
                    //{
                    OnProvisioned?.Invoke(this, contentType);
                    //}
                }
            }
        }
Пример #5
0
        public override void Provision(bool forceOverwrite, ProvisionLevel level)
        {
            if (Field != null && Model != null && Model.Context != null && Model.Context.Context != null)
            {
                if (Field.Behavior == ProvisionBehavior.None)
                {
                    return;
                }

                if (level != ProvisionLevel.Default)
                {
                    //Field.Level = level;
                }
                else if (Field.Level == ProvisionLevel.Default)
                {
                    if (ContentType != null)
                    {
                        level = ContentType.Level;
                    }
                    if (level == ProvisionLevel.Default)
                    {
                        level = List != null && List.Behavior != ProvisionBehavior.None ? ProvisionLevel.List : ProvisionLevel.Web;
                    }
                }

                if (Field.Behavior == ProvisionBehavior.Default)
                {
                    if (level == ProvisionLevel.List)
                    {
                        if (List != null)
                        {
                            Field.Behavior = List.Behavior;
                        }
                    }
                    if (Field.Behavior == ProvisionBehavior.Default && ContentType != null)
                    {
                        Field.Behavior = ContentType.Behavior;
                    }
                }

                if (Field.DataType == FieldType.ContentTypeId ||
                    Field.DataType == FieldType.Counter ||
                    Field.DataType == FieldType.Computed ||
                    Field.DataType == FieldType.File)
                {
                    return;
                }

                var   context = Model.Context.Context;
                Web   web     = context.Web;
                List  list    = null;
                Field field   = null;
                if (List != null && List.Behavior != ProvisionBehavior.None)
                {
                    if (List.Id != default)
                    {
                        list = context.Web.Lists.GetById(List.Id);
                    }
                    else if (List.Url != null)
                    {
                        list = context.Web.GetList($"{ new Uri(Model.Context.SiteUrl).LocalPath.TrimEnd('/')}/{List.Url.TrimStart('/')}");
                    }
                    else if (!string.IsNullOrEmpty(List.Title))
                    {
                        list = context.Web.Lists.GetByTitle(List.Title);
                    }
                    //if (list != null)
                    //{
                    //  context.Load(list);
                    //  try
                    //  {
                    //    context.ExecuteQuery();
                    //  }
                    //  catch (Exception ex)
                    //  {
                    //    list = null;
                    //  }
                    //}
                }

                FieldCollection fields = null;

                field  = web.AvailableFields.GetByInternalNameOrTitle(Field.Name);
                fields = web.Fields;

                context.Load(field);
                try
                {
                    context.ExecuteQuery();
                }
                catch (Exception)
                {
                    field = null;
                }

                if (level == ProvisionLevel.List && list != null)
                {
                    Field existField = list.Fields.GetByInternalNameOrTitle(Field.Name);
                    context.Load(existField);
                    try
                    {
                        context.ExecuteQuery();
                    }
                    catch (Exception ex)
                    {
                        existField = null;
                    }
                    if (existField != null)
                    {
                        field = existField;
                    }
                    else
                    {
                        field = null;
                    }
                    fields = list.Fields;
                }

                if (!forceOverwrite /*&& !Field.Overwrite*/ && Field.Behavior != ProvisionBehavior.Overwrite && field != null)
                {
                    OnProvisioned?.Invoke(this, field);
                    return;
                }

                if (fields != null)
                {
                    if (Field.DataType == FieldType.Calculated)
                    {
                        string formula    = null;
                        string fieldRefs  = null;
                        string newFormula = null;

                        if (typeof(CalculatedFieldAttribute).IsAssignableFrom(Field.GetType()))
                        {
                            fieldRefs = (Field as CalculatedFieldAttribute).FieldRefs == null
                                ? null
                                : string.Join("", (Field as CalculatedFieldAttribute).FieldRefs.Select(fieldRef => new Caml.CamlFieldRef()
                            {
                                Name = fieldRef
                            }.ToString()));
                            formula = (Field as CalculatedFieldAttribute).Formula;

                            if (!string.IsNullOrEmpty(fieldRefs) && !string.IsNullOrEmpty(formula))
                            {
                                if (level == ProvisionLevel.List)
                                {
                                    var refFields = new List <Field>();
                                    foreach (string fieldName in (Field as CalculatedFieldAttribute).FieldRefs)
                                    {
                                        var refField = (level == ProvisionLevel.Web ? web.AvailableFields : fields).GetByInternalNameOrTitle(fieldName);
                                        context.Load(refField, f => f.Title, f => f.InternalName, f => f.Id);
                                        refFields.Add(refField);
                                    }
                                    context.ExecuteQuery();
                                    foreach (Field refField in refFields)
                                    {
                                        newFormula = formula.Replace($"[{refField.InternalName}]", $"[{refField.Title}]");
                                    }
                                }
                            }
                        }
                        if (field == null)
                        {
                            string fieldXml = $"<Field Type='{Field.DataType}' Name='{Field.Name}' StaticName='{Field.Name}' DisplayName='{Field.Title ?? Field.Name}' ResultType='{{2}}'><Formula>{{0}}</Formula><FieldRefs>{{1}}</FieldRefs></Field>";
                            fieldXml = string.Format(fieldXml, System.Security.SecurityElement.Escape(newFormula), fieldRefs, (Field as CalculatedFieldAttribute).ResultType);

                            field = fields.AddFieldAsXml(fieldXml, true, AddFieldOptions.AddFieldInternalNameHint);
                        }

                        if (Field is CalculatedFieldAttribute)
                        {
                            (Field as CalculatedFieldAttribute).Formula = newFormula;
                        }

                        try
                        {
                            field = ApplyField(field);
                        }
                        finally
                        {
                            if (Field is CalculatedFieldAttribute)
                            {
                                (Field as CalculatedFieldAttribute).Formula = formula;
                            }
                        }

                        OnProvisioning?.Invoke(this, field);
                    }
                    else
                    {
                        if (Field.DataType == FieldType.Lookup)
                        {
                            if (_valueType != null &&
                                (typeof(ISpEntityLookup).IsAssignableFrom(_valueType) || typeof(ISpEntityLookupCollection).IsAssignableFrom(_valueType)) ||
                                typeof(IListItemEntity).IsAssignableFrom(_valueType))
                            {
                                bool allowMultipleValues = false;
                                if (typeof(LookupFieldAttribute).IsAssignableFrom(Field.GetType()))
                                {
                                    allowMultipleValues = (Field as LookupFieldAttribute).IsMultiple;
                                }
                                if (field == null)
                                {
                                    string fieldXml = allowMultipleValues
                                      ? $"<Field Type='LookupMulti' Name='{Field.Name}' StaticName='{Field.Name}' DisplayName='{Field.Title ?? Field.Name}' Mult='TRUE' />"
                                      : $"<Field Type='{Field.DataType}' Name='{Field.Name}' StaticName='{Field.Name}' DisplayName='{Field.Title ?? Field.Name}' />";
                                    field = fields.AddFieldAsXml(fieldXml, true, AddFieldOptions.AddFieldInternalNameHint);
                                }
                                field = ApplyField(field);
                                OnProvisioning?.Invoke(this, field);
                            }
                            else
                            {
                                field = null;
                            }
                        }
                        else if (Field.DataType == FieldType.User)
                        {
                            bool allowMultipleValues = false;
                            if (typeof(LookupFieldAttribute).IsAssignableFrom(Field.GetType()))
                            {
                                allowMultipleValues = (Field as LookupFieldAttribute).IsMultiple;
                            }
                            if (field == null)
                            {
                                string fieldXml = allowMultipleValues
                                  ? $"<Field Type='UserMulti' Name='{Field.Name}' StaticName='{Field.Name}' DisplayName='{Field.Title ?? Field.Name}' Mult='TRUE' />"
                                  : $"<Field Type='{Field.DataType}' Name='{Field.Name}' StaticName='{Field.Name}' DisplayName='{Field.Title ?? Field.Name}' />";
                                field = fields.AddFieldAsXml(fieldXml, true, AddFieldOptions.AddFieldInternalNameHint);
                            }
                            field = ApplyField(field);
                            OnProvisioning?.Invoke(this, field);
                        }
                        else if ((Field.DataType == FieldType.Choice || Field.DataType == FieldType.MultiChoice) && _valueType.IsEnum)
                        {
                            if (field == null)
                            {
                                string fieldXml = $"<Field Type='{Field.DataType}' Name='{Field.Name}' StaticName='{Field.Name}' DisplayName='{Field.Title ?? Field.Name}' />";
                                field = fields.AddFieldAsXml(fieldXml, true, AddFieldOptions.AddFieldInternalNameHint);
                            }

                            field = ApplyField(field);
                            OnProvisioning?.Invoke(this, field);
                        }
                        else if (Field.DataType == FieldType.Note)
                        {
                            if (field == null)
                            {
                                string fieldXml = $"<Field Type='{Field.DataType}' Name='{Field.Name}' StaticName='{Field.Name}' DisplayName='{Field.Title ?? Field.Name}' RichText='{(Field is NoteFieldAttribute && (Field as NoteFieldAttribute).RichText ? "TRUE" : "FALSE")}' RichTextMode='{(Field is NoteFieldAttribute && (Field as NoteFieldAttribute).RichText ? "FullHtml" : "Compatible")}' IsolateStyles='{(Field is NoteFieldAttribute && (Field as NoteFieldAttribute).RichText ? "TRUE" : "FALSE")}' />";
                                field = fields.AddFieldAsXml(fieldXml, true, AddFieldOptions.AddFieldInternalNameHint);
                            }
                            field = ApplyField(field);
                            OnProvisioning?.Invoke(this, field);
                        }
                        else
                        {
                            if (field == null)
                            {
                                string fieldXml = $"<Field Type='{Field.DataType}' Name='{Field.Name}' StaticName='{Field.Name}' DisplayName='{Field.Title ?? Field.Name}' />";
                                field = fields.AddFieldAsXml(fieldXml, true, AddFieldOptions.AddFieldInternalNameHint);
                            }
                            field = ApplyField(field);
                            OnProvisioning?.Invoke(this, field);
                        }
                    }
                    if (field != null)
                    {
                        field.Update();
                        context.Load(field);
                        context.ExecuteQuery();

                        OnProvisioned?.Invoke(this, field);
                    }
                }
            }
        }