Example #1
0
        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 fieldNameToRemove in configContentType.RemovedFields)
            {
                try
                {
                    RemoveFieldFromContentType(webFields, fieldNameToRemove, contentType);
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("Field {0} could not be removed from the content type {1} with error {2}",
                        fieldNameToRemove, configContentType.DisplayName, ex.Message);
                }
            }
            foreach (var fieldName in configContentType.Fields)
            {
                // We don't want to add removed fields to the content type
                if (configContentType.RemovedFields.Contains(fieldName)) continue;

                try
                {
                    AddOrUpdateFieldOfContentType(configContentType, webFields, fieldName, contentType);
                }
                catch (Exception ex)
                {
                    Log.ErrorFormat("Field {0} could not be added to the content type {1} with error {2}", fieldName,
                        configContentType.DisplayName, ex.Message);
                    Log.Info("Field " + fieldName +
                             " does not exist. If this is a lookup field, run content type creation again after setting up site hierarchy");
                }
            }
        }
Example #2
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();
        }