Beispiel #1
0
        public override void UnProvision(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;
                }
                if (level != ProvisionLevel.Default)
                {
                }
                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);
#if !SP2013 && !SP2016
                    context.Load(list, l => l.AllowDeletion);
#endif
                    try
                    {
                        context.ExecuteQuery();
                    }
                    catch (Exception ex)
                    {
                        list = null;
                    }
                }
                if (list != null)
                {
#if !SP2013 && !SP2016
                    if (list.AllowDeletion)
#endif
                    {
                        if (OnUnProvisioning != null)
                        {
                            OnUnProvisioning.Invoke(this, list);
                        }
                        list.DeleteObject();
                        context.ExecuteQuery();
                        if (OnUnProvisioned != null)
                        {
                            OnUnProvisioned.Invoke(this, list);
                        }
                    }
                }
            }
        }
Beispiel #2
0
        public override void UnProvision(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 if (ContentType.Level == ProvisionLevel.Default)
                {
                    level = List != null ? ProvisionLevel.List : ProvisionLevel.Web;
                }

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

                if (List != null)
                {
                    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;
                //  }
                //}

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

                if (!string.IsNullOrEmpty(ctId))
                {
                    if (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 (level == ProvisionLevel.Web)
                    {
                        webContentType = web.AvailableContentTypes.GetById(ctId);
                        context.Load(webContentType);
                        try
                        {
                            context.ExecuteQuery();
                            ctName = webContentType.Name;
                        }
                        catch
                        {
                            webContentType = null;
                        }
                    }
                }
                else if (!string.IsNullOrEmpty(ctName))
                {
                    if (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 (level == ProvisionLevel.Web)
                    {
                        var webContentTypes = context.LoadQuery(web.AvailableContentTypes.Where(ct => ct.Name == ctName));
                        try
                        {
                            context.ExecuteQuery();
                            webContentType = webContentTypes.FirstOrDefault();
                        }
                        catch
                        {
                            webContentType = null;
                        }
                    }
                }

                if (listContentType != null)
                {
                    if (!listContentType.ReadOnly && !listContentType.Sealed)
                    {
                        OnUnProvisioning?.Invoke(this, listContentType);
                        listContentType.DeleteObject();
                        context.ExecuteQuery();
                        OnUnProvisioned?.Invoke(this, listContentType);
                    }
                }
                if (webContentType != null)
                {
                    if (!webContentType.ReadOnly && !webContentType.Sealed)
                    {
                        OnUnProvisioning?.Invoke(this, webContentType);
                        webContentType.DeleteObject();
                        context.ExecuteQuery();
                        OnUnProvisioned?.Invoke(this, webContentType);
                    }
                }
            }
        }
Beispiel #3
0
        public override void UnProvision(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
                {
                    level = Field.Level;
                    if (level == ProvisionLevel.Default)
                    {
                        if (ContentType != null)
                        {
                            level = ContentType.Level;
                        }
                        if (level == ProvisionLevel.Default)
                        {
                            level = List != null ? ProvisionLevel.List : ProvisionLevel.Web;
                        }
                    }
                }

                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)
                {
                    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;
                    //  }
                    //}
                }

                bool deleted = false;
                if (level == ProvisionLevel.Web)
                {
                    field = web.Fields.GetByInternalNameOrTitle(Field.Name);

                    context.Load(field);
                    try
                    {
                        context.ExecuteQuery();
                    }
                    catch (Exception ex)
                    {
                        field = null;
                    }
                    if (field != null && list == null)
                    {
                        if (field.CanBeDeleted)
                        {
                            field.DeleteObject();
                            OnUnProvisioning?.Invoke(this, field);
                            deleted = true;
                        }
                    }
                }
                if (list != null)
                {
                    Field existField = list.Fields.GetByInternalNameOrTitle(Field.Name);
                    context.Load(existField);
                    try
                    {
                        context.ExecuteQuery();
                    }
                    catch (Exception ex)
                    {
                        existField = null;
                    }
                    if (existField != null)
                    {
                        if (existField.CanBeDeleted)
                        {
                            existField.DeleteObject();
                            OnUnProvisioning?.Invoke(this, field);
                            deleted = true;
                        }
                    }
                    if (field != null)
                    {
                        if (field.CanBeDeleted)
                        {
                            field.DeleteObject();
                            OnUnProvisioning?.Invoke(this, field);
                            deleted = true;
                        }
                    }
                    else
                    {
                        field = existField;
                    }
                }

                if (deleted)
                {
                    context.ExecuteQuery();
                    OnUnProvisioned?.Invoke(this, field);
                }
            }
        }