private void DeployHideContentTypeLinks(object modelHost, ContentTypeModelHost contentTypeModelHost, HideContentTypeFieldLinksDefinition hideFieldLinksModel)
        {
            var contentType = contentTypeModelHost.HostContentType;
            var fieldLinks  = contentType.FieldLinks.OfType <SPFieldLink>().ToList();

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioning,
                Object           = contentType,
                ObjectType       = typeof(SPContentType),
                ObjectDefinition = hideFieldLinksModel,
                ModelHost        = modelHost
            });

            // re-order
            foreach (var srcFieldLink in hideFieldLinksModel.Fields)
            {
                SPFieldLink currentFieldLink = null;

                if (!string.IsNullOrEmpty(srcFieldLink.InternalName))
                {
                    currentFieldLink = fieldLinks.FirstOrDefault(c => c.Name == srcFieldLink.InternalName);
                }

                if (currentFieldLink == null && srcFieldLink.Id.HasValue)
                {
                    currentFieldLink = fieldLinks.FirstOrDefault(c => c.Id == srcFieldLink.Id.Value);
                }

                if (currentFieldLink != null)
                {
                    currentFieldLink.ReadOnly = false;
                    currentFieldLink.Required = false;
                    currentFieldLink.Hidden   = true;
                }
            }

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioned,
                Object           = contentType,
                ObjectType       = typeof(SPContentType),
                ObjectDefinition = hideFieldLinksModel,
                ModelHost        = modelHost
            });

            contentTypeModelHost.ShouldUpdateHost = true;
        }
Beispiel #2
0
        private void DeployContentTypeOrder(object modelHost, ContentTypeModelHost contentTypeModellHost, UniqueContentTypeFieldsOrderDefinition reorderFieldLinksModel)
        {
            var contentType = contentTypeModellHost.HostContentType;

            var fieldLinks = contentType.FieldLinks.OfType <SPFieldLink>().ToList();
            var fields     = contentType.Fields.OfType <SPField>().ToList();

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioning,
                Object           = contentType,
                ObjectType       = typeof(SPContentType),
                ObjectDefinition = reorderFieldLinksModel,
                ModelHost        = modelHost
            });

            var newOrder = new List <string>();

            // re-order
            foreach (var srcFieldLink in reorderFieldLinksModel.Fields)
            {
                SPField currentField = null;

                if (!string.IsNullOrEmpty(srcFieldLink.InternalName))
                {
                    currentField = fields.FirstOrDefault(c => c.InternalName == srcFieldLink.InternalName);
                }

                if (currentField == null && srcFieldLink.Id.HasValue)
                {
                    currentField = fields.FirstOrDefault(c => c.Id == srcFieldLink.Id.Value);
                }

                if (currentField != null)
                {
                    var ctField = contentType.Fields[currentField.Id];

                    // must always be internal name of the field
                    if (!newOrder.Contains(ctField.InternalName))
                    {
                        newOrder.Add(ctField.InternalName);
                    }
                }
            }

            if (newOrder.Count > 0)
            {
                contentType.FieldLinks.Reorder(newOrder.ToArray());
            }

            InvokeOnModelEvent(this, new ModelEventArgs
            {
                CurrentModelNode = null,
                Model            = null,
                EventType        = ModelEventType.OnProvisioned,
                Object           = contentType,
                ObjectType       = typeof(SPContentType),
                ObjectDefinition = reorderFieldLinksModel,
                ModelHost        = modelHost
            });

            contentTypeModellHost.ShouldUpdateHost = true;
        }