{"id":1724,"date":"2014-09-30T17:18:17","date_gmt":"2014-09-30T16:18:17","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=1724"},"modified":"2014-09-30T17:18:17","modified_gmt":"2014-09-30T16:18:17","slug":"quick-tip-21","status":"publish","type":"post","link":"https:\/\/www.stuartroberts.net\/index.php\/2014\/09\/30\/quick-tip-21\/","title":{"rendered":"Add Field with XML"},"content":{"rendered":"<p><strong>SharePoint Short #21<\/strong><\/p>\n<p>A quick short today \ud83d\ude42<\/p>\n<p>Instead of adding new fields to a content type or list creating the object (SPFieldText, etc.) and adding it, simply pass the XML definition for whatever field you are looking to create into the AddFieldAsXml method of the Fields collection.<\/p>\n<pre lang=\"csharp\">\r\npublic void AddFieldToList(SPList list, string xmlDefinition)\r\n{\r\n    string fieldName = list.Fields.AddFieldAsXml(xmlDefinition);\r\n    SPField newField = list.Fields.GetField(fieldName);\r\n    \/\/ Do whatever you need to with the new field\r\n    \/\/\r\n    \/\/ and then update it.\r\n    newField.Update();\r\n}\r\n<\/pre>\n<p>The xmlDefinition is simply a copy of the Field tag, for example from a content type definition.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SharePoint Short #21 A quick short today \ud83d\ude42 Instead of adding new fields to a content type or list creating the object (SPFieldText, etc.) and adding it, simply pass the XML definition for whatever field you are looking to create &hellip; <a href=\"https:\/\/www.stuartroberts.net\/index.php\/2014\/09\/30\/quick-tip-21\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[3,44],"tags":[],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-rO","_links":{"self":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1724"}],"collection":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=1724"}],"version-history":[{"count":1,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1724\/revisions"}],"predecessor-version":[{"id":1725,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1724\/revisions\/1725"}],"wp:attachment":[{"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=1724"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=1724"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=1724"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}