Example #1
0
        private void CreateTaxonomyField(ShField field, FieldCollection fields)
        {
            Log.Debug("Attempting to create taxonomy field " + field.DisplayName);
            var fieldSchema = field.GetFieldAsXml();
            var newField = fields.AddFieldAsXml(fieldSchema, false, AddFieldOptions.AddFieldInternalNameHint);
            ClientContext.Load(newField);
            ClientContext.ExecuteQuery();

            var termSetId = GetTermSetId(field);
            var newTaxonomyField = ClientContext.CastTo<TaxonomyField>(newField);
            newTaxonomyField.SspId = field.SspId;
            newTaxonomyField.TermSetId = termSetId;
            newTaxonomyField.TargetTemplate = String.Empty;
            newTaxonomyField.AnchorId = Guid.Empty;
            newTaxonomyField.CreateValuesInEditForm = field.OpenTermSet;
            newTaxonomyField.Open = field.OpenTermSet;
            newTaxonomyField.Update();
            ClientContext.ExecuteQuery();
        }
Example #2
0
        private void CreateField(ShField field, FieldCollection fields)
        {
            // code to handle lookup fields. does not work if the list has not been created...
            if (field.Type == "Lookup" || field.Type == "LookupMulti")
            {
                var web = ClientContext.Web;
                if (!web.IsObjectPropertyInstantiated("Lists"))
                {
                    ClientContext.Load(web, x => x.Lists);
                    ClientContext.ExecuteQuery();
                }

                var listTitle = field.List;
                var list = web.Lists.GetByTitle(listTitle);

                try
                {
                    ClientContext.Load(list, x=>x.Id);
                    ClientContext.ExecuteQuery();
                    field.List = list.Id.ToString();
                }
                catch (Exception)
                {
                    Log.Info("Lookup field " + field.DisplayName + " id:" + field.ID + " cannot be created since the list " + listTitle + " has not been created. Please rund content type creation again after setting up the site hierarchy");
                    return;
                }

            }

            var fieldXml = field.GetFieldAsXml();
            Field newField = fields.AddFieldAsXml(fieldXml, true, AddFieldOptions.AddFieldInternalNameHint);

            ClientContext.Load(newField);
            ClientContext.ExecuteQuery();
        }