Create field in SharePoint programmatically using CSOM (Not with XML) -
is possible create fields in sharepoint csom, not using xml?
i've seen many examples using xml, none setting properties field programmatically?
fields.add(new **fieldcreationinformation** { internalname = "test", etc.. });
that's doable, in following example introduced fieldcreationinformation class
:
[xmlroot("field")] public class fieldcreationinformation { [xmlattribute("id")] public guid id { get; set; } [xmlattribute()] public string displayname { get; set; } [xmlattribute("name")] public string internalname { get; set; } [xmlignore()] public bool addtodefaultview { get; set; } //public ienumerable<keyvaluepair<string, string>> additionalattributes { get; set; } [xmlattribute("type")] public fieldtype fieldtype { get; set; } [xmlattribute()] public string group { get; set; } [xmlattribute()] public bool required { get; set; } public string toxml() { var serializer = new xmlserializer(gettype()); var settings = new xmlwritersettings(); settings.indent = true; settings.omitxmldeclaration = true; var emptynamepsaces = new xmlserializernamespaces(new[] { xmlqualifiedname.empty }); using (var stream = new stringwriter()) using (var writer = xmlwriter.create(stream, settings)) { serializer.serialize(writer, this, emptynamepsaces); return stream.tostring(); } } public fieldcreationinformation() { id = guid.newguid(); } }
and extension method creating new field:
public static class fieldcollectionextensions { public static field add(this fieldcollection fields, fieldcreationinformation info) { var fieldschema = info.toxml(); return fields.addfieldasxml(fieldschema, info.addtodefaultview, addfieldoptions.addfieldtodefaultview); } }
usage
var fieldinfo = new fieldcreationinformation(); fieldinfo.fieldtype = fieldtype.geolocation; fieldinfo.internalname = "contactslocation"; fieldinfo.displayname = "contacts location"; ctx.site.rootweb.fields.add(fieldinfo); ctx.executequery();
Comments
Post a Comment