Esempio n. 1
0
        private void AddOrUpdateFieldOfContentType(ShContentType configContentType, FieldCollection webFields,
                                                   string fieldName,
                                                   ContentType contentType)
        {
            // Need to load content type fields every iteration because fields are added to the collection
            Field webField = webFields.GetByInternalNameOrTitle(fieldName);
            FieldLinkCollection contentTypeFields = contentType.FieldLinks;

            ClientContext.Load(contentTypeFields);
            ClientContext.Load(webField);
            ClientContext.ExecuteQuery();

            var fieldLink = contentTypeFields.FirstOrDefault(existingFieldName => existingFieldName.Name == fieldName);

            if (fieldLink == null)
            {
                var link = new FieldLinkCreationInformation {
                    Field = webField
                };
                fieldLink = contentType.FieldLinks.Add(link);
            }

            fieldLink.Required = configContentType.RequiredFields.Contains(fieldName);
            fieldLink.Hidden   = configContentType.HiddenFields.Contains(fieldName);

            contentType.Update(true);
            ClientContext.ExecuteQuery();
        }
        private void AddSiteColumnsToContentType(ShContentType configContentType)
        {
            Log.Debug("Attempting to add fields to content type " + configContentType.DisplayName);

            Web web = ClientContext.Web;
            ContentTypeCollection contentTypes = web.ContentTypes;

            ClientContext.Load(contentTypes);
            ClientContext.ExecuteQuery();
            ContentType     contentType = contentTypes.GetById(configContentType.ID);
            FieldCollection webFields   = web.Fields;

            ClientContext.Load(contentType);
            ClientContext.Load(webFields);
            ClientContext.ExecuteQuery();

            foreach (var fieldName in configContentType.Fields)
            {
                // Need to load content type fields every iteration because fields are added to the collection
                Field webField = webFields.GetByInternalNameOrTitle(fieldName);
                FieldLinkCollection contentTypeFields = contentType.FieldLinks;
                ClientContext.Load(contentTypeFields);
                ClientContext.Load(webField);
                ClientContext.ExecuteQuery();

                var fieldLink = contentTypeFields.FirstOrDefault(existingFieldName => existingFieldName.Name == fieldName);
                if (fieldLink == null)
                {
                    var link = new FieldLinkCreationInformation {
                        Field = webField
                    };
                    fieldLink = contentType.FieldLinks.Add(link);
                }

                fieldLink.Required = configContentType.RequiredFields.Contains(fieldName);
                if (configContentType.HiddenFields.Contains(fieldName))
                {
                    fieldLink.Hidden   = true;
                    fieldLink.Required = false;
                    //var hiddenField = contentType.Fields.FirstOrDefault(ct => ct.InternalName == fieldName);
                    //if (hiddenField != null)
                    //{
                    //    hiddenField.Required = false;
                    //    hiddenField.Update();
                    //}
                }
                contentType.Update(true);
                ClientContext.ExecuteQuery();
            }
        }
Esempio n. 3
0
        private void RemoveFieldFromContentType(FieldCollection webFields, string fieldNameToRemove,
                                                ContentType contentType)
        {
            // Need to load content type fields every iteration because fields are added to the collection
            Field webField = webFields.GetByInternalNameOrTitle(fieldNameToRemove);
            FieldLinkCollection contentTypeFields = contentType.FieldLinks;

            ClientContext.Load(contentTypeFields);
            ClientContext.Load(webField);
            ClientContext.ExecuteQuery();

            var fieldLink =
                contentTypeFields.FirstOrDefault(
                    existingFieldName => existingFieldName.Name == fieldNameToRemove);

            if (fieldLink != null)
            {
                fieldLink.DeleteObject();
            }
            contentType.Update(true);
            ClientContext.ExecuteQuery();
        }
Esempio n. 4
0
        private void AddSiteColumnsToContentType(GtContentType configContentType)
        {
            Web web = ClientContext.Web;
            ContentTypeCollection contentTypes = web.ContentTypes;

            ClientContext.Load(contentTypes);
            ClientContext.ExecuteQuery();
            ContentType     contentType = contentTypes.GetById(configContentType.ID);
            FieldCollection fields      = web.Fields;

            ClientContext.Load(contentType);
            ClientContext.Load(fields);
            ClientContext.ExecuteQuery();

            foreach (var fieldName in configContentType.Fields)
            {
                // Need to load content type fields every iteration because fields are added to the collection
                Field webField = fields.GetByInternalNameOrTitle(fieldName);
                FieldLinkCollection contentTypeFields = contentType.FieldLinks;
                ClientContext.Load(contentTypeFields);
                ClientContext.Load(webField);
                ClientContext.ExecuteQuery();

                var fieldLink = contentTypeFields.FirstOrDefault(existingFieldName => existingFieldName.Name == fieldName);
                if (fieldLink == null)
                {
                    var link = new FieldLinkCreationInformation {
                        Field = webField
                    };
                    fieldLink = contentType.FieldLinks.Add(link);
                }

                fieldLink.Required = configContentType.RequiredFields.Contains(fieldName);
                fieldLink.Hidden   = configContentType.HiddenFields.Contains(fieldName);
                contentType.Update(true);
                ClientContext.ExecuteQuery();
            }
        }