public override ModelNode ReverseSingleHost(object reverseHost, ReverseOptions options)
        {
            var typedHost = (reverseHost as ContentTypeLinkReverseHost);
            var item = typedHost.HostContentType;

            var def = new ContentTypeLinkDefinition();

            def.ContentTypeName = item.Name;

            return new ContentTypeLinkModelNode
            {
                Options = { RequireSelfProcessing = true },
                Value = def
            };
        }
        protected SPContentType GetListContentType(SPList list, ContentTypeLinkDefinition definition)
        {
            SPContentType result = null;

            if (!string.IsNullOrEmpty(definition.ContentTypeName))
                result = list.ContentTypes[definition.ContentTypeName];

            if (result == null && !string.IsNullOrEmpty(definition.ContentTypeId))
            {
                var linkContenType = new SPContentTypeId(definition.ContentTypeId);
                var bestMatch = list.ContentTypes.BestMatch(linkContenType);

                if (bestMatch.IsChildOf(linkContenType))
                    result = list.ContentTypes[bestMatch];
            }

            return result;
        }
 public static ModelNode AddContentTypeLink(this ModelNode model, ContentTypeLinkDefinition definition, Action<ModelNode> action)
 {
     return model.AddDefinitionNode(definition, action);
 }
 public static ModelNode AddContentTypeLink(this ModelNode model, ContentTypeLinkDefinition definition)
 {
     return AddContentTypeLink(model, definition, null);
 }
        public void CanDeploy_ContentTypeLink_As_CTH_Item_ContentType()
        {
            if (!TestOptions.EnableContentTypeHubTests)
                return;

            WithDisabledPropertyUpdateValidation(() =>
            {
                var listDef = ModelGeneratorService.GetRandomDefinition<ListDefinition>(def =>
                {
                    def.TemplateType = BuiltInListTemplateTypeId.GenericList;
                    def.ContentTypesEnabled = true;
                });

                var ctLinkDef1 = new ContentTypeLinkDefinition
                {
                    ContentTypeName = "cth-item-1",
                    ContentTypeId = "0x01000FF176352927C44BB2DB4FBF2F30E88F"
                };

                var ctLinkDef2 = new ContentTypeLinkDefinition
                {
                    ContentTypeName = "cth-item-2",
                    ContentTypeId = "0x010072398EFE7B102948B9BEE545225CA462"
                };

                var webModel = SPMeta2Model.NewWebModel(web =>
                {
                    web.AddList(listDef, list =>
                    {
                        list.AddContentTypeLink(ctLinkDef1);
                        //list.AddContentTypeLink(ctLinkDef2);
                    });
                });

                TestModel(webModel);
            });
        }
        public void CanDeploy_ContentTypeLink_As_CTH_Document_ContentType()
        {
            if (!TestOptions.EnableContentTypeHubTests)
                return;

            WithDisabledPropertyUpdateValidation(() =>
            {
                var listDef = ModelGeneratorService.GetRandomDefinition<ListDefinition>(def =>
                {
                    def.TemplateType = BuiltInListTemplateTypeId.DocumentLibrary;
                    def.ContentTypesEnabled = true;
                });

                var ctLinkDef1 = new ContentTypeLinkDefinition
                {
                    ContentTypeName = "cth-doc-1",
                    ContentTypeId = "0x01010021C1A5D40722E14591426E165F107547"
                };

                var ctLinkDef2 = new ContentTypeLinkDefinition
                {
                    ContentTypeName = "cth-doc-2",
                    ContentTypeId = "0x010100429E2FB078A6984385E2531F073EA963"
                };

                var webModel = SPMeta2Model.NewWebModel(web =>
                {
                    web.AddList(listDef, list =>
                    {
                        list.AddContentTypeLink(ctLinkDef1);
                        //list.AddContentTypeLink(ctLinkDef2);
                    });
                });

                TestModel(webModel);
            });
        }
        protected ContentType FindListContentType(List list, ContentTypeLinkDefinition contentTypeLinkModel)
        {
            ContentType result = null;

            // TODO
            // https://github.com/SubPointSolutions/spmeta2/issues/68

            // if content type name was not provided, this fails
            // should be re-done by ID and Name
            // OOTB content types could be binded by ID, and custom content types might be binded by name


            // trying to find by name
            if (!string.IsNullOrEmpty(contentTypeLinkModel.ContentTypeName))
            {
                TraceService.InformationFormat((int)LogEventId.ModelProvisionCoreCall,
                    "ContentTypeName is not NULL. Trying to find list content type by ContentTypeName: [{0}]", contentTypeLinkModel.ContentTypeName);

                result = list.ContentTypes.FindByName(contentTypeLinkModel.ContentTypeName);
            }

            // trying to find by content type id
            // will never be resolved, actually
            // list content types have different ID

            //if (result == null && !string.IsNullOrEmpty(contentTypeLinkModel.ContentTypeId))
            //    result = list.ContentTypes.GetById(contentTypeLinkModel.ContentTypeId);

            // trying to find by beat match
            if (result == null)
            {
                TraceService.InformationFormat((int)LogEventId.ModelProvisionCoreCall,
                    "Trying to find list content type by ContentTypeId: [{0}]", contentTypeLinkModel.ContentTypeId);

                // No SPContentTypeCollection.BestMatch() method avialable.
                // http://officespdev.uservoice.com/forums/224641-general/suggestions/6356289-expose-spcontenttypecollection-bestmatch-for-csom

                // TODO, correct best match impl
                foreach (var contentType in list.ContentTypes)
                {
                    if (contentType.Id.ToString().ToUpper().StartsWith(contentTypeLinkModel.ContentTypeId.ToUpper()))
                        result = contentType;
                }
            }

            return result;
        }
        protected ContentType FindSiteContentType(Web web, ContentTypeLinkDefinition contentTypeLinkModel)
        {
            ContentType targetContentType = null;

            if (!string.IsNullOrEmpty(contentTypeLinkModel.ContentTypeName))
                targetContentType = web.AvailableContentTypes.FindByName(contentTypeLinkModel.ContentTypeName);

            if (targetContentType == null && !string.IsNullOrEmpty(contentTypeLinkModel.ContentTypeId))
                targetContentType = web.AvailableContentTypes.FindById(contentTypeLinkModel.ContentTypeId);

            if (targetContentType == null)
                throw new Exception(string.Format("Cannot find content type specified by model: id:[{0}] name:[{1}]",
                                            contentTypeLinkModel.ContentTypeId, contentTypeLinkModel.ContentTypeName));

            return targetContentType;
        }
        protected ContentType FindListContentType(List list, ContentTypeLinkDefinition contentTypeLinkModel)
        {
            // TODO
            // https://github.com/SubPointSolutions/spmeta2/issues/68

            // if content type name was not provided, this fails
            // should be re-done by ID and Name
            // OOTB content types could be binded by ID, and custom content types might be binded by name

            if (!string.IsNullOrEmpty(contentTypeLinkModel.ContentTypeName))
                return list.ContentTypes.FindByName(contentTypeLinkModel.ContentTypeName);

            if (!string.IsNullOrEmpty(contentTypeLinkModel.ContentTypeId))
                return list.ContentTypes.GetById(contentTypeLinkModel.ContentTypeId);

            throw new Exception(
                string.Format("Either ContentTypeName or ContentTypeId must be provides. Can't lookup current list content type by Name:[{0}] and ContentTypeId:[{1}] provided.",
                contentTypeLinkModel.ContentTypeName, contentTypeLinkModel.ContentTypeId));
        }
        public void CanDeploy_CanSetupUniqueContentTypeFieldsOrder_At_OOTB_List_Scope()
        {
            var fieldDef = ModelGeneratorService.GetRandomDefinition<FieldDefinition>(def =>
            {
                def.Hidden = false;
               
                def.ShowInDisplayForm = true;
                def.ShowInEditForm = true;
                def.ShowInListSettings = true;
                def.ShowInNewForm = true;
                def.ShowInViewForms = true;

                def.AddFieldOptions = BuiltInAddFieldOptions.AddToAllContentTypes
                    | BuiltInAddFieldOptions.AddFieldInternalNameHint;
            });

            var listDef = ModelGeneratorService.GetRandomDefinition<ListDefinition>(def =>
            {
                def.ContentTypesEnabled = false;
                def.TemplateType = BuiltInListTemplateTypeId.GenericList;
            });

            var contentTypeLinkDef = new ContentTypeLinkDefinition
            {
                ContentTypeName = BuiltInContentTypeNames.Item,
                ContentTypeId = BuiltInContentTypeId.Item
            };

            var webModel = SPMeta2Model.NewWebModel(web =>
            {
                // adding field first
                web.AddList(listDef, list =>
                {
                    list.AddField(fieldDef, field =>
                    {

                    });
                });

                // then working with the content type
                web.AddList(listDef.Inherit(), list =>
                {
                    list.AddContentTypeLink(contentTypeLinkDef, contenTypeLink =>
                    {
                        contenTypeLink.RegExcludeFromEventsValidation();

                        contenTypeLink.AddUniqueContentTypeFieldsOrder(new UniqueContentTypeFieldsOrderDefinition
                        {

                            Fields = new List<FieldLinkValue>
                            {
                                    new FieldLinkValue {InternalName = fieldDef.InternalName},
                                    new FieldLinkValue {InternalName = "Title"},
                            }
                        });
                    });
                });
            });

            TestModel(webModel);
        }