Example #1
0
        public virtual void ConfigureBeforeContentTypeBinding(ClientContext ctx, Web web)
        {
            if (CorrespondingLookupFieldName != null)
            {
                CorrespondingLookupField = web.Fields.GetByInternalNameOrTitle(CorrespondingLookupFieldName);
                Utility.AttachListToLookup(ctx, CorrespondingLookupField, List);
            }
            if (CorrespondingLookupFieldNames != null && CorrespondingLookupFieldNames.Count > 0)
            {
                foreach (var fieldName in CorrespondingLookupFieldNames)
                {
                    var fieldToAttach = web.Fields.GetByInternalNameOrTitle(fieldName);
                    Utility.AttachListToLookup(ctx, fieldToAttach, List);
                }
            }
            if (RemoveFields != null && RemoveFields.Count > 0)
            {
                RefreshList(ctx, web);
                foreach (var removeField in RemoveFields)
                {
                    var deleteField = List.Fields.FirstOrDefault(f => f.InternalName == removeField);
                    deleteField?.DeleteObject();
                }
                ctx.ExecuteQueryRetry();
            }
            if (AdditionalFields != null)
            {
                var excludeFields = new List <string>();

                RefreshList(ctx, web);
                foreach (var existingField in List.Fields)
                {
                    //TODO: Change completely to ID from builder down
                    //This is a temporary hack to deal with crazy mechanics
                    foreach (var field in AdditionalFields)
                    {
                        if (field.Value.ToLower().Contains(existingField.Id.ToString().ToLower()) &&
                            !excludeFields.Contains(field.Key))
                        {
                            excludeFields.Add(field.Key);
                        }
                    }
                }

                foreach (var field in AdditionalFields.Keys)
                {
                    if (!excludeFields.Contains(field))
                    {
                        CleanupTaxonomyHiddenField(ctx, web, AdditionalFields[field], excludeFields);
                        AddFieldAsXml(field, FieldTokenizer.DoTokenReplacement(ctx, AdditionalFields[field]));
                    }
                }
                ctx.ExecuteQueryRetry();
            }
        }
        public void CreateAll(ClientContext ctx)
        {
            _ctx = ctx;
            //Install to rootweb if not a hostweb
            _targetWeb = _ctx.Web.AppInstanceId == default(Guid) ? _ctx.Site.RootWeb : _ctx.Web;

            if (FieldDefinitions != null && FieldDefinitions.Count > 0)
            {
                try
                {
                    GetExistingFields();

                    foreach (var key in FieldDefinitions.Keys)
                    {
                        if (!_existingFields.Contains(key))
                        {
                            //The Version attribute is BAD. Let SharePoint manage it.
                            var schemaXml = FieldDefinitions[key].RemoveXmlAttribute("Version");
                            schemaXml = FieldTokenizer.DoTokenReplacement(_ctx, schemaXml);
                            CleanupTaxonomyHiddenField(schemaXml);
                            _targetWeb.Fields.AddFieldAsXml(schemaXml, true, AddFieldOptions.AddToNoContentType);
                            OnNotify(ProvisioningNotificationLevels.Verbose, "Adding field " + key);
                        }
                        else
                        {
                            OnNotify(ProvisioningNotificationLevels.Verbose, "Skipped existing field " + key);
                        }
                    }
                    _ctx.ExecuteQueryRetry();
                }
                catch (Exception ex)
                {
                    OnNotify(ProvisioningNotificationLevels.Normal,
                             "Error creating fields at " + ctx.Site.RootWeb.Url + " | " + ex);
                    Trace.TraceError("Error creating fields at " + ctx.Site.RootWeb.Url + " | " + ex);
                    throw;
                }
            }
        }